How to put certain classes in the main DEX file?

We found a problem on the Amazon market that IAP does not work if the receivers are not located in the main DEX file. The question is how to get gradle put certain classes (receivers) in the main DEX file.

EDIT: updated with gradle DEX options

 afterEvaluate { tasks.matching { it.name.startsWith('dex') }.each { dx -> if (dx.additionalParameters == null) { dx.additionalParameters = [] } dx.additionalParameters += '--multi-dex' dx.additionalParameters += "--main-dex-list=class_files.txt" } } dexOptions { javaMaxHeapSize "4g" preDexLibraries = false } compile('com.android.support:multidex:1.0.0') 
+8
source share
4 answers

With the Android plugin for Gradle, version 2.2.0 (released in September 2016) you can use multiDexKeepFile api

 android { buildTypes { debug { ... multiDexEnabled true multiDexKeepFile file('multidex_keep_file.txt') } } } 

Where multidex_keep_file.txt is a file with one class per line, which should be explicitly added to the main dex file

  com/example/MyClass.class com/example/MyClass2.class 

You can also use multiDexKeepProguard to store the entire package.

 -keep class com.example.** { *; } 
+12
source

No need to manually add multi-dex parameters to dex tasks.
This can be automatically handled by the Android plugin (starting with v0.14.0).

Remove the afterEvaluate section and compile ('com.android.support:multidex:1.0.0') from your build.gradle and add the following instead:

 android { defaultConfig { ... multiDexEnabled = true } } 

The plugin is smart enough to pack all components (recipients among them) into the main dex file.

0
source

Sergii Pechenizkyi answar only holds some class in the main dex, but does not generate two dex. Add --minimal-main-dex to your builg.gradle . But this is only solved below gradle1.5.0 . You can use DexKnifePlugin to solve your problem.

 afterEvaluate { tasks.matching { it.name.startsWith('dex') }.each { dx -> if (dx.additionalParameters == null) { dx.additionalParameters = [] } dx.additionalParameters += '--multi-dex' dx.additionalParameters += "--main-dex-list=class_files.txt" dx.additionalParameters += '--minimal-main-dex' } } 
0
source

I have the same problem. The main thing was that you need to set "minSdkVersion 16" before "multiDexEnabled true", otherwise your application class can be placed in the second dex, and the application will fail on android below 5.0

0
source

Source: https://habr.com/ru/post/986677/


All Articles