Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot fit requested classes in a single dex file, even for earlier commits which previously compiled fine

So I've just hit the maximum method count limit for my android project, which fails to build with the following error message:

Error: null, Cannot fit requested classes in a single dex file (# methods: 117407 > 65536)

I understand what the message means, and how to resolve it (running proguard, enabling multidex etc). My problem is that I don't understand why I'm suddenly getting this message - I was doing was removing some old bits of code which were redundant, hit build, and now I get this message.

Question 1: How can it be possible that my method count (117407 according to the error message) is suddenly massively over the limit (65536), even though I did not add any library dependencies? I actually removed code, and suddenly I have like 50 thousand methods too many?

Now this is where it gets really weird: I wanted to analyse the APK to figure out what's causing the problem, but of course I can't build it. So instead of enabling multidex I decided to revert my code to yesterday (which definitely absolutely did build fine yesterday - I have the app on my phone to prove it!), but I still get this build error message. I don't understand how this is possible. I tried reverting to several days ago, same thing (cloning a new repo and checking out an earlier commit).

So, question 2: How am I getting this build error for the exact same code which just yesterday built fine without error?

The only thing I can think of is that a library that I am using as a dependency has suddenly increased in size - but I'm declaring specific versions of everything in my gradle build, for example:

// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

So, surely my dependencies should not have changed?

Any ideas what I can do to figure this out are greatly appreciated. I've tried cleaning my project, and invalidating caches/restart in android studio. I really don't want to enable multidex or have to run proguard on my debug build.

Here's the full build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
    applicationId "XXXXXXXXX"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "0.1"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true  // see https://developer.android.com/studio/write/vector-asset-studio#sloption
}
buildTypes {
    release {
        minifyEnabled false
        // Do code shrinking!
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

// Core stuff
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-vector-drawable:28.0.0'
implementation 'com.google.android.gms:play-services-wearable:16.0.1'

// Dagger
implementation 'com.google.dagger:dagger:2.21'
kapt 'com.google.dagger:dagger-compiler:2.21'
// Dagger for Android
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21' // if you use the support libraries
kapt 'com.google.dagger:dagger-android-processor:2.21'

// Constraint layout
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

// Associated WearOS project
wearApp project(':wear')

// Common library project
implementation project(':common')

// These were added to resolve gradle error on the 'com.android.support:appcompat-v7:28.0.0' implementation:
// All com.android.support libraries must use the exact same version specification (mixing versions can lead to
// runtime crashes). Found versions 28.0.0, 26.1.0. Examples include com.android.support:animated-vector-drawable:28.0.0
// and com.android.support:support-media-compat:26.1.0
// This seems to be related to linking the wear project. If the wear project was not linked, the error went away.
implementation 'com.android.support:support-media-compat:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'

// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
// Retrofit RxJava
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
// Retrofit logging:
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'

// Room
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
implementation "android.arch.persistence.room:common:$room_version"
implementation "android.arch.persistence.room:rxjava2:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"

// For modern time handling (java.time requires API 26 or higher)
implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1'

// Graphing
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'

// Dropbox
implementation 'com.dropbox.core:dropbox-core-sdk:3.0.11'

// OpenCSV
implementation 'com.opencsv:opencsv:4.5'

}

EDIT

So after enabling multidex, there are some heavy dependencies showing up under the following TLDs when I analyse the APK using Android Studio (I'm not sure if I should be looking at defined or referenced method numbers?):

  • com.dropbox: 26000 defined methods, 34000 referenced methods
  • com.android (mainly support libraries): 18700 defined, 24600 referenced
  • org.apache (commons, log etc): 15000 defined, 15700 referenced

These alone take me up to the limit. I still don't get why this is suddenly happening though :( Surely if I have not added any libraries, these numbers should not have changed?

like image 969
James Allen Avatar asked Feb 27 '19 18:02

James Allen


1 Answers

Simple add this to your gradle (Module: app) >> multiDexEnabled true

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

then Rebuild Project in Menu click => Build>Rebuild Project.

like image 101
Driss Baidou Avatar answered Nov 05 '22 03:11

Driss Baidou