How Can I Use The `--multi-dex` Option?
Solution 1:
If you are using Gradle/Android Studio, you can add this to your gradle configuration:
android {
....
dexOptions {
preDexLibraries = false
}
}
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = []
}
dx.additionalParameters += '--multi-dex'// this is optional// dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()
}
}
Then you need to add the multidex support library jar, located in sdk/extras/android/support/multidex/library/libs
. And install it either by extending your application from MultiDexApplication
, or calling Multidex.install()
from your application's attachBaseContext
method.
For more details look at this blogpost: http://blog.osom.info/2014/10/multi-dex-to-rescue-from-infamous-65536.html
UPDATE:
Here https://developer.android.com/tools/building/multidex.html you can find the official way to use multidex with Gradle.
Basically you need to change your gradle file like this:
android {
compileSdkVersion 21
buildToolsVersion "21.1.0"
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
And set your Application class to android.support.multidex.MultiDexApplication or if you already have an Application class you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex.
Solution 2:
Update: You can now find detailed instructions how to configure multi dex at the following page: https://developer.android.com/tools/building/multidex.html
Simple answer is that the currently the Android Studio tools do not allow you to specify the options for generating multiple dex files (--multi-dex). So you would need to script your build process by hand ... a real pain.
There are a couple of Android bugs which look relevant: 63936 and 20814
Solution 3:
From the javadoc documentation:
usage:
dx --dex [--debug] [--verbose] [--positions=<style>] [--no-locals]
[--no-optimize] [--statistics] [--[no-]optimize-list=<file>] [--no-strict]
[--keep-classes] [--output=<file>] [--dump-to=<file>] [--dump-width=<n>]
[--dump-method=<name>[*]] [--verbose-dump] [--no-files] [--core-library]
[--num-threads=<n>] [--incremental] [--force-jumbo]
[--multi-dex [--main-dex-list=<file> [--minimal-main-dex]]
[<file>.class | <file>.{zip,jar,apk} | <directory>] ...
Convert a set of classfiles into a dex file, optionally embedded in a
jar/zip. Output name must end with one of: .dex .jar .zip .apk or be a directory.
Positions options: none, important, lines.\n
--multi-dex: allows to generate several dex files if needed. This option is
exclusive with --incremental, causes --num-threads to be ignored and only
supports folder or archive output.
--main-dex-list=<file>: <file> is a list of class file names, classes defined by
those class files are put in classes.dex.
--minimal-main-dex: only classes selected by --main-dex-list are to be put in
the main dex.
dx
is a tool that used on dexing as part of android build process. In the build.xml
(located at sdk.dir/tools/ant/buildxml
) which used for default android build, it is used as part of target name="-dex"
, declared within macro dex-helper
. It have outdated Anttask which still doesn't support --multi-dex.
From the dexer
source code, --main-dex-list options receive file input which have all class file that you wanted to be in main dex, separated with new line. Like this:
bin/classes/com/example/MainActivity.class
Every other classes not declared in this list will go to secondary dex.
Or you can use the old way before --multi-dex introduced: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html
Solution 4:
Right now, I edit
.../sdk/build-tools/19.0.3/dx
replacing the last line:
exec java $javaOpts -jar "$jarpath" --multi-dex "$@"
It's a hack. It's not maintainable. Probably your CI administrator will hate you...
BUT IT'S WORKING!
Solution 5:
As you know you have to modify build.xml a little bit which is located in tools if using ant build. you change output property from an archive to a directory (i.e $project/bin) and dx step will be done successfully. It may be so, but ant-tasks.jar and sdklib.jar are required to modify to complete 'apkbuilder' step.
- ApkBuilder, ApkBuilderTask classes
Post a Comment for "How Can I Use The `--multi-dex` Option?"