Speed up gradle build in multidex application

2020-05-12 04:59发布

My application has a bunch of librarys that are essential that is why I was forced to use multidex support library and it works nicely. But where the problem shows is in the gradle buid speed. It takes on average 2minutes to build and when I am developing and testing this is quite annoying.

Is there a way to speed up my debug builds?

7条回答
闹够了就滚
2楼-- · 2020-05-12 05:34

Go in setting , search compiler , type "--offline" in Command line options and than compile.

查看更多
你好瞎i
3楼-- · 2020-05-12 05:40

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 smaller if you wish):

dexOptions {
    javaMaxHeapSize "4g"
}
查看更多
手持菜刀,她持情操
4楼-- · 2020-05-12 05:45

Changing MinSdk to 21 made everything back to normal for me.Now everything compiles in like 6s

查看更多
Ridiculous、
5楼-- · 2020-05-12 05:47

Android Studio 1.3 (currently in Preview 3) is using a new build system which improved gradle build time (really, like 10-30x faster).

More information in the Live Session at Google I/O 2015

查看更多
够拽才男人
6楼-- · 2020-05-12 05:48

This is no longer needed with the latest Android Studio 3.0

查看更多
SAY GOODBYE
7楼-- · 2020-05-12 05:52

You can speed-up your development builds by specifying the minimum SDK version = 21.
Official documentation includes a whole section about that.

Example (from documentation):

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'
        }
    }
}
dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

Once you added the product flavors, you can use the devDebug task (instead of default debug task) for your development builds:
- from command line: run ./gradlew installDevDebug
- from Android Studio: open Build Variants window and select the devDebug build variant.

You should, of course, work against a device whose SDK >= 21.


There's also a solution for those who don't want to use flavors. As suggested in this gist, dynamically calculate the minSdkVersion value:

int minSdk = hasProperty('devMinSdk') ? devMinSdk.toInteger() : 14
apply plugin: 'com.android.application'

android {
    ...
    defaultConfig {
        minSdkVersion minSdk
        ...
    }
}

In this example, we're checking if devMinSdk property defined, and if true - we're using it. Otherwise, we default to 14.

How do we pass devMinSdk value to build script? Two options:

Using command line:

./gradlew installDebug -PdevMinSdk=21

Using Android Studio preferences:

Go to Preferences (Settings on Windows) -> Build, Execution, Deployment -> Compiler -> put -PdevMinSdk=21 in Command-line Options text box.

Android Studio compiler options

查看更多
登录 后发表回答