Post on 17-Jan-2017
transcript
Tips & TricksAndroidSebastian Świerczek
Tips & Tricks list
#1 Gradle libraries versions
#2 Android Support Annotations
#3 Dependencies conflicts resolving
#4 StrictMode
#5 Tools Attributes
Gradle libraries versions
#1
Versions for single module
def libVersion = [ support : "23.0.1", supportTest: "0.4"]
dependencies { compile "com.android.support:appcompat-v7:${libVersion.support}" compile "com.android.support:recyclerview-v7:${libVersion.support}"
androidTestCompile "com.android.support.test:runner:${libVersion.supportTest}" androidTestCompile "com.android.support.test:rules:${libVersion.supportTest}"}
#1 Gradle libraries versions
root > appModule > build.gradle > module2 > build.gradle > build.gradle
Versions for all modules
ext { libVersion = [ support : "23.0.1", supportTest: "0.4" ]
libDependencies = [ recyclerView : "com.android.support:recyclerview-v7:${libVersion.support}", appCompat : "com.android.support:appcompat-v7:${libVersion.support}",
supportTestRunner : "com.android.support.test:runner:${libVersion.supportTest}", supportTestRules : "com.android.support.test:rules:${libVersion.supportTest}" ]}
#1 Gradle libraries versions
root > appModule > build.gradle > module2 > build.gradle > build.gradle
Versions for all modules
dependencies { compile libDependencies.recyclerView compile libDependencies.appCompat
androidTestCompile libDependencies.supportTestRunner androidTestCompile libDependencies.supportTestRules}
#1 Gradle libraries versions
root > appModule > build.gradle > module2 > build.gradle > build.gradle
Android Support Annotations
#2
#2 Android Support Annotations
Resource Type Annotations
public void passStringResource(@StringRes int stringResId){...}
public void passColorResource(@ColorRes int colorResId){...}
public void passDrawable(@DrawableRes int drawableResId){...}
public void passColorRgbInt(@ColorInt int rgbColor){...}
public void passStringOrColor(@StringRes @ColorRes int stringOrColorResId){...}
#2 Android Support Annotations
Resource Type Annotations
passStringResource(R.string.app_name);
passStringResource(R.color.black);passStringResource(1234);
#2 Android Support Annotations
Resource Type Annotations
passStringOrColorResource(R.string.app_name);passStringOrColorResource(android.R.color.white);
passStringOrColorResource(R.drawable.ic_launcher);
#2 Android Support Annotations
Resource Type Annotations
passColorRgbInt(resources.getColor(R.color.white));passColorRgbInt(0xFFFFFFFF);
passColorRgbInt(R.color.white);
#2 Android Support Annotations
Typedef Annotations
@IntDef ({DOWNLOAD_IDLE,DOWNLOAD_IN_PROGRESS})
public @interface DownloadStatus {}
public static final int DOWNLOAD_IDLE = 0;public static final int DOWNLOAD_IN_PROGRESS = 1;
#2 Android Support Annotations
Typedef Annotations
public void setDownloadStatus(@DownloadStatus int status){...}
#2 Android Support Annotations
Typedef Annotations
setDownloadStatus(DOWNLOAD_IDLE);setDownloadStatus(DOWNLOAD_IN_PROGRESS);
setDownloadStatus(1234);
Dependencies conflicts resolving
#3
#3 Dependencies conflicts resolving
Google Play Services dependencies conflict
Error:Execution failed for task ':app:processDebugResources'.> Error: more than one library with package name
'com.google.android.gms' You can temporarily disable this error with
android.enforceUniquePackageName=false However, this is temporary and will be enforced in 1.0
#3 Dependencies conflicts resolving
Dependencies
compile 'com.google.android.gms:play-services-maps:8.1.0' compile 'co.realtime:messaging-android:2.1.52'
#3 Dependencies conflicts resolving
androidDependencies task to the rescue
./gradlew androidDependencies
#3 Dependencies conflicts resolving
androidDependencies task to the rescue
+--- com.google.android.gms:play-services-maps:8.1.0| \--- com.google.android.gms:play-services-base:8.1.0| \--- com.google.android.gms:play-services-basement:8.1.0| \--- com.android.support:support-v4:23.0.1| \--- LOCAL: internal_impl-23.0.1.jar+--- co.realtime:messaging-android:2.1.52| +--- LOCAL: json_simple-1.1.jar| +--- LOCAL: httpcore-4.2.4.jar| +--- com.google.android.gms:play-services:6.1.71
#3 Dependencies conflicts resolving
Fix the issue
compile 'com.google.android.gms:play-services:8.1.0' compile 'co.realtime:messaging-android:2.1.52'
StrictMode
#4
#4 StrictMode
Detect UI Thread operations
if (DEBUG) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build());}
#4 StrictMode
Example output
StrictMode policy violation; ~duration=6193 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=31 violation=2
at (…) onReadFromDisk(StrictMode.java:1137)(…)at (…) (MainActivity.java:36)
Tools Attributes
#5
#5 Tools Attributes
Tools Layout Attributes
View in designer.
<FrameLayout (...) xmlns:tools="http://schemas.android.com/tools"> <TextView (...) android:text="@string/hello_world" tools:text="@string/long_text" /> <Button android:id="@+id/youCanSeeMeAtRuntime"(...) android:visibility="visible" tools:visibility="gone" /></FrameLayout>
#5 Tools Attributes
Tools Layout Attributes
View at runtime.
#5 Tools Attributes
Tools Layout Pointer
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:id="@+id/my_fragment" android:name="com.example.user.exampleapp.MyFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout="@layout/my_fragment_layout" /></FrameLayout>
#5 Tools Attributes
Tools Layout Pointer
Without: With: