NoClassDefFoundError with Android Studio on Android 4

I'm in the process of converting an existing app from Eclipse to Android Studio. However, when I run it on a device running 4.x (I've tested several versions on emulators), it immediately crashes with a NoClassDefFoundError.

I've tried commenting out references to the classes it can't find, but there's always another. As far as can tell, the offending class is always

  1. Within my own code
  2. An inner class (Update: With more investigation, this one isn't always true)

Everything works fine on a 5.0.1 emulator (I don't have a device to test on). My build.gradle file is fairly long, but looks like this:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

def AAVersion = "2.7.1"

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.myapp.android"
        minSdkVersion 8
        targetSdkVersion 19
        multiDexEnabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    packagingOptions {
        *snip*
    }
}

buildscript {
    repositories {
        mavenCentral()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

repositories {
    maven {
        url "https://repo.commonsware.com.s3.amazonaws.com"
    }
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.pact.android'
    }
}

dependencies {
    *snip compile project(':...') declarations

    apt "com.googlecode.androidannotations:androidannotations:$AAVersion"
    compile "com.googlecode.androidannotations:androidannotations-api:$AAVersion"

    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.google.android.gms:play-services:3.1.36'

    *snip many more compile declarations*

    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Some examples of classes that cause trouble:

  • An anonymous class that implements an interface inside the facebook library (as a library project)
  • Parcelable.CREATOR implementations in my models
  • An inner class that extends android.os.Library
  • An inner class in my own code
  • A class that implements an interface in a library from maven

What's going wrong, and how do I fix it?

Answers


I was incompletely implementing MultiDex support, so some of my classes weren't in the proper dex file. To fix it, you have to do more than just set multiDexEnabled = true in your defaultConfig block. You also have to:

  1. Include compile 'com.android.support:multidex:1.0.1' in your dependencies
  2. Have your Application class extend MultiDexApplication instead of just Application. Alternatively, you can call MultiDex.install() in attachBaseContext() of your application.

See https://developer.android.com/tools/building/multidex.html for more details.


1) Add multiDexEnabled = true in your default Config

2) Add compile com.android.support:multidex:1.0.0 in your dependencies

3) Application class extend MultiDexApplication instead of just Application


I solved the problem by removing multiDexEnabled = true in my case.


1) add multiDexEnabled = true in your defaultConfig in build.gradle.

2) You also have to Include compile 'com.android.support:multidex:1.0.1'

3) add the following to your "application" tag in your manifest:

android:name="android.support.multidex.MultiDexApplication"

its working with me , hope that help


I also issue get resolved after doing following steps

1) add multiDexEnabled = true in your defaultConfig in build.gradle.

2) You also have to Include compile com.android.support:multidex:1.0.1

3) add the following to your "application" tag in your manifest:

android:name="android.support.multidex.MultiDexApplication"

I got the same issue, tried all solutions suggested on StackOverflow but they did not work in my case. After that I found out that one of libraries compiled by Java 8. So, I need to enable Java 8 in my project too.

  android {
    //...
    compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
    }
  }

There are many reasons which leads to this error. Hope it helps.


In my case, it was Kotlin forEach was causing the exception. More info here: java.lang.NoClassDefFoundError $$inlined$forEach$lambda$1 in Kotlin


Need Your Help

Bootstrap: 'TypeError undefined is not a function'/'has no method 'tab'' when using bootstrap-tabs

javascript jquery ruby-on-rails twitter-bootstrap

I'm building a little site at the moment and wanted to use some of the Bootstrap parts from Initialzr, specifically the tabs. I've currently tried using the example code provided by the Bootstrap p...

Is it possible to specify a different ssh port when using rsync?

ssh rsync

I have been attempting the following command: