时之沙 / Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚

来自CSDN博客:Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚 http://blog.csdn.net/t12x3456/article/details/40837287#

  移动开发 最后更新时间 2016-11-11 14:22:11
blog_20141110_1_3667407 12行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: method ID not in [0, 0xffff]: 65536
at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:501)
at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:282)
at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:490)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:167)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
blog_20141110_2_7888312 13行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
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
}
}
blog_20141110_3_3362953 10行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
android/support/multidex/BuildConfig.class
android/support/multidex/MultiDex$V14.class
android/support/multidex/MultiDex$V19.class
android/support/multidex/MultiDex$V4.class
android/support/multidex/MultiDex.class
android/support/multidex/MultiDexApplication.class
android/support/multidex/MultiDexExtractor$1.class
android/support/multidex/MultiDexExtractor.class
android/support/multidex/ZipUtil$CentralDirectory.class
android/support/multidex/ZipUtil.class
blog_20141110_4_3646361 7行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
repositories {
jcenter()
}

dependencies {
compile 'com.google.android:multidex:0.1'
}
blog_20141110_5_6994430 7行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
public class MyApplication extends FooApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
blog_20141110_6_5915418 37行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.3'
classpath 'jp.leafytree.gradle:gradle-android-scala-plugin:1.1'
}
}

apply plugin: 'com.android.application'
apply plugin: 'jp.leafytree.android-scala'

repositories {
jcenter()
}

android {
compileSdkVersion 21
buildToolsVersion '21.0.2'

defaultConfig {
applicationId 'some.app'
minSdkVersion 19
targetSdkVersion 19
versionCode 1
versionName '1.0'
}
}

dependencies {
compile 'com.google.android:multidex:0.1'
compile 'com.android.support:support-v4:19.0.1'
compile 'com.google.android.gms:play-services:5.0.77'
compile 'org.scala-lang:scala-library:2.11.2'
compile 'org.scaloid:scaloid_2.11:3.4-10'
}
blog_20141110_7_135541 14行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
@Override
public void onCreate() {
super.onCreate();

final Context mContext = this;
new Runnable() {

@Override
public void run() {
// put your logic here!
// use the mContext instead of this here
}
}.run();
}
blog_20141110_8_9895560 13行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
$ANDROID_SDK/build-tools/android-4.4W/dx --dex --num-threads=4 --multi-dex
...
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Library dex files are not supported in multi-dex mode
at com.android.dx.command.dexer.Main.runMultiDex(Main.java:322)
at com.android.dx.command.dexer.Main.run(Main.java:228)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
blog_20141110_9_1291783 6行 Java
Raw
 1
 2
 3
 4
 5
 6
android {
// ...
dexOptions {
preDexLibraries = false
}
}
blog_20141110_10_3560058 2行 Java
Raw
 1
 2
UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: Java heap space
blog_20141110_11_3527821 7行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
android {
// ...
dexOptions {
javaMaxHeapSize "2g"
}
}
blog_20141126_12_5703574 1行 Java
Raw
 1
com.android.dex.DexException: Multiple dex files define L{package}/BuildConfig;
blog_20141126_13_8179592 4行 Java
Raw
 1
 2
 3
 4
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':lib-project-module')
}
blog_20150527_6_5714988 19行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
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'
}
blog_20160606_4_5382036 7行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
android {
defaultConfig {
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies { compile 'com.google.android:multidex:0.1'}
blog_20161111_4_7371066 7行 Java
Raw
 1
 2
 3
 4
 5
 6
 7
android {
defaultConfig {
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies { compile 'com.google.android:multidex:0.1'}