Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multidex - NoClassDefFoundError with multidex enabled

My app is crashing on pre-21 with java.lang.NoClassDefFoundError app.module.SomeClass error.

I already have Multidex enabled:

build.gradle:

android {
    defaultConfig {
        ...
        multiDexEnabled true
    }
}

dependencies {
   ...
   implementation "androidx.multidex:multidex:2.0.1"
}

My Application class:

class App : DaggerApplication() {
    ...
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

After reading about Declaring classes required in the primary DEX file I created multidex-config.pro file to include app.module.** in primary DEX file:

-keep class app.module.** { *; }

And registered it in build.gradle:

android {
  buildTypes {
    debug {
      ...
      multiDexKeepProguard file('multidex-config.pro')
    }
}

I confirmed it by checking build/intermediates/legacy_multidex_main_dex_list/debug/mainDexList.txt and analyzing the debug apk (checking whether classes.dex includes app.module.SomeClass).

But I'm still getting java.lang.NoClassDefFoundError app.module.SomeClass error.

I also tried cleaning caches, running on different machines (cli build only without Android Studio), disabling instant run, specifying javaMaxHeapSize, just extending MultiDexApplication and etc.

What can I try next?

like image 501
alashow Avatar asked Apr 23 '19 01:04

alashow


People also ask

What is multidex enabled?

Android applications by default have SingleDex support which limits your application to have only 65536 methods(references). So multidexEnabled = true simply means that now you can write more than 65536 methods(references) in your application.

Why multidex is required in Android?

This number represents the total number of references that can be invoked by the code within a single Dalvik Executable (DEX) bytecode file. This page explains how to move past this limitation by enabling an app configuration known as multidex, which allows your app to build and read multiple DEX files.


1 Answers

We had a deeper look at this here:

https://issuetracker.google.com/issues/131100011

There is an unfortunate bug in some Dalvik VMs on pre-21 devices where package private methods were sometimes incorrectly overriden by public methods in another package. If the package private method is final, that will then lead to errors of the form:

E/dalvikvm: Method Lcom/mycompany/MyViewModel;.clear overrides final Landroidx/lifecycle/ViewModel;.clear

It looks like that could be what is hitting you here. Unfortunately, the only workaround for that particular issue is to rename your "clear" method to something else so that you do not hit this unfortunate VM bug.

like image 182
Mads Ager Avatar answered Sep 28 '22 10:09

Mads Ager