Tips & Tricks Android

28
Tips & Tricks Android Sebastian Świerczek

Transcript of Tips & Tricks Android

Page 1: Tips & Tricks Android

Tips & TricksAndroidSebastian Świerczek

Page 2: Tips & Tricks Android

Tips & Tricks list

#1 Gradle libraries versions

#2 Android Support Annotations

#3 Dependencies conflicts resolving

#4 StrictMode

#5 Tools Attributes

Page 3: Tips & Tricks Android

Gradle libraries versions

#1

Page 4: Tips & Tricks Android

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

Page 5: Tips & Tricks Android

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

Page 6: Tips & Tricks Android

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

Page 7: Tips & Tricks Android

Android Support Annotations

#2

Page 8: Tips & Tricks Android

#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){...}

Page 9: Tips & Tricks Android

#2 Android Support Annotations

Resource Type Annotations

passStringResource(R.string.app_name);

passStringResource(R.color.black);passStringResource(1234);

Page 10: Tips & Tricks Android

#2 Android Support Annotations

Resource Type Annotations

passStringOrColorResource(R.string.app_name);passStringOrColorResource(android.R.color.white);

passStringOrColorResource(R.drawable.ic_launcher);

Page 11: Tips & Tricks Android

#2 Android Support Annotations

Resource Type Annotations

passColorRgbInt(resources.getColor(R.color.white));passColorRgbInt(0xFFFFFFFF);

passColorRgbInt(R.color.white);

Page 12: Tips & Tricks Android

#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;

Page 13: Tips & Tricks Android

#2 Android Support Annotations

Typedef Annotations

public void setDownloadStatus(@DownloadStatus int status){...}

Page 14: Tips & Tricks Android

#2 Android Support Annotations

Typedef Annotations

setDownloadStatus(DOWNLOAD_IDLE);setDownloadStatus(DOWNLOAD_IN_PROGRESS);

setDownloadStatus(1234);

Page 15: Tips & Tricks Android

Dependencies conflicts resolving

#3

Page 16: Tips & Tricks Android

#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

Page 17: Tips & Tricks Android

#3 Dependencies conflicts resolving

Dependencies

compile 'com.google.android.gms:play-services-maps:8.1.0' compile 'co.realtime:messaging-android:2.1.52'

Page 18: Tips & Tricks Android

#3 Dependencies conflicts resolving

androidDependencies task to the rescue

./gradlew androidDependencies

Page 19: Tips & Tricks Android

#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

Page 20: Tips & Tricks Android

#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'

Page 21: Tips & Tricks Android

StrictMode

#4

Page 22: Tips & Tricks Android

#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());}

Page 23: Tips & Tricks Android

#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)

Page 24: Tips & Tricks Android

Tools Attributes

#5

Page 25: Tips & Tricks Android

#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>

Page 26: Tips & Tricks Android

#5 Tools Attributes

Tools Layout Attributes

View at runtime.

Page 27: Tips & Tricks Android

#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>

Page 28: Tips & Tricks Android

#5 Tools Attributes

Tools Layout Pointer

Without: With: