1

I started a new app which uses firebase features heavily as well as the support libraries. I quickly hit the 65k dex limit, even though there is no reason I should be there given the simplicity of the app. I know I need to exclude certain dependencies that I have no use for, so I have been using gradlew app:dependencies to analyze the dependencies and pull out what is pulled in more than once or things I don't need.

Despite tons of excludes, I'm unable to get the method count down considerably (only 3%). Am I just stuck with multi-dex? If not, how do I effectively decrease method count more?

More information:

  • minSdk 19

Here is the before/after of build.gradle depencies block and the gradlew app:dependencies output. I'm happy to post the dexcount text output of how many methods each library has if that will help. I can say that the biggest contributors to method count are the support libs and com.google.common.* and com.google.android.gms.*

Before Slimming

  • Total methods in app-debug.apk: 67707 (103.31% used)

After Slimming

  • Total methods in app-debug.apk: 65852 (100.48% used)

Before: build.gradle dependency block (no excludes)

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:multidex:1.0.2'
    implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
    implementation "com.android.support:recyclerview-v7:$supportLibraryVersion"
    implementation "com.android.support:design:$supportLibraryVersion"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation "com.android.support:cardview-v7:$supportLibraryVersion"
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
    implementation('com.oakwoodsc.rxfirestore:rxfirestore-debug:1.0@aar') {
        exclude group: 'io.reactivex.rxjava2', module: 'rxandroid'
        exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
    }
    implementation 'com.firebaseui:firebase-ui-auth:3.1.0' // Remove once custom version
    implementation "com.google.android.gms:play-services-location:$firebaseVersion"
    implementation "com.google.firebase:firebase-firestore:$firebaseVersion"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Before: gradlew app:dependencies output

Lots of duplication!

+--- com.google.firebase:firebase-core:11.4.2
|    \--- com.google.firebase:firebase-analytics:11.4.2
|         +--- com.google.android.gms:play-services-basement:11.4.2
|         |    +--- com.android.support:support-v4:25.2.0 -> 26.1.0
|         |    |    +--- com.android.support:support-compat:26.1.0
|         |    |    |    +--- com.android.support:support-annotations:26.1.0
|         |    |    |    \--- android.arch.lifecycle:runtime:1.0.0
|         |    |    |         +--- android.arch.lifecycle:common:1.0.0
|         |    |    |         \--- android.arch.core:common:1.0.0
|         |    |    +--- com.android.support:support-media-compat:26.1.0
|         |    |    |    +--- com.android.support:support-annotations:26.1.0
|         |    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|         |    |    +--- com.android.support:support-core-utils:26.1.0
|         |    |    |    +--- com.android.support:support-annotations:26.1.0
|         |    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|         |    |    +--- com.android.support:support-core-ui:26.1.0
|         |    |    |    +--- com.android.support:support-annotations:26.1.0
|         |    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|         |    |    \--- com.android.support:support-fragment:26.1.0
|         |    |         +--- com.android.support:support-compat:26.1.0 (*)
|         |    |         +--- com.android.support:support-core-ui:26.1.0 (*)
|         |    |         \--- com.android.support:support-core-utils:26.1.0 (*)
|         |    \--- com.google.android.gms:play-services-basement-license:11.4.2
|         +--- com.google.firebase:firebase-common:11.4.2
|         |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|         |    +--- com.google.android.gms:play-services-tasks:11.4.2
|         |    |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|         |    |    \--- com.google.android.gms:play-services-tasks-license:11.4.2
|         |    \--- com.google.firebase:firebase-common-license:11.4.2
|         +--- com.google.firebase:firebase-analytics-impl:11.4.2
|         |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|         |    +--- com.google.firebase:firebase-iid:11.4.2
|         |    |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|         |    |    +--- com.google.firebase:firebase-common:11.4.2 (*)
|         |    |    \--- com.google.firebase:firebase-iid-license:11.4.2
|         |    +--- com.google.firebase:firebase-common:11.4.2 (*)
|         |    +--- com.google.android.gms:play-services-tasks:11.4.2 (*)
|         |    \--- com.google.firebase:firebase-analytics-impl-license:11.4.2
|         \--- com.google.firebase:firebase-analytics-license:11.4.2
+--- com.android.support:multidex:1.0.2
+--- com.android.support:appcompat-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0
|    +--- com.android.support:support-v4:26.1.0 (*)
|    +--- com.android.support:support-vector-drawable:26.1.0
|    |    +--- com.android.support:support-annotations:26.1.0
|    |    \--- com.android.support:support-compat:26.1.0 (*)
|    \--- com.android.support:animated-vector-drawable:26.1.0
|         +--- com.android.support:support-vector-drawable:26.1.0 (*)
|         \--- com.android.support:support-core-ui:26.1.0 (*)
+--- com.android.support:recyclerview-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0
|    +--- com.android.support:support-compat:26.1.0 (*)
|    \--- com.android.support:support-core-ui:26.1.0 (*)
+--- com.android.support:design:26.1.0
|    +--- com.android.support:support-v4:26.1.0 (*)
|    +--- com.android.support:appcompat-v7:26.1.0 (*)
|    +--- com.android.support:recyclerview-v7:26.1.0 (*)
|    \--- com.android.support:transition:26.1.0
|         +--- com.android.support:support-annotations:26.1.0
|         \--- com.android.support:support-v4:26.1.0 (*)
+--- com.android.support.constraint:constraint-layout:1.0.2 -> 1.1.0-beta1
|    \--- com.android.support.constraint:constraint-layout-solver:1.1.0-beta1
+--- com.android.support:cardview-v7:26.1.0
|    \--- com.android.support:support-annotations:26.1.0
+--- io.reactivex.rxjava2:rxandroid:2.0.1
|    \--- io.reactivex.rxjava2:rxjava:2.0.1 -> 2.1.5
|         \--- org.reactivestreams:reactive-streams:1.0.1
+--- io.reactivex.rxjava2:rxjava:2.1.5 (*)
+--- com.oakwoodsc.rxfirestore:rxfirestore-debug:1.0
+--- com.firebaseui:firebase-ui-auth:3.1.0
|    +--- com.android.support:design:26.1.0 (*)
|    +--- com.android.support:customtabs:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0 (*)
|    |    \--- com.android.support:support-annotations:26.1.0
|    +--- com.android.support.constraint:constraint-layout:1.1.0-beta1 (*)
|    +--- com.google.firebase:firebase-auth:11.4.2
|    |    +--- com.google.android.gms:play-services-base:11.4.2
|    |    |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|    |    |    +--- com.google.android.gms:play-services-tasks:11.4.2 (*)
|    |    |    \--- com.google.android.gms:play-services-base-license:11.4.2
|    |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|    |    +--- com.google.firebase:firebase-common:11.4.2 (*)
|    |    +--- com.google.android.gms:play-services-tasks:11.4.2 (*)
|    |    \--- com.google.firebase:firebase-auth-license:11.4.2
|    +--- com.google.android.gms:play-services-auth:11.4.2
|    |    +--- com.google.android.gms:play-services-auth-api-phone:11.4.2
|    |    |    +--- com.google.android.gms:play-services-base:11.4.2 (*)
|    |    |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|    |    |    +--- com.google.android.gms:play-services-tasks:11.4.2 (*)
|    |    |    \--- com.google.android.gms:play-services-auth-api-phone-license:11.4.2
|    |    +--- com.google.android.gms:play-services-auth-base:11.4.2
|    |    |    +--- com.google.android.gms:play-services-base:11.4.2 (*)
|    |    |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|    |    |    +--- com.google.android.gms:play-services-tasks:11.4.2 (*)
|    |    |    \--- com.google.android.gms:play-services-auth-base-license:11.4.2
|    |    +--- com.google.android.gms:play-services-base:11.4.2 (*)
|    |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|    |    +--- com.google.android.gms:play-services-tasks:11.4.2 (*)
|    |    \--- com.google.android.gms:play-services-auth-license:11.4.2
|    \--- com.android.support:cardview-v7:26.1.0 (*)
+--- com.google.android.gms:play-services-location:11.4.2
|    +--- com.google.android.gms:play-services-base:11.4.2 (*)
|    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|    +--- com.google.android.gms:play-services-tasks:11.4.2 (*)
|    \--- com.google.android.gms:play-services-location-license:11.4.2
\--- com.google.firebase:firebase-firestore:11.4.2
     +--- com.google.android.gms:play-services-basement:11.4.2 (*)
     +--- com.google.firebase:firebase-common:11.4.2 (*)
     +--- com.google.android.gms:play-services-tasks:11.4.2 (*)
     +--- com.squareup.okhttp:okhttp:2.7.2
     |    \--- com.squareup.okio:okio:1.6.0
     \--- com.google.guava:guava:20.0

After: build.gradle dependency block (with excludes all over)

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation("com.google.android.gms:play-services-base:$playServicesVersion") {
        exclude group: "com.android.support", module: "support-v4"
    }
    implementation 'com.android.support:multidex:1.0.2'
    implementation "com.android.support:design:$supportLibraryVersion"
    implementation("com.firebaseui:firebase-ui-auth:3.1.0") {  // Remove once custom version
        exclude group: "com.google.android.gms", module: "play-services-base"
        exclude group: "com.google.android.gms", module: "play-services-basement"
        exclude group: "com.google.android.gms", module: "play-services-tasks"
        exclude group: "com.android.support", module: "design"
        exclude group: "com.android.support", module: "support-compat"
        exclude group: "com.android.support", module: "support-annotations"
        exclude group: "com.android.support", module: "cardview-v7"
    }
    implementation("com.android.support:cardview-v7:$supportLibraryVersion") {
        exclude group: "com.android.support", module: "support-annotations"
    }
    implementation('com.oakwoodsc.rxfirestore:rxfirestore-debug:1.0@aar') {
        exclude group: 'io.reactivex.rxjava2', module: 'rxandroid'
        exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
    }
    implementation("com.google.android.gms:play-services-location:$playServicesVersion") {
        exclude group: "com.google.android.gms", module: "play-services-base"
        exclude group: "com.google.android.gms", module: "play-services-tasks"
        exclude group: "com.google.android.gms", module: "play-services-basement"
    }
    implementation("com.google.firebase:firebase-firestore:$playServicesVersion") {
        exclude group: "com.google.android.gms", module: "play-services-basement"
        exclude group: "com.google.android.gms", module: "play-services-tasks"
    }
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

After: gradlew app:dependencies output:

Less duplication...

+--- com.google.android.gms:play-services-base:11.4.2
|    +--- com.google.android.gms:play-services-basement:11.4.2
|    |    \--- com.google.android.gms:play-services-basement-license:11.4.2
|    +--- com.google.android.gms:play-services-tasks:11.4.2
|    |    +--- com.google.android.gms:play-services-basement:11.4.2 (*)
|    |    \--- com.google.android.gms:play-services-tasks-license:11.4.2
|    \--- com.google.android.gms:play-services-base-license:11.4.2
+--- com.android.support:multidex:1.0.2
+--- com.android.support:design:26.1.0
|    +--- com.android.support:support-v4:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- android.arch.lifecycle:runtime:1.0.0
|    |    |         +--- android.arch.lifecycle:common:1.0.0
|    |    |         \--- android.arch.core:common:1.0.0
|    |    +--- com.android.support:support-media-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    +--- com.android.support:support-core-utils:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    +--- com.android.support:support-core-ui:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    \--- com.android.support:support-fragment:26.1.0
|    |         +--- com.android.support:support-compat:26.1.0 (*)
|    |         +--- com.android.support:support-core-ui:26.1.0 (*)
|    |         \--- com.android.support:support-core-utils:26.1.0 (*)
|    +--- com.android.support:appcompat-v7:26.1.0
|    |    +--- com.android.support:support-annotations:26.1.0
|    |    +--- com.android.support:support-v4:26.1.0 (*)
|    |    +--- com.android.support:support-vector-drawable:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    \--- com.android.support:animated-vector-drawable:26.1.0
|    |         +--- com.android.support:support-vector-drawable:26.1.0 (*)
|    |         \--- com.android.support:support-core-ui:26.1.0 (*)
|    +--- com.android.support:recyclerview-v7:26.1.0
|    |    +--- com.android.support:support-annotations:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0 (*)
|    |    \--- com.android.support:support-core-ui:26.1.0 (*)
|    \--- com.android.support:transition:26.1.0
|         +--- com.android.support:support-annotations:26.1.0
|         \--- com.android.support:support-v4:26.1.0 (*)
+--- com.firebaseui:firebase-ui-auth:3.1.0
|    +--- com.android.support:customtabs:26.1.0
|    +--- com.android.support.constraint:constraint-layout:1.1.0-beta1
|    |    \--- com.android.support.constraint:constraint-layout-solver:1.1.0-beta1
|    +--- com.google.firebase:firebase-auth:11.4.2
|    |    +--- com.google.firebase:firebase-common:11.4.2
|    |    |    \--- com.google.firebase:firebase-common-license:11.4.2
|    |    \--- com.google.firebase:firebase-auth-license:11.4.2
|    \--- com.google.android.gms:play-services-auth:11.4.2
|         +--- com.google.android.gms:play-services-auth-api-phone:11.4.2
|         |    \--- com.google.android.gms:play-services-auth-api-phone-license:11.4.2
|         +--- com.google.android.gms:play-services-auth-base:11.4.2
|         |    \--- com.google.android.gms:play-services-auth-base-license:11.4.2
|         \--- com.google.android.gms:play-services-auth-license:11.4.2
+--- com.android.support:cardview-v7:26.1.0
+--- com.oakwoodsc.rxfirestore:rxfirestore-debug:1.0
+--- com.google.android.gms:play-services-location:11.4.2
|    \--- com.google.android.gms:play-services-location-license:11.4.2
+--- com.google.firebase:firebase-firestore:11.4.2
|    +--- com.google.firebase:firebase-common:11.4.2 (*)
|    +--- com.squareup.okhttp:okhttp:2.7.2
|    |    \--- com.squareup.okio:okio:1.6.0
|    \--- com.google.guava:guava:20.0
+--- io.reactivex.rxjava2:rxandroid:2.0.1
|    \--- io.reactivex.rxjava2:rxjava:2.0.1 -> 2.1.5
|         \--- org.reactivestreams:reactive-streams:1.0.1
\--- io.reactivex.rxjava2:rxjava:2.1.5 (*)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Brandon
  • 1,886
  • 2
  • 17
  • 28
  • Did you run lint? – Pomagranite Dec 14 '17 at 16:19
  • "Lots of duplication" -- Gradle nets those out. You do not need to exclude duplicates. "how do I effectively decrease method count more?" -- get rid of dependencies *that you are requesting*. `appcompat-v7` alone is ~20% of the way to the 64K DEX method reference limit. – CommonsWare Dec 14 '17 at 16:20
  • Thanks for the comment @CommonsWare. Given your comment [here](https://stackoverflow.com/a/31297546/4284981), and my understanding that I'd lose the back-ported Material Design on devices running < API 21, I don't think I want to lose `appcompat-v7`. Like @Tanis.7x notes in their answer, it might be more worth than it's worth at that point. I will probably attempt to enable Proguard and see if that gets me down in method count – Brandon Dec 14 '17 at 16:36

2 Answers2

1

First things first, you don't need all those excludes. If two dependencies use com.android.support:support-v4:26.1.0, it is only included once. It is listed twice so you can see who depends on it.

One option you may want to consider is turning on ProGuard. This will strip out any unused code, including library code. In your case, it will likely get you under the 65k limit.

To use it, set this in your build.gradle: release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }

You will want to create proguard-rules.pro and populate it with some directive specific to your app. Be sure to check out the full documentation to see what you might need to include there.

While it is possible to stay under the 65k limit without ProGuard, most apps of moderate complexity will surpass it fairly quickly, and I've found that avoiding libraries such as AppCompat in an effort to stay under the 65k limit is generally more work than it is worth.

Even with ProGuard, some apps will encounter the 65k limit. Other apps may not want to use ProGuard for one reason or another. In these cases, your best option is to enable multidex, which allows you to go over the 65k limit.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • I'm going to give this a shot and see how it works, will give the green check the 'ol clickaroo if I can make it happen based on your answer. – Brandon Dec 14 '17 at 16:37
  • Thanks for your answer. With Proguard enabled for release build type, method count is only ~26k. Now, the question is: Do I enable Proguard for debug mode, and if I _don't_, is there a way to only enable multidex for debug? I'm aiming to get rid of it because of it's build-time implications. – Brandon Dec 14 '17 at 16:49
  • Yes, you can enable it for debug mode if you want. It may be a good idea to, to see how the app responds during release. Enabling multidex is placed within the `android.defaultConfig` gradle object in your app's `build.gradle`, so before you build a release dont forget to remove the `multidex true` command. – martinomburajr Dec 14 '17 at 16:59
  • Yep, see: https://stackoverflow.com/questions/37151047/can-i-enable-multidex-in-android-debug-build-only – Bryan Herbst Dec 14 '17 at 17:02
  • Instead of doing multidex on only debug, I enabled Proguard and resource shrinking on debug (the same as release). This seems to be working just fine, and is preferable to multidex being enabled. – Brandon Dec 14 '17 at 17:04
1

Proguard is the key to your problems. Ensure that your app's build.gradle file has the following under buildtypes

buildTypes {
    //debug
    //staging
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

In my case my release build, minifies the code, shrinks resources, and most importantly uses proguard to reduce code bloat brought by other libraries. You can copy the code proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

Watch this awesome short video on the benefits of Proguard by David East.

Also if you haven't already check the Proguard documentation out

martinomburajr
  • 1,235
  • 14
  • 29