Simple program gets "Cannot fit requested classes in a single dex file" when adding Firestore dependency

Viewed 1541

I have a very simple program that runs fine--until I add the library dependencies to connect with Firestore. Specifically, when I add implementation 'com.google.firebase:firebase-firestore:21.2.0' to my app gradle. (Changing to an earlier 17.x.x version makes no difference). The error is "Cannot fit requested classes in a single dex file", which to me makes no sense since the program is so simple: no way I have more than 64K methods running.

Here is the MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void addUnitType(View view) {
    return;
}

}

...here is activity_main.XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_unit_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_marginStart="7dp"
        android:hint="Unit Type (MedSurg, ICU, etc)"
        android:inputType="text"
        android:textSize="10sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

...this is the project gradle

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        classpath 'com.google.gms:google-services:4.3.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

...this is the app gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.nofirestore"
        minSdkVersion 19
        targetSdkVersion 29
        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'
    **implementation 'com.google.firebase:firebase-firestore:21.2.0'**
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'
}

And again, this is the offending code in the app gradle--remove it and I am fine, put it in the dependencies and the compiling crashes:

implementation 'com.google.firebase:firebase-firestore:21.2.0'

and this the error

"Cannot fit requested classes in a single dex file"

Any thoughts on what gives? I have run much more complex apps without the need for multidex. I would love to know. Thank you.

4 Answers

Well, it was NOT the missing

apply plugin: 'com.google.gms.google-services'

Instead, it was something as simple as the minSdkVersion19: changing that to 22 got rid of the multidex compiling error, and it runs fine!

Inside the defaultConfig block add the following:

multiDexEnabled true

Also in the build.gradle add the following dependency:

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

You can check more information about multidex in the following link:

https://developer.android.com/studio/build/multidex

Updating the minSdkVersion to 22 in the app level build.gradle worked on its own. There was no need for me to enter the multiDexEnabled settings noted above.

type multiDexEnabled true after minSdkVersion 21 in build.gradle app module

Related