FATAL EXCEPTION: java.lang.NoClassDefFoundError: a

2019-01-16 20:45发布

问题:

I just migrated from eclipse to studio. I followed one blog to export project from eclipse to studio. The app working fine in lollipop and throwing the following error in pre lollipop devices.

Getting this error only in studio. not in eclipse.

FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$layout
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:324)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:246)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.hsp.inventory.SplashActivity.onCreate(SplashActivity.java:53)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
          //
          ......
          //

My gradle file

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url "http://dl.bintray.com/journeyapps/maven"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

apply plugin: 'com.android.application'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:cardview-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.google.code.gson:gson:2.3'
   // compile "com.android.support:support-v4:18.0.+"

    compile project(':sliderLibrary')
    compile project(':camera')
    compile project(':volley')

    // Zxing library compile

    compile 'com.journeyapps:zxing-android-embedded:2.3.0@aar'
    compile 'com.journeyapps:zxing-android-legacy:2.3.0@aar'
    compile 'com.journeyapps:zxing-android-integration:2.3.0@aar'
    compile 'com.google.zxing:core:3.2.0'

}


android {

    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.hsp.inventory"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets', 'src/main/assets', 'src/main/assets/fonts']
        }

        instrumentTest.setRoot('tests')


        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

Please let me know if you want to take a look at my manifest and act_splash.xml. I will update here.

Any idea?

回答1:

I faced the same issue and fixed it. It is issue with Dex limit. Because the dex limit is reached, it creates two dex files. Lollipop knows how to read, pre-Lollipop has no idea unless you specify it in the Application class.

Please make sure following is in place:

in build.gradle

dependencies {
    compile 'com.android.support:multidex:1.0.0'
}
defaultConfig {          
    multiDexEnabled true
}

IMPORTANT to support pre-Lollipop:

In Manifest, under the application tag,

<application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
</application>

Or if you have used your own Application class, make your Application override attachBaseContext starting with

 import android.support.multidex.MultiDexApplication;
 import android.support.multidex.MultiDex;

 public class MyApplication extends MultiDexApplication {
 // ......

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

Reference: https://developer.android.com/tools/building/multidex.html#mdex-gradle



回答2:

Your logcat returns

java.lang.NoClassDefFoundError: android.support.v7.appcompat

So update your support repository (Version 23) And ** For API 23:**

compile 'com.android.support:appcompat-v7:23.0.0'


回答3:

As I can see the gradle file you have posted here is top level gradle file,which contains stuff that should not be there. Replace your top level gradle file contents like below :

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url "http://dl.bintray.com/journeyapps/maven"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}


回答4:

In the android documentation for v7 appcompat is a note:

Note: This library depends on the v4 Support Library. If you are using Ant or Eclipse, make sure you include the v4 Support Library as part of this library's classpath.

I think thats your problem since you have comment out the line in your gradle:

   // compile "com.android.support:support-v4:18.0.+"

Add the line again and try to compile the project.

Also i see no com.android.support:appcompat-v7:21.0.0 line in your gradle



回答5:

May be a long shot, but ensure your images are under /res/drawable and not /res/drawable-v24 See this answer: Binary XML file line #0: Error inflating class ImageView