Search code examples
androidgradleandroid-multidex

gradle - Android Studio build too slow multidex application


When I add to my project the multidex:true, and make an Application class that extends from the MultiDexApplication, my project build time passed from 20 sec to around 90 sec.How to do some faster?


Solution

  • If you are like me who already tried Vic Vu's solution but still can't avoid enabling multiDex then you can try this (as long as your are using a device that has Android 5.0 and above).

    Note This will only speed up your development build. Your production build will still be slow.

    Basically you need to introduce 2 product flavors one for dev and one for prod.

    Add multiDexEnabled true

    android {
        productFlavors {
            // Define separate dev and prod product flavors.
            dev {
                // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
                // to pre-dex each module and produce an APK that can be tested on
                // Android Lollipop without time consuming dex merging processes.
                minSdkVersion 21
            }
            prod {
                // The actual minSdkVersion for the application.
                minSdkVersion 14
            }
        }
              ...
        buildTypes {
            release {
                runProguard true
                proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                     'proguard-rules.pro'
            }
            defaultConfig {
                applicationId "com.something.something"
                targetSdkVersion 23
                versionCode 1
                versionName "1.0.0"
    
                multiDexEnabled true
            }
        }
    dependencies {
      compile 'com.android.support:multidex:1.0.1'
    }
    

    And I have a class which extends Application so I had to override attachBaseContext()

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    

    If you are not extending Application simply use MultiDexApplication in your AndroidManifest.xml application tag.

    Ensure that in your Android Studio Build Variants you are pointing to devDebug.

    Read the complete instructions here https://developer.android.com/studio/build/multidex.html#dev-build