Home java "Cannot fit requested classes in a single dex file" error in Andoid...

“Cannot fit requested classes in a single dex file” error in Andoid studio

Author

Date

Category

I get 4 errors when launching a project in Andoid studio (green triangle)

Cannot fit requested classes in a single dex file (# methods: 67080 & gt; 65536)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
Caused by: com.android.tools.r8.utils.b: Error: null, Cannot fit requested classes in a single dex file (# methods: 67080 & gt; 65536)
Error: null, Cannot fit requested classes in a single dex file (# methods: 67080 & gt; 65536)

I managed to work up that it was connected with something from build.gradle, I didn’t make it any further.

Help me figure out what the problem is. (Sorry, if something goes wrong – I’m a noob, just starting my way in the development of mob applications)

Code build.gradle (Project: MyApp):

buildscript {
  repositories {
    jcenter ()
    google ()
    maven {
      url 'https://maven.google.com/'
      name 'Google'
    }
    maven {url 'https://plugins.gradle.org/m2/'}
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.6.1'
    classpath 'com.google.gms: google-services: 4.3.4'
    classpath 'gradle.plugin.com.onesignal: onesignal-gradle-plugin: 0.12.6'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
}
allprojects {
  repositories {
    google () // Google's Maven repository
    jcenter ()
    maven {
      url 'https://maven.google.com/'
      name 'Google'
    }
  }
}
task clean (type: Delete) {
  delete rootProject.buildDir
}

Code build.gradle (Module: App):

apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
  compileSdkVersion 29
  buildToolsVersion "29.0.2"
  defaultConfig {
    applicationId "com.super.app"
    minSdkVersion 16
    targetSdkVersion 29
    versionCode 4
    versionName "1.3"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
  android {
    defaultConfig {
      manifestPlaceholders = [
          onesignal_app_id: 'xxx-xxx-xxx-xxx-xxx', // Real app_id here
      ]
    }
  }
}
dependencies {
  implementation fileTree (dir: 'libs', include: ['* .jar'])
  implementation 'androidx.constraintlayout: constraintlayout: 1.1.3'
  testImplementation 'junit: junit: 4.12'
  implementation 'androidx.appcompat: appcompat: 1.1.0'
  implementation 'com.google.android.material: material: 1.1.0'
  implementation 'androidx.cardview: cardview: 1.0.0'
  implementation 'com.google.android.gms: play-services-ads: 19.0.1'
  implementation 'com.google.code.gson: gson: 2.8.5'
  implementation 'com.google.firebase: firebase-analytics: 17.2.3'
  implementation 'com.onesignal: OneSignal: 3.12.6'
  implementation platform ('com.google.firebase: firebase-bom: 26.1.1')
}

Answer 1, authority 100%

You are getting this error because you have reached and passed the Android build architecture limit – a single DEX file can only contain about 64K references.

There are several options to solve this problem.

  • Use multi-dex. Instead of a single DEX file, your APK will have multiple dex files. You can follow this tutorial to install it in your build files. This may affect your minimum SDK version.

Try adding the following dependencies in your build.gradle (app) file:

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

And in defaultConfig:

multidexenabled true
  • Use Proguard or R8 – among other things, these tools provide code compression. This means that all unused code will be removed, and therefore the limit of 64 KB should not be achieved. You can follow this manual to configure code compression.

I hope that something will help. More options can be found in English-speaking stackoverflow.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions