Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Tips & Tricks

Android Tips & Tricks

Short presentation about my favorite tips & tricks for Android developers.

Meet.mobile Szczecin 28 September 2015

Sebastian Świerczek

October 04, 2015
Tweet

Other Decks in Programming

Transcript

  1. Tips & Tricks list #1 Gradle libraries versions #2 Android

    Support Annotations #3 Dependencies conflicts resolving #4 StrictMode #5 Tools Attributes
  2. 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
  3. 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
  4. 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
  5. #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){...}
  6. #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;
  7. #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
  8. #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
  9. #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()); }
  10. #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)
  11. #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>
  12. #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>