Android gives error "Cannot fit requested classes in a single dex file"
Asked Answered
S

8

95

I don't know why but it's impossible to launch my app on my mobile this morning. I get this error message:

Cannot fit requested classes in a single dex file. Try supplying a main-dex list.

# methods: 68061 > 65536 Message{kind=ERROR, text=Cannot fit requested classes in a single dex file. Try supplying a main-dex list.

# methods: 68061 > 65536, sources=[Unknown source file], tool

I'm really new to Android and I don't understand the problem and what I need to do? And why I get this problem now and not before?

Subadar answered 14/7, 2018 at 17:9 Comment(0)
E
280

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

Exemplify answered 14/7, 2018 at 17:23 Comment(5)
Anyone wonder which build.gradle file please update following file android/app/build.gradleDarsie
An important detail. This error started after updating com.google.android.gms: play-services-ads from version 19.3.0 to version 19.4.0 in the "build.gradle" of my app. My app has two flavors, PRO and FREE. Since the com.google.android.gms: play-services-ads only applys to the FREE version, instead of adding "multiDexEnabled true" in the gradle "defaultConfig", you must add it in the "productFlavors" area in your "build.gradle"Polymerize
I have added it without implementation and it worked (!)Spinose
This makes me wonder what we're supposed to do if it wasn't for a site like StackOverflow? Same thing happens when I beat a game using a walkthrough, then start exploring "why didn't I know that", and finding out there really was no way to know.. Is that true here?Cranial
This worked for us.Joettejoey
F
33

Running RN 0.62 and this worked for me with successful build.

Update build.gradle enter image description here

classpath('com.android.support:multidex:1.0.3')

Update defaultConfig

enter image description here

multiDexEnabled true

After those 2 changes, restart the gradle build again.

Formfitting answered 18/6, 2020 at 21:11 Comment(2)
Thnaks you but it worked for me only with multiDexEnabled true.Korn
Perfect answer. Nothing better than seeing visuals of exactly where things need to be added in.Solano
G
16

Multidex is not always the solution to the problem, is true that it will generate more dex files to fit your method count, but be sure to not import more methods that you need because this in long term will make your builds slower than before.

For example, if you just need to use the location library from play services, you have two options

First one is implementing the whole play-services libraries that will come with location

implementation 'com.google.android.gms:play-services:11.8.0'

These whole libraries could have more than 40.000+ methods (is only an estimative, I don't really know the total count), being close to reaching the 65536 limit methods.

Instead you should be targeting only the libraries you will use instead of the whole bundle of libraries

in this case

implementation 'com.google.android.gms:play-services-location:11.8.0'

could have just 50 - 100 methods to work with, which will be better at build time than loading a whole bunch of methods from the whole library package that you won't ever use.

this is just a tip to avoid getting

Cannot fit requested classes in a single dex file.

For minSdkVersion above android 5.0 API 20 +

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. ART performs pre-compilation at application install time which scans for classes(..N).dex files and compiles them into a single .oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART.

If you are targeting lower devices (Android 4.1 API 16) or before Android 5 (API 20)

Versions of the platform prior to Android 5.0 use the Dalvik runtime for executing app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. In order to get around this limitation, you can use the multidex support library, which becomes part of the primary DEX file of your app and then manages access to the additional DEX files and the code they contain.

You will need to use multidex in this last case

Gnathic answered 30/12, 2019 at 15:6 Comment(0)
H
10

In build.gradle(app) file: Add the following in the dependencies:

 implementation 'com.android.support:multidex:1.0.3'

And add the following in the defaultConfig,

multiDexEnabled true

Hope you will find the solution.

Hireling answered 26/5, 2020 at 12:49 Comment(0)
C
10

Update 2021 Android-X and Android Studio 4.XX:

Adding multiDexEnabled in the default config of your app-level Build.Gradle is enough

multiDexEnabled true

See image for reference

enter image description here

Catinacation answered 18/4, 2021 at 13:20 Comment(0)
T
1

Recommended solution for android x

why the error

When your app and the libraries it references exceed 65,536 methods, you encounter a build error that indicates your app has reached the limit of the Android build architecture more information here

solution

just add the multi dex support library to your dependencies in the buid.gradle (Module: app) file

dependencies {
    def multidex_version = "2.0.1"
    implementation 'androidx.multidex:multidex:$multidex_version'
}
   
Tweezers answered 28/8, 2020 at 4:13 Comment(0)
F
1

You can fix this problem by following these steps.

Step 1: Migrate to AndroidX

Refactor > Migrate to AndroidX

Step 2: Add this dependency in build.gradle file:

implementation 'androidx.multidex:multidex:2.0.1'

Step 3: In build.gradle's defaultConfig section add:

multiDexEnabled true
Ferret answered 16/2, 2022 at 11:41 Comment(0)
P
-1

This error is comes when you use deprecated npm as well as correct npm for same purpose

Pentachlorophenol answered 3/12, 2020 at 9:46 Comment(1)
remove from pakage.jsonPentachlorophenol

© 2022 - 2024 — McMap. All rights reserved.