Search code examples
androidandroid-gradle-pluginproguardandroid-proguardandroid-multidex

May Proguard save going to multidex app ?


I have an app with a few libs, that reached the red-line of 65536 method count.

  1. I achieved to setup the app as an multidex APK.
  2. For size optimisation, I decided to use Proguard, because I just use a few feature of Guava and common.java.lang, and those libs bring their whole family with them.
  3. After Proguard job, my app ref ~ 45 Kmethods
  4. I often read that multidex app may crash time to time
  5. And that because of second-dex runtime loading, this take time.

Does 4 and 5 are true ?

Then I just tried to not using mutidex, because my end methods count is < 56Kmethods with prodGuard, but it failed as if it has more !

To do so, I just put the gradle parameter multiDexEnabled to false

Is there something else to check/do ?

Here is a part of my Gradle :

android {
    compileSdkVersion ANDROID_BUILD_SDK_VERSION
    buildToolsVersion ANDROID_BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId "XXXX"
        targetSdkVersion ANDROID_BUILD_TARGET_SDK_VERSION
        minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION
        versionCode ANDROID_BUILD_VERSION_CODE
        versionName ANDROID_BUILD_APP_VERSION_NAME
        // Enabling multidex support.
        multiDexEnabled false
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            debuggable false
            ext.enableCrashlytics = true
            renderscriptOptimLevel 3
            signingConfig android.signingConfigs.release
            zipAlignEnabled true
            minifyEnabled true
            //  shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro', 'proguard-rules-new.pro', 'proguard-rules-eventbus.pro', 'proguard-rules-firebase.pro', 'proguard-rules-fabric.pro', 'proguard-rules-leakcanary.pro'
        }
        debug {
            debuggable true
            renderscriptOptimLevel 3
            applicationIdSuffix ".debug"
            versionNameSuffix "debug"
        }
    }

Solution

    1. I often read that multidex app may crash time to time

    From the android developers documentation page (http://developer.android.com/tools/building/multidex.html#limitations):

    Applications using a multidex configuration that make very large memory allocation requests may crash during run time due to a Dalvik linearAlloc limit (Issue 78035). The allocation limit was increased in Android 4.0 (API level 14), but apps may still run into this limit on Android versions prior to Android 5.0 (API level 21)

    ART has built in support for multi-dex apks, so multi dexing should not cause any problems in lollipop and above. You may see issues on some devices running kitkat and below, although this should be rare unless you have a very high method count or memory requirements.

    1. And that because of second-dex runtime loading, this take time.

    Yes, multidex does slow down the very first start-up time of your app significantly. (upto 200% in case of yelp, when they went 20k methods above the limit) Even cold-start times increase.

    Hence, if you can avoid multi-dexing, it is strongly recommended that you do so.

    Even if you go above the limit, you should still try to minimize the method count as more and more methods slow down the app startup time on pre-lollipop devices.

    In your case, if your build succeeded but if you're seeing run-time crashes (especially such as "No Class def. found") then it could be that you haven't configured proguard correctly, and it may be striping away some required components.

    Timothy Meller from yelp has given a detailed talk on this issue, in which he also shares some multi-dex optimizations and the importance of proguard configurations:

    https://www.youtube.com/watch?v=skmOBriQ28E

    I'd recommend you watch this if you want a better understanding of multi-dexing on android