79

We just changed our application to use the appcompat-v7 support library in order to take advantage of the support actionbar and support Material themes. Using v21.0.0 of appcompat-v7 (andv21.0.0 of support-v4), we are now seeing crashes in Google Play and Crashlytics only from Samsung devicesrunningAndroid v4.2.2. Here is the stack trace from Google Play and the app appears to crash as soon as theactionbar` is shown and/or invalidated.

java.lang.NoClassDefFoundError: android.support.v7.internal.view.menu.MenuBuilder
at android.support.v7.app.ActionBarActivityDelegateBase.initializePanelMenu(ActionBarActivityDelegateBase.java:991)
at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1041)
at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1259)
at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:80)
at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:116)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

Other devices and emulators running v4.2.2 do no exhibit this behavior. It's my understanding that many Google apps are already using this new version of appcompat to display the action bar. If these apps are not reporting crashes on these devices, it would be helpful to know how this is being avoided/fixed.

I reported this as a bug to Google but it got closed with the reason that it is a development issue. Although I do agree this may be the case, I'm wondering if/how anyone is currently able to use appcompat-v7 v21.0.0 and not get crashes on Samsung 4.2.2 devices.

Update: It looks like Google is at least considering possible workarounds for this. See this for details.

Eric Nordvik
  • 14,656
  • 8
  • 42
  • 50
Erik Pedersen
  • 793
  • 1
  • 5
  • 6
  • possible duplicate of [NoClassDefFoundError: android.support.v7.internal.view.menu.MenuBuilder](http://stackoverflow.com/questions/24809580/noclassdeffounderror-android-support-v7-internal-view-menu-menubuilder) – Nachi Oct 30 '14 at 15:41
  • This may be a related issue, but it appears to be a slightly different exception. One recent solution posted in the other question relates to having a spinner in the action bar and I do not have that situation. I'm just using menu items. – Erik Pedersen Oct 30 '14 at 15:55
  • I have this problem too. App is in production and receiving crashes from samsung with 4.2.2. devices – Martin Vandzura Nov 03 '14 at 07:16
  • If you're using spinner in your action bar then it is an issue on Samsung devices. Check http://stackoverflow.com/a/24810104/569346 – NinjaCoder Nov 04 '14 at 02:01
  • 1
    I have no spinner in my Action Bar too and am getting this from non Samsung devices running Android 4.2.2: Qmobile I9 and Wiko (unknown model). – Jürgen 'Kashban' Wahlmann Nov 04 '14 at 08:45
  • have you tried to clean project? or check if the library really inserted on the project @ properties>android. – ralphgabb Nov 05 '14 at 05:45
  • @Jürgen'Kashban'Wahlmann I am also getting the same exception for QMobile A290 device running android 4.4, did you find any solution? – Devashish Mamgain Dec 01 '14 at 14:39
  • 1
    @Devashish: The proguard solution in the second answer worked for me. I tested against Samsung device affected with appthwack.com and after applying the proguard config I got no more errors. – Jürgen 'Kashban' Wahlmann Dec 02 '14 at 08:29

9 Answers9

15

I found the proper solution here: https://stackoverflow.com/a/26641388/1266123

By using

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

instead of

-keep class android.support.v7.** {*;}
Community
  • 1
  • 1
robUx4
  • 853
  • 9
  • 13
  • 1
    I think you can save more space by using "-keepnames" instead of "-keep". – Justin Feb 15 '15 at 20:45
  • As noted in the discussion from https://code.google.com/p/android/issues/detail?id=78377, using this particular solution might cause problems due to resource references within the appcompat library. However, this has been working for our particular app so I'm marking this answer as accepted. – Erik Pedersen Mar 19 '15 at 14:07
  • where should i write this line? – Bugs Happen Jun 27 '15 at 08:16
  • @BugsHappen, this would go in your Gradle build file. You can read more about ProGuard here: http://developer.android.com/tools/help/proguard.html – Dick Lucas Jul 07 '15 at 20:18
7

As #150 from https://code.google.com/p/android/issues/detail?id=78377 said

Because careful with -keep class !android.support.v7.internal.view.menu.**. There are a number of classes in there which are referenced from the appcompat's resources.

The better solution is add the following lines instead:

-keep class !android.support.v7.internal.view.menu.MenuBuilder, !android.support.v7.internal.view.menu.SubMenuBuilder, android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
Pongpat
  • 13,248
  • 9
  • 38
  • 51
  • In my tests, based on a review of the generated proguard mapping file, this suggested proguard config does not result in obfuscating the MenuBuilder class name, although it does obfuscate SubMenuBuilder – Andy Dennie Mar 27 '15 at 14:38
  • Figured it out; see my answer. – Andy Dennie Mar 27 '15 at 15:10
  • hey Andy i have the same issue, MenuBuilder is not obfuscated but others are, mind telling me how you solved it? thanks – Qing Mar 21 '16 at 07:19
6

Since Appcompat 23.1.1 the .internal package in the AppCompat jar was removed.

Updated fix using proguard:

#FOR APPCOMPAT 23.1.1:
-keep class !android.support.v7.view.menu.*MenuBuilder*, android.support.v7.** { *; }
-keep interface android.support.v7.* { *; }
RWIL
  • 8,729
  • 1
  • 28
  • 37
2

For all having this problem, only workaround so far seems to be using proguard. Checkout discussion at https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=78377

Martin Vandzura
  • 3,047
  • 1
  • 31
  • 63
1

If anyone interested in using a solution without progaurd .

Read the link i have tried this in one of my apps which gave the exception on setSupportActionBar(toolbar) in onCreate().

Its pretty simple just add try catch block around the call

try {

 setSupportActionBar(toolbar);

} catch (Throwable t) {

 // WTF SAMSUNG!

}
Ravi
  • 4,872
  • 8
  • 35
  • 46
0

I encountered the same issue on Tecno P9, but after using build tools 24 and for my support library I used 24.2.0, it was fixed.

Ikechukwu Kalu
  • 1,474
  • 14
  • 15
-2

Change the Compile Sdk Version of your project to "API 18:(JellyBean)"

The default is set to "Lollipop"

So far it solved my problem on Qmobile i9

STEPS

  1. Right Click on your project and select Open Module Settings (or press F4)
  2. In the properties tab Compiled Sdk Version
-3

Replace AppCompatActivity With Activity

This helped me.

  • Cheap fix. He might be using AppCompat because he wants to support API < 11 or something. This will break that idea. – Sufian Nov 07 '15 at 07:29
  • I think, it is maybe bad enough post to be downvoted, if anybody agrees with the reason named by Sufian. But not so bad to be deleted. – Gangnus Nov 07 '15 at 20:48
-3

Replace

public class class_name extends AppCompatActivity
{

.........

}

With

public class class_name extends Activity
{

.........

}

This helped me.

Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71