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?

Upvotes: 25

Views: 19631

Answers (4)

Mark Pazon
Mark Pazon

Reputation: 6205

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

Upvotes: 27

Vic Vuci
Vic Vuci

Reputation: 7051

Supplying as an answer because this is better fit with the formatting.

To simply answer your question: No, there is no way. Multidex is a process meant to help lift the burden of the 65k method limit. This process is complicated and will simply make your build times longer.

The best you can can do is lower your method count.

In your build.gradle (supplied here) you're using:

`compile 'com.google.android.gms:play-services:8.3.0'`

But if you look at the most recent play services api you can pick and choose what services you actually need.

Look at Table 1 on this page.

Only use the ones you need. Google play services as a whole is somewhere around 30k methods.

That should help.

Upvotes: 25

Will Calderwood
Will Calderwood

Reputation: 4636

Multidexing uses more memory. As you get closer to your max heap size in Java you'll find Java spends more time doing GC than it does doing any real work, this can slow things down a lot.

I'd strongly recommend increasing the max heap size when using multidex. Add the following to the android closure in your build.gradle file to make the max heap size 4GB (Make it larger/smaller if you wish):

dexOptions {
    javaMaxHeapSize "4g"
}

Upvotes: 2

Alex Lipov
Alex Lipov

Reputation: 13958

It depends.

You haven't specified it in your question, but if you just want to speed-up your development builds - then you can avoid the extra work. Official documentation includes a whole section about that.

Upvotes: 1

Related Questions