Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError on Android 4.4 or lower

I've got an Android application that I recently upgraded to the Gradle build system among other things, such as using a newer version of build tools, etc.

My targetSdkVersion is 19, so I should be good to go on Android 4.4 and higher. When I run using a 5.0+ device, all is well; however Android 4.4 always crashes with a java.lang.NoClassDefFoundError error of some sort.

As a test, I removed original class that it complained about missing only to have it crash while pointing at a different class.

The first class that it crashed on was an internal private class in a 3rd party library. After removing that library, it pointed to a internal private class in the app itself.

In summary:

  • the app runs fine on Android 5.0+. It crashes with java.lang.NoClassDefFoundError on anything less than 5.0.
  • In tests so far, the NoClassDef always seems to refer to an inner class - this is just based on two tests, so it may not be anything concrete.

Here's my android.manifest file: https://gist.github.com/rscott78/19dd88ccde66172d9332

like image 958
bugfixr Avatar asked Sep 15 '15 01:09

bugfixr


Video Answer


1 Answers

For what it's worth, this can happen when you enable multi-dex support without adding the correct code in your Application class.

Create a class, have it inherit from Application, then add this override:

@Override
protected void attachBaseContext(Context base) {
  super.attachBaseContext(base);
  MultiDex.install(this);
}

In your AndroidManifest, add a name attribute to your application tag:

<application name=".MyApplication"
like image 133
bugfixr Avatar answered Dec 17 '22 01:12

bugfixr