I want to build my app with more classes.dex file in one apk. So I follow instruction of google help page. It is ok for android 5.0, But i cannot build for phone prior to Android 5.0. it always report:com.android.dex.DexException: Too many classes in --main-dex-list, main dex capacity exceeded. I don't know hot to build. My build gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
//buildToolsVersion "20"
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.android.ccbtest.first"
minSdkVersion 21
targetSdkVersion 21
multiDexEnabled true
}
dexOptions {
preDexLibraries = false
}
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = []
}
dx.additionalParameters += '--multi-dex' // enable multidex
// optional
// dx.additionalParameters += "--main-dex list=$projectDir/<filename>".toString() // enable the main-dex-list
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile files('libs/ccbtest.jar')
compile files('libs/ccbtest1.jar')
compile files('libs/ccbtest2.jar')
compile files('libs/ccbtest3.jar')
compile files('libs/ccbtest4.jar')
compile files('libs/ccbtest5.jar')
compile files('libs/ccbtest6.jar')
// Multi-dex Lib
compile 'com.android.support:multidex:1.0.0'
}
if i modify minSdkVersion 21 from 21 to 20. It will fail.
I want to in phone prior to android 5.0. Please help me about this.
thanks.
br
mwt
.
First of all, as oberflansch commented, remove the closure that you're passing to
afterEvaluate
. The Android gradle plugin will handle all the dx configuration automatically once you've specifiedmultiDexEnabled = true
.The
Too many classes in --main-dex-list, main dex capacity exceeded
error most probably says that your main dex file exceeded the 65536 methods limit.I have encountered the same issue in the past, and eventually had to patch the Android gradle plugin to not include Activity classes in main dex. It's a bit hacky, but works well. See my blogpost for further explanation.