gradle - Android Studio 构建 multidex 应用程序太慢
gradle - Android Studio build too slow multidex application
当我将 multidex:true 添加到我的项目并创建一个从 MultiDexApplication 扩展的应用程序 class 时,我的项目构建时间从 20 秒增加到大约 90 sec.How 到做得更快?
作为答案提供,因为这更符合格式。
简单回答你的问题:没有,没有办法。 Multidex 是一个旨在帮助减轻 65k 方法限制负担的过程。这个过程很复杂,只会使您的构建时间更长。
您能做的最好的事情就是减少方法数。
在您的 build.gradle () 中,您正在使用:
`compile 'com.google.android.gms:play-services:8.3.0'`
但如果您查看最新的播放服务 api,您可以挑选您实际需要的服务。
看Table1on this page.
只使用你需要的。 Google 播放服务作为一个整体大约有 3 万个方法。
这应该有所帮助。
视情况而定。
你没有在你的问题中指定它,但如果你只是想加快你的 开发 构建 - 那么你可以避免额外的工作。官方文档包含关于此的 whole section。
多索引使用更多内存。当您接近 Java 中的最大堆大小时,您会发现 Java 花在 GC 上的时间比花在任何实际工作上的时间都多,这会大大降低速度。
我强烈建议在使用 multidex 时增加最大堆大小。将以下内容添加到 build.gradle 文件中的 android 闭包中,使最大堆大小为 4GB(如果需要,可以设为 larger/smaller):
dexOptions {
javaMaxHeapSize "4g"
}
如果你像我一样已经尝试过 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'
}
我有一个 class 扩展 Application
所以我不得不覆盖 attachBaseContext()
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
如果您不扩展 Application
,只需在 AndroidManifest.xml
application
标签中使用 MultiDexApplication
。
确保在您的 Android Studio Build Variants
中指向 devDebug
。
在此处阅读完整说明https://developer.android.com/studio/build/multidex.html#dev-build
当我将 multidex:true 添加到我的项目并创建一个从 MultiDexApplication 扩展的应用程序 class 时,我的项目构建时间从 20 秒增加到大约 90 sec.How 到做得更快?
作为答案提供,因为这更符合格式。
简单回答你的问题:没有,没有办法。 Multidex 是一个旨在帮助减轻 65k 方法限制负担的过程。这个过程很复杂,只会使您的构建时间更长。
您能做的最好的事情就是减少方法数。
在您的 build.gradle (
`compile 'com.google.android.gms:play-services:8.3.0'`
但如果您查看最新的播放服务 api,您可以挑选您实际需要的服务。
看Table1on this page.
只使用你需要的。 Google 播放服务作为一个整体大约有 3 万个方法。
这应该有所帮助。
视情况而定。
你没有在你的问题中指定它,但如果你只是想加快你的 开发 构建 - 那么你可以避免额外的工作。官方文档包含关于此的 whole section。
多索引使用更多内存。当您接近 Java 中的最大堆大小时,您会发现 Java 花在 GC 上的时间比花在任何实际工作上的时间都多,这会大大降低速度。
我强烈建议在使用 multidex 时增加最大堆大小。将以下内容添加到 build.gradle 文件中的 android 闭包中,使最大堆大小为 4GB(如果需要,可以设为 larger/smaller):
dexOptions {
javaMaxHeapSize "4g"
}
如果你像我一样已经尝试过 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'
}
我有一个 class 扩展 Application
所以我不得不覆盖 attachBaseContext()
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
如果您不扩展 Application
,只需在 AndroidManifest.xml
application
标签中使用 MultiDexApplication
。
确保在您的 Android Studio Build Variants
中指向 devDebug
。
在此处阅读完整说明https://developer.android.com/studio/build/multidex.html#dev-build