Using Gradle to split external libraries in separated dex files to solve Android Dalvik 64k methods limit

后端 未结 4 1763
离开以前
离开以前 2020-12-04 20:13

Is there a proper/easy way to solve the 64k methods limit using Gradle?

I mean some custom Gradle task to use pre-dexed jars to create separated dex fil

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 20:36

    Update for Android Gradle plugin 2.2.0: It is not possible to access the dex task anymore, but in exchange additionalParameters was introduced as part of dexOptions. Use it like

    android {
      dexOptions {
        additionalParameters += '--minimal-main-dex'
        // additionalParameters += '--main-dex-list=$projectDir/'.toString()
        // additionalParameters += '--set-max-idx-number=55000'
      }
    }
    

    Update for Android Gradle plugin 0.14.0: There is now direct multi-dex support via the new multiDexEnabled true directive (requires build-tools 21.1.0, support repository revision 8, and Android Studio 0.9).

    Original answer: Ever since Gradle Android plugin 0.9.0 you actually can pass the --multi-dex to dx by adding this to you app's build.gradle file:

    afterEvaluate {
        tasks.matching {
            it.name.startsWith('dex')
        }.each { dx ->
            if (dx.additionalParameters == null) {
                dx.additionalParameters = ['--multi-dex']
            } else {
                dx.additionalParameters += '--multi-dex'
            }
    
            // Add more additional parameters like this:
            dx.additionalParameters += '--main-dex-list=class-list.txt'
            dx.additionalParameters += '--minimal-main-dex'
        }
    }
    

    So far for the creating he multiple dex files. To actually use the multiple dex files, take a look at https://github.com/casidiablo/multidex (which is a fork of Google's upcoming MultiDex support library).

提交回复
热议问题