Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Some users of my app are experiencing this crash, it appears to possibly be related to Samsung devices. Has anyone else experienced this issue and found a solution?

java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$layout
       at android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java:297)
       at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:225)
       at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
       at com.afollestad.impression.ui.MainActivity.onCreate(MainActivity.java:160)
       at android.app.Activity.performCreate(Activity.java:5451)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
       at android.app.ActivityThread.access$900(ActivityThread.java:173)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5579)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(NativeStart.java)

Line 160 sets the Activity's content view. The content view is this:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.afollestad.lessen.ui.MainActivity"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="?colorPrimary"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="?popup_theme" />

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <FrameLayout
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="start">

        <fragment
            android:id="@+id/navigation_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.afollestad.impression.fragments.NavDrawerFragment"
            tools:layout="@layout/fragment_navdrawer"
            android:tag="NAV_DRAWER" />

    </FrameLayout>

</android.support.v4.widget.DrawerLayout>

The fragment is a simple list fragment.

like image 684
afollestad Avatar asked Dec 23 '14 05:12

afollestad


4 Answers

I know that issue was solved but if you arrive on this thread, it could be that it doesn't solve your problem in one particular combination:

  • All works on 5.0 devices
  • It doesn't work on 4.x
  • You think you enable MultiDex
  • Proguard doesn't seem to be a problem

It happened to me and I searched for 3 hours. I hope it'll help some. To enable MultiDex on 4.x devices, it is not enough to modify the build: you have to subclass the Application class. Just follow that and that

Hope it helps.

like image 175
Laurent Meyer Avatar answered Nov 11 '22 09:11

Laurent Meyer


I fixed the issue by using Proguard with the following config:

-keep class !android.support.v7.internal.view.menu.**,** {*;}
-dontwarn
-ignorewarnings
-dontshrink

To enable Proguard with newer versions of Gradle (in Android Studio):

android {

    ...

    buildTypes {
        debug {
            ...
        }
        release {
            ...
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

proguard-rules.pro is where the properties at the top go.

like image 38
afollestad Avatar answered Nov 11 '22 08:11

afollestad


If you use multidex in your application, extend your Application class from MultiDexApplication

like image 3
Gurgen Hakobyan Avatar answered Nov 11 '22 07:11

Gurgen Hakobyan


if you're using api compat support-v7, any libraries whose reference to it should be marked exclude module: 'support-v4'

for example:

compile('com.android.support:cardview-v7:22.2.0') { exclude module: 'support-v4' }

like image 1
Anton R Avatar answered Nov 11 '22 09:11

Anton R