Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to build apk: The number of method references cannot exceed 64K

I have been trying to build the apk file for my app, however, I am getting the error: The number of method references cannot exceed 64K.

Here are the errors,

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

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.7.0_15\bin\java.exe'' finished with non-zero exit value 2

This is my gradle file,

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "nikhilraghavendra.hopper"
    minSdkVersion 21
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-identity:8.4.0'
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.android.support:cardview-v7:23.2.1'
compile 'com.google.android.gms:play-services:8.4.0'
}

I want to build the apk file and deploy it without any issues, how do I do it?

Update

I also tried the following

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

dexOptions {
    maxProcessCount = 4 // this is the default value
}

dataBinding{
    enabled = true
}

defaultConfig {
    applicationId "nikhilraghavendra.hopper"
    minSdkVersion 21
    targetSdkVersion 23
    resConfigs "en", "fr"
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }
    debug {
        minifyEnabled true
        useProguard false
    }
}
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-identity:8.4.0'
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.android.support:cardview-v7:23.2.1'
compile 'com.google.android.gms:play-services:8.4.0'
}

This is producing the message:

Error:Execution failed for task ':app:transformClassesWithNewClassShrinkerForDebug'.

Warnings found during shrinking, please use -dontwarn or -ignorewarnings to suppress them.

How do I deal with this and build a proper apk? Please Help.

like image 655
Nikhil Raghavendra Avatar asked Apr 15 '16 05:04

Nikhil Raghavendra


3 Answers

Unable to build apk: The number of method references cannot exceed 64K occur when app .dex file have more than 65,536 methods.

Exceed 65K Method that means method count more than 65,536 methods There are different ways to fix it:

1. Reduce dependencies in your project put dependencies in gradle which are required

Most problems that I found from another developers is use Google Play Services in them project like this.

 compile 'com.google.android.gms:play‐services:8.4.0'

So the best way is choosing only some dependencies in Google Play Services that you really want to use it.

compile 'com.google.android.gms:play‐services‐location:8.4.0'
compile 'com.google.android.gms:play‐services‐maps:8.4.0'
compile 'com.google.android.gms:play‐services‐ads:8.4.0'

2. Set minimum SDK version to 21 or higher

Why it worked when change minimum SDK to 21? That's because of Android 5.0 or higher are use ART (Android Runtime) that supported MultiDex instead of Dalvik. So ART can support more than 65,536 methods.

3. Use Proguard to reduce useless method

4. Use MultiDex

But these are the Limitation of MultiDex library

• May occurs ANR while app launching if .dex files too large.

• Should be define minimum SDK to version 14 or higher.

• MulitDex uses more memory and may crash while app running if allocation memory is over limit.

• Take more build time when you build the project

like image 44
Devraj Singh Avatar answered Nov 03 '22 16:11

Devraj Singh


android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"

    defaultConfig {
        applicationId "com.try.app"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

here multiDexEnabled true should do the game for you

UPDATE : To support latest Android version
1. If your minSdkVersion is set to 21 or higher, all you need to do is set multiDexEnabled to true in your module-level build.gradle file, as shown above.
2. However, if your minSdkVersion is set to 20 or lower, then you must use the multidex support library along with above changes as follows:

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

Apart from the above addition of support library, you need to make changes to your Application class as mentioned in this Link.

BEST PRACTICE:
1. Remove any unused code with proguard.
2. Avoid adding unnecessary dependencies in your project.
3. If limited number methods or classes of any open source library are needed, then its advisable to clone only those in your project as it not only gives you total control on them but also allows proguard to act on them and you don't have any unused methods in your code.

Source : Android Developers - Configure Apps with 64K Method count

like image 128
Mohammed Atif Avatar answered Nov 03 '22 15:11

Mohammed Atif


Put this inside your defaultConfig:

multiDexEnabled true
like image 32
Nongthonbam Tonthoi Avatar answered Nov 03 '22 15:11

Nongthonbam Tonthoi