NoClassDefFoundError with Android Studio on Android 4
Asked Answered
N

7

43

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?

Nomography answered 30/12, 2014 at 1:5 Comment(0)
N
121

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.

Nomography answered 30/12, 2014 at 17:14 Comment(0)
M
10

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

Molloy answered 11/3, 2016 at 10:47 Comment(0)
D
1

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

Deidradeidre answered 4/10, 2016 at 7:12 Comment(0)
F
0

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

Fulani answered 4/4, 2016 at 21:26 Comment(1)
It's doesn't work when you just added multiDexEnabled = true ... karl's answer is right.Plumlee
F
0

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"
Feuilleton answered 18/1, 2017 at 4:20 Comment(0)
G
0

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.

Getup answered 23/8, 2017 at 2:26 Comment(0)
T
0

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

Tayyebeb answered 24/5, 2018 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.