Salar Rastari Salar Rastari - 4 months ago 20
Android Question

How to fix DEX 64K Methods Limit in minSdkVersion 21?

I got this error while creating APK file:


Error:The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html


I referenced to this tutorial to fix this issue but I still get this error.

I used MultiDex in my project like this:

in biuld.gradle for sdk 16:

apply plugin: 'com.android.application'

repositories {
jcenter()
mavenCentral()
}

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.saly.test"

minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"

multiDexEnabled true

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
productFlavors {
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':volley')
compile 'com.android.support:multidex:1.0.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:support-v4:24.0.0'
compile 'com.google.android.gms:play-services:9.2.1'
compile 'com.google.android.gms:play-services-location:9.2.1'
compile 'com.google.android.gms:play-services-gcm:9.2.1'
compile 'com.google.android.gms:play-services-maps:9.2.1'
compile 'com.onesignal:OneSignal:2.+@aar'
}


in Manifest for sdk 16:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>

<application
...
android:name="android.support.multidex.MultiDexApplication">

...

</application>
</manifest>


now I get the new error:


Error:UNEXPECTED TOP-LEVEL ERROR: Error:java.lang.OutOfMemoryError: GC
overhead limit exceeded Error: at
com.android.dx.cf.cst.ConstantPoolParser.parse0(ConstantPoolParser.java:289)
Error: at
com.android.dx.cf.cst.ConstantPoolParser.parse(ConstantPoolParser.java:150)
Error: at
com.android.dx.cf.cst.ConstantPoolParser.parseIfNecessary(ConstantPoolParser.java:124)
Error: at
com.android.dx.cf.cst.ConstantPoolParser.getPool(ConstantPoolParser.java:115)
Error: at
com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:491)
Error: at
com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
Error: at
com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
Error: at
com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
Error: at com.android.dx.command.dexer.Main.parseClass(Main.java:772)
Error: at com.android.dx.command.dexer.Main.access$1500(Main.java:85)
Error: at
com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1700)
Error: at
com.android.dx.command.dexer.Main.processClass(Main.java:755)
Error: at
com.android.dx.command.dexer.Main.processFileBytes(Main.java:723)
Error: at com.android.dx.command.dexer.Main.access$1200(Main.java:85)
Error: at
com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1653)
Error: at
com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
Error: at
com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
Error: at
com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
Error: at com.android.dx.command.dexer.Main.processOne(Main.java:677)
Error: at
com.android.dx.command.dexer.Main.processAllFiles(Main.java:569)
Error: at com.android.dx.command.dexer.Main.runMultiDex(Main.java:366)
Error: at com.android.dx.command.dexer.Main.run(Main.java:275)
Error: at com.android.dx.command.dexer.Main.main(Main.java:245)
Error: at com.android.dx.command.Main.main(Main.java:106)
:app:transformClassesWithDexForDebug FAILED Error:Execution failed for
task ':app:transformClassesWithDexForDebug'.
com.android.build.api.transform.TransformException:
com.android.ide.common.process.ProcessException:
java.util.concurrent.ExecutionException:
com.android.ide.common.process.ProcessException:
org.gradle.process.internal.ExecException: Process 'command
'C:\Program Files\Java\jdk1.8.0_66\bin\java.exe'' finished with
non-zero exit value 3


then I changed my minimum sdk to 21 for fixing this error but it's not working.

this is my build.gradle for sdk 21:

apply plugin: 'com.android.application'

repositories {
jcenter()
mavenCentral()
}

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.saly.test"

minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
productFlavors {
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':volley')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:support-v4:24.0.0'
compile 'com.google.android.gms:play-services:9.2.1'
compile 'com.google.android.gms:play-services-location:9.2.1'
compile 'com.google.android.gms:play-services-gcm:9.2.1'
compile 'com.google.android.gms:play-services-maps:9.2.1'
}


How can I fix this error?

thanks in advance.

Answer

Just add these lines

dexOptions {
    javaMaxHeapSize "4g"
}

in your gradle script anywhere inside

android { 
....

dexOptions {
    javaMaxHeapSize "4g"
}

....
}
Comments