I am trying to connect my app with FireStore. I am following all the steps in the Tools/Firebase/Firestore/Read and write documents with Cloud Firestore tab. The problem is that when running my application I get the following error:
Cannot fit requested classes in a single dex file (# methods: 72403 > 65536).
I attach my gradle:app
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.bdfirestore"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.firebase:firebase-firestore:21.3.1'
}
The error:
This happens because of what the documentation explains in the How to enable multidex for apps with methods larger than 64K section :
That happened to me recently in a project as a result of having migrated to
androidx
. Therefore it is very likely that the new platform will use more methods so that the limit of65,536 métodos
is exceeded. Those methods are not only those of your application, but those of all the libraries that you have incorporated into your project.Two solutions are possible: enable
multidex
or apply a method reduction policy.1. Enable
multidex
You only have to modify some configurations in the file:
app/build.gradle
:What does it mean
multidex
?The same documentation explains it clearly:
When you enable
multidex
you indicate that multiple files can be processedDEX
.2. Apply a method reduction policy
If you want to apply a method reduction policy, you can review Android's recommendations in How to reduce, obfuscate, and optimize your app .
You can also read: How to avoid the 64K limit
Adding to the answer of @A.Cedano, the error that appears from dex is because you exceed the number of allowed methods (65536) this means that in your application module build.gradle, surely you added many dependencies or added entire packages with many methods.
For example, if we are going to use the location service of google play services there are two ways to do it
One is to directly add all play services to our app
What would add for example 50000+ methods
Or you can simply add the location library from play services, which is the one you need
which for example would only add 100 methods for example.
In this way you prevent dexing problems in your app, this is the same for all libraries, for example Firebase, when we add firebase-core instead of each element that we need separately.
This is why dex problems occur, and you have to add the multidex if you need more dex files.
Basically how it works is like this
A dex file is created that contains, together with the bytecode of your apk and its methods, a limit of 65536 methods for your app, if this amount is exceeded, a dexing problem will occur which will tell you that you cannot allocate more than what you can in your app. This is where you add multidex to create not just 1 but x dex files needed to run your app, so you'll never have the dex problem again, but it's also not recommended to always use it if it's not needed since you run an apk with tons of methods that you don't need, causing its performance to be reduced.
The problem
When you use a minSdkVersion in 16 to 19
When you use a minSdkVersion in 20 and up
ART documentation: https://developer.android.com/guide/practices/verifying-apps-art?hl=es-419
So if you use minSdkVersion >= 20 you won't need to use multidex in your app