gradle - Android Studio build too slow multidex application

点点圈 提交于 2019-11-27 02:33:58

问题


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?


回答1:


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




回答2:


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.




回答3:


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"
}



回答4:


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.



来源:https://stackoverflow.com/questions/30177495/gradle-android-studio-build-too-slow-multidex-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!