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.
A Dex file contains code which is ultimately executed by the Android Runtime. Every APK has a single classes. dex file, which references any classes or methods used within an app.
d8 is a command line tool that Android Studio and the Android Gradle Plugin use to compile your project's Java bytecode into DEX bytecode that runs on Android devices, and it allows you to use Java 8 language features in your app's code. d8 is also included as a standalone tool in Android Build Tools 28.0.
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.
In android/app/build.gradle
android {
compileSdkVersion 23
buildToolsVersion '23.0.0'
defaultConfig {
applicationId "com.dkm.example"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
multiDexEnabled true
it works for me
android {
compileSdkVersion 23
buildToolsVersion '23.0.0'
defaultConfig {
applicationId "com.dkm.example"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
it worked for me.
I got this error message because while coding my project auto update compile version in my build.gradle
file :
android {
...
buildToolsVersion "23.0.2"
...
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0' }
Solve it by correcting the version:
android {
...
buildToolsVersion "23.0.2"
...
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
This is what worked for me:
This happens because there are too many unused methods. Most of these methods are coming from included libraries in your build.gradle
Use minify and shrink resources to fix this with gradle and clean up your code at the same time.
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true
shrinkResources true
}
}
This error can also occur when you load all google play services apis when you only using afew.
As stated by google:"In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.
From version 6.5, you can instead selectively compile Google Play service APIs into your app."
For example when your app needs play-services-maps ,play-services-location .You need to add only the two apis in your build.gradle file at app level as show below:
compile 'com.google.android.gms:play-services-maps:10.2.1'
compile 'com.google.android.gms:play-services-location:10.2.1'
Instead of:
compile 'com.google.android.gms:play-services:10.2.1'
For full documentation and list of google play services apis click here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With