The Number of Method References in a .Dex File Cannot Exceed 64K API 17

The number of method references in a .dex file cannot exceed 64k API 17

You have too many methods. There can only be 65536 methods for dex.

As suggested you can use the multidex support.

Just add these lines in the module/build.gradle:

android {

defaultConfig {
...

// Enabling multidex support.
multiDexEnabled true
}
...
}

dependencies {
implementation 'androidx.multidex:multidex:2.0.1' //with androidx libraries
//implementation 'com.android.support:multidex:1.0.3' //with support libraries

}

Also in your Manifest add the MultiDexApplication class from the multidex support library to the application element

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="androidx.multidex.MultiDexApplication">

<!-- If you are using support libraries use android:name="android.support.multidex.MultiDexApplication" -->

<!--If you are using your own custom Application class then extend -->
<!--MultiDexApplication and change above line as-->
<!--android:name=".YourCustomApplicationClass"> -->

...
</application>
</manifest>

If you are using your own Application class, change the parent class from Application to MultiDexApplication.

If you can't do it, in your Application class override the attachBaseContext method with:

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
MultiDex.install(this);
}

Another solution is to try to remove unused code with ProGuard - Configure the ProGuard settings for your app to run ProGuard and ensure you have shrinking enabled for release builds.

The number of method references in a .dex file exceed 64K

You need to enable multidex in the android default config then:

android {
compileSdkVersion 23
buildToolsVersion '23.0.3'

defaultConfig {
applicationId "com.example.case"
minSdkVersion 16
targetSdkVersion 23
versionCode 43
versionName "4.0.13"

// Enabling multidex support.
multiDexEnabled true
}

When you are building you application in a daily routine, you normally use the debug default flavor. So if you application has more than 65k method, you need ot enable it on all flavor.

As a side note, you may want to use Proguard on the debug build so you won't have to enable multiDex on it.

Full app/build.gradle (documentation)

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.1'
}

Last part: add the MultiDex application in the manifest (or as a parent of your own Application

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>

How fix error :the number of method references in a .dex file cannot exceed 64K?

Try this..

  defaultConfig {
applicationId "com.pokemongo.pokemon"
minSdkVersion 16
targetSdkVersion 24

multiDexEnabled true

}

dexOptions should add..

dexOptions {
//incremental = true;
preDexLibraries = false
javaMaxHeapSize "4g"
}

Also In Your AndroidManifest.xml add this lines android:name

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="android.support.multidex.MultiDexApplication"
>

In your gradle..

  apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion '24.0.0'
defaultConfig {
applicationId "com.pokemongo.pokemon"
minSdkVersion 16
targetSdkVersion 24
multiDexEnabled true
}

dexOptions {
//incremental = true;
preDexLibraries = false
javaMaxHeapSize "4g"
}
buildTypes {
release {

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')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.google.android.gms:play-services:9.4.0'
compile 'com.android.support:multidex:1.0.1'
}

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


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

number of referenced method in .dex file cannot exceed 64K and java.lang.UnsupportedOperationException

a). Why do I need to add Multidex Support Library?

-> The purpose of this is to split up your Dex file into multiple Dex files.This library provides support for building apps with multiple Dalvik Executable (DEX) files. Apps that reference more than 65536 methods are required to use Multidex configurations.

b). What's the future purpose?

-> Android has a problem whereby there is an upper limit on the number of method definitions in a Dex file (64k). This means that once that limit is reached, you cannot expand your application any further.

Before Multidex, it was advised to use ProGuard to reduce the number of method definitions by removing methods that aren't used in the code. Especially useful when implementing the Google Play Services Framework.

Multidex was then introduced and allows Apps to continue to expand without worrying about method count limits. It does carry the danger of making the App more unstable. Therefore it is advised to try ProGuard first to reduce the method count.

c). What does the second error mean?

java.lang.UnsupportedOperationException

Ref: https://stackoverflow.com/a/21061985/3758024

Please provide the complete crash log/stack trace of this crash from your app with the relevant code snippet.

d). In what sense it say that .dex file cannot exceed 64K? Can you help me?

-> Android app (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536—including Android framework methods, library methods, and methods in your own code. In the context of computer science, the term Kilo, K, denotes 1024 (or 2^10). Because 65,536 is equal to 64 X 1024, this limit is referred to as the '64K reference limit'.

More References.

1) https://developer.android.com/studio/build/multidex.html#avoid

2) https://medium.com/@rotxed/dex-skys-the-limit-no-65k-methods-is-28e6cb40cf71#.3eg897jca

3) https://blog.mustafaali.xyz/dexs-64k-limit-is-not-a-problem-anymore-well-almost-2b1faac3508#.wiaruldue

4)http://www.fasteque.com/deep-dive-into-android-multidex/

Error:The number of method references in a .dex file cannot exceed 64K.6

You shouldn't ever be using compile 'com.google.android.gms:play-services:9.4.0' as that includes every Google Play services library - only use the APIs you need.

This should reduce your method count enough to fit under the 64K limit.



Related Topics



Leave a reply



Submit