gradle - Android Studio构建太慢的multidex应用程序

Але*_*чук 24 android gradle android-multidex

当我向我的项目添加multidex:true,并创建一个从MultiDexApplication扩展的Application类时,我的项目构建时间从20秒传递到大约90秒.如何更快地做一些?

Mar*_*zon 26

如果你像我一样已经尝试了Vic Vu的解决方案,但仍然无法避免启用multiDex,那么你可以试试这个(只要你使用的是Android 5.0及更高版本的设备).

注意这只会加快您的开发构建.您的生产构建仍然会很慢.

基本上你需要引入2种产品口味dev,一种用于prod.

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'
}
Run Code Online (Sandbox Code Playgroud)

我有一个扩展的类,Application所以我不得不重写attachBaseContext()

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}
Run Code Online (Sandbox Code Playgroud)

如果您没有扩展,Application只需MultiDexApplicationAndroidManifest.xml application标签中使用即可.

确保在您Build Variants指向的Android Studio 中devDebug.

阅读完整说明https://developer.android.com/studio/build/multidex.html#dev-build


Vic*_*cVu 25

作为答案提供,因为这更适合格式化.

简单地回答你的问题:不,没有办法.Multidex是一个旨在帮助解除65k方法限制负担的过程.这个过程很复杂,只会让你的构建时间更长.

你能做的最好的事情就是降低你的方法数.

在您的build.gradle(此处提供)中,您正在使用:

`compile 'com.google.android.gms:play-services:8.3.0'`
Run Code Online (Sandbox Code Playgroud)

但是如果你看看最近的游戏服务api,你可以选择你真正需要的服务.

请查看此页面上的表1 .

只使用你需要的.Google Play服务整体上大约有30k种方法.

这应该有所帮助.

  • 谢谢,你让我免于死于无聊.只添加钱包和登录服务在30 k方法下保持不变. (3认同)