本文介绍了在 multidex 应用程序中加速 gradle 构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一堆必不可少的库,这就是为什么我被迫使用 multidex 支持库并且它运行良好.但是问题出在gradle构建速度上.构建平均需要 2 分钟,当我开发和测试时,这很烦人.

有没有办法加快我的调试版本?

解决方案

您可以通过指定最低 SDK 版本 = 21 来加速您的开发构建.
官方文档包括关于此的

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?

解决方案

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.

这篇关于在 multidex 应用程序中加速 gradle 构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:52