how to use android-support-multidex.jar for phone prior to Android 5.0 in android studio?

。_饼干妹妹 提交于 2019-12-12 05:41:44

问题


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

.


回答1:


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 specified multiDexEnabled = 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.



来源:https://stackoverflow.com/questions/32753991/how-to-use-android-support-multidex-jar-for-phone-prior-to-android-5-0-in-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!