17

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?

Rethinavel
  • 3,912
  • 7
  • 28
  • 49
  • 1
    Switch to the latest version v 23 by updating your support repository. `compile com.android.support:appcompat-v7:23` – Raghunandan Aug 20 '15 at 06:21
  • @Raghunandan I got this error after updating: `Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'. Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'. Error:Execution failed for task ':processDebugResources'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/rethinavel/Library/Android/sdk/build-tools/22.0.1/aapt'' finished with non-zero exit value 1` – Rethinavel Aug 20 '15 at 06:56
  • 1
    Use AppCompat theme . Also update build tools to 23 – Raghunandan Aug 20 '15 at 07:05
  • @Raghunandan You are right sir – IntelliJ Amiya Aug 20 '15 at 07:17
  • Have you rebuild the hole project? – Zelldon Aug 20 '15 at 12:41
  • @Zelldon Yes i did. Still getting the same error `FATAL EXCEPTION: java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$layout` in pre lollipop devices. – Rethinavel Aug 20 '15 at 12:51
  • Do comment/mark the correct answer for people to get it right. – Ramesh Aug 28 '15 at 13:43

5 Answers5

78

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

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Ramesh
  • 1,361
  • 1
  • 11
  • 19
  • This is the only thing which worked for me!! You saved my day, night, morning and everything!! Hearty thanks Ramesh! – Rethinavel Aug 31 '15 at 08:35
  • It isn't necessary to change the name of your Application class in the Android Manifest. At least, it wasn't for me. – ironMover Oct 14 '15 at 06:41
  • @mikbanUtah: "If you have written Application class, Have your Application override attachBaseContext" – Ramesh Oct 14 '15 at 10:27
  • @Ramesh "If you have written [a custom] Application class, Have[sic] your Application override attachBaseContext [and just make sure the Application class in the Android manifest is the same as the one you wrote]" It isn't clear that it's optional to do one or the other – ironMover Oct 20 '15 at 16:15
  • Wow. Wow. Wow! This is amazing. Thanks a lot! – Sidharth Raja Nov 16 '15 at 13:37
  • I used this code. But MyApplication class gave null Context. so I used public class MyApplication extends Application in MyApplication class. it is working perfectly. I up-voted this answer, thanks Ramesh & Rethinavel Pillai. – Thirumalvalavan Dec 03 '15 at 12:11
  • do i have to extend MultiDexApplication inside every java file i mean all activity and all fragments ?? – Sudhanshu Gaur Dec 04 '15 at 22:52
1

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'
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    After changing, I got this error `Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'. Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'. Error:Execution failed for task ':processDebugResources'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/rethinavel/Library/Android/sdk/build-tools/22.0.1/aapt'' finished with non-zero exit value 1` – Rethinavel Aug 20 '15 at 06:55
  • 1
    FAILURE: Build failed with an exception. I am still getting the same exception. :-( – Rethinavel Aug 20 '15 at 07:06
  • @RethinavelPillai Did you set your buildtoolversion 23 instead buildToolsVersion '22.0.1' – IntelliJ Amiya Aug 20 '15 at 07:12
  • `compileSdkVersion 22 buildToolsVersion '23' defaultConfig { applicationId "com.hsp.inventory" minSdkVersion 14 targetSdkVersion 22 versionCode 1 versionName "1.0" multiDexEnabled true }` [SDK repository](http://s24.postimg.org/5y4p61c1h/Screen_Shot_2015_08_20_at_1_01_06_pm.png) **Style :** ` – Rethinavel Aug 20 '15 at 07:35
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87449/discussion-between-rethinavel-pillai-and-intellij-amiya). – Rethinavel Aug 20 '15 at 07:42
0

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()
    }
}
Amar
  • 141
  • 1
  • 11
  • let me try your solution and revert you back! – Rethinavel Aug 20 '15 at 07:06
  • I guess both are same. Doesn't it? – Rethinavel Aug 20 '15 at 07:08
  • you tried it ? the part starting from apply plugin should not be there,it should be only in the inner build.gradle file. – Amar Aug 20 '15 at 07:12
  • Are you sure about it? – Rethinavel Aug 20 '15 at 07:39
  • you tried all other things i guess , i am suggesting you because it happens when migrating eclipse project to android studio. – Amar Aug 20 '15 at 07:49
  • Actually i don't have good knowledge about gradle files. Honestly i don't know how to change the files as you mentioned. would you please guide me! – Rethinavel Aug 20 '15 at 07:53
  • There will be a build.gradle file inside your app folder that is the inner gradle file and one in your project folder that is the outer or root gradle file . You have posted the root gradle file as I can see the comment on the top of file , so simply replace that file with contents as i have suggested and it may solve your problem,if not then the root gradle file still needs to be corrected like i have suggested. – Amar Aug 20 '15 at 08:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87454/discussion-between-rethinavel-pillai-and-amar). – Rethinavel Aug 20 '15 at 08:22
0

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

Zelldon
  • 5,396
  • 3
  • 34
  • 46
0

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

kurdtpage
  • 3,142
  • 1
  • 24
  • 24