Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FATAL EXCEPTION: java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$layout

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?

like image 624
Rethinavel Avatar asked Aug 20 '15 06:08

Rethinavel


1 Answers

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

like image 88
Ramesh Avatar answered Sep 30 '22 16:09

Ramesh