Android gives error Cannot fit requested classes in a single dex file
In root build.gradle
file do something like:
dependencies {
// ...
implementation 'androidx.multidex:multidex:2.0.1'
}
android {
defaultConfig {
// ...
multiDexEnabled true
}
}
More details here: Error:Cannot fit requested classes in a single dex file.Try supplying a main-dex list. # methods: 72477 > 65536
D8: Cannot fit requested classes in a single dex file
Add multiDex to your app-level build.gradle file.
Under defaultConfig add this line
multiDexEnabled true
Then in dependencies add multidex and check versions
implementation 'androidx.multidex:multidex: 2.0.1'
Error : Cannot fit requested classes in a single dex file (# methods: 65978 65536)
You must add the library in the app gradle :
implementation 'com.android.support:multidex:1.0.3'
After, add in the defaultConfig of the app gradle :
multiDexEnabled true
Cannot fit requested classes in a single dex file after updating android studio
If you want to stay away from multiDex
you have two options. Actually one option, the other one is just crippled form of the main option. By the way, I just copy-pasted all your gradle
file and tested it.
Option 1. (Recommended)
Step 1. Create a proguard
file for your debug. Let's say debug-proguard-rules.pro
and add the following line to your debug proguard file. This line will force the compiler to keep classes and class memebers' names as-is during debug build which is quite helpful:
-dontobfuscate
Step 2. Add debug
build type to your gradle. This will apply proguard
rules from both proguard-rules.pro
and proguard-rules-debug.pro
files.
buildTypes {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro', 'proguard-rules-debug.pro'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
Option 2.
If you don't care about your classes and class members being obfuscated in debug
build, then just add the following line to your build types
:
buildTypes {
debug {
minifyEnabled true
}
release {
//........
}
}
Related Topics
Separate Back Stack For Each Tab in Android Using Fragments
How to Get the Absolute Coordinates of a View
Android: Storing Username and Password
How to Make Ellipsize="Marquee" Always Scroll
How to Disable Bottomnavigationview Shift Mode
How to Make Wrap_Content Work on a Recyclerview
How to Refresh Mediastore on Android
Listen to Volume Buttons in Background Service
Android: Broadcast Receiver for Screen on and Screen Off
Android - Android.View.Inflateexception: Binary Xml File Line #8: Error Inflating Class Fragment
How to Send String from One Activity to Another
Failed to Resolve: Com.Google.Firebase:Firebase-Core:11.2.0
Android Fragments and Animation
How to Simulate a Touch Event in Android
Difference Between Oncreate() and Onstart()