How to put specific classes into main DEX file?
Asked Answered
F

4

14

We found an issue on Amazon market that IAP doesn't work if it's receivers located not in main DEX file. The question is how to force gradle to put specific classes (receivers) into main DEX file.

Here are the gradle DEX settings:

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')
Filipino answered 6/5, 2015 at 15:37 Comment(0)
D
17

With Android Plugin for Gradle, Revision 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 file with single class per line which needs to be explicitly added to the main dex

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

You can also use multiDexKeepProguard to keep whole package

-keep class com.example.** { *; }
Disqualify answered 2/10, 2016 at 20:25 Comment(3)
Hi, this solution is not working for me, can you check my question : #46601492Frustration
The multiDexKeepProguard part is what worked for me.Scum
What can I do, if I want use multiDexKeepProguard, and exclude some classes from maindex?Chet
S
1

I had the same problem. The main point was that you have to set "minSdkVersion 16" before "multiDexEnabled true" else your Application class could be placed in the second dex and app will crash on android lower 5.0

Sells answered 5/12, 2018 at 4:38 Comment(1)
could you link to official documentation?Consummate
M
0

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

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

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

The plugin is smart enough to package all the components (receivers among them) in main dex file.

Maxia answered 6/6, 2015 at 9:23 Comment(0)
M
0

Sergii Pechenizkyi's answar only keep some class in main dex , but not generate two dex. Add --minimal-main-dex to your builg.gradle. But this only solve 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'
    }
}
Mckeever answered 14/3, 2017 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.