NoClassDefFoundError: android.support.v7.internal.view.menu.MenuBuilder
EDIT:
The solution that worked for me was (Using Proguard) to replace this:
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.** { *; }
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
with this:
# Allow obfuscation of android.support.v7.internal.view.menu.**
# to avoid problem on Samsung 4.2.2 devices with appcompat v21
# see https://code.google.com/p/android/issues/detail?id=78377
-keep class !android.support.v7.internal.view.menu.**,android.support.** {*;}
Credit goes to the google group, #138.
Old answer (Temporary Workaround):
It happens in a project where I use an spinner in the ActionBar. My solution was to check for those conditions and change the app flow:
public static boolean isSamsung_4_2_2() {
String deviceMan = Build.MANUFACTURER;
String deviceRel = Build.VERSION.RELEASE;
return "samsung".equalsIgnoreCase(deviceMan) && deviceRel.startsWith("4.2.2");
}
Then in the activity's onCreate method:
if (isSamsung_4_2_2()) {
setContentView(R.layout.activity_main_no_toolbar);
} else {
setContentView(R.layout.activity_main);
}
As pointed out this is not a definitive solution, it is just a way to allow users to have access to limited functionality while a more permanent solution is found.
Once again getting java.lang.NoClassDefFoundError: android.support.v7.internal.view.menu.i after updating to Support Tools 23
Your ProGuard "keep class" rule needs to be updated, as the targeted classes are no longer being obfuscated. In version 23 of the support tools, Google moved the menu-related classes out of the internal package. I can confirm that the following ProGuard rules eliminated the NoClassDefFoundError that was being seen on certain Samsung devices running Android 4.2.2.
# Workaround for conflict with certain OEM-modified versions of the Android appcompat
# support libs, especially Samsung + Android 4.2.2
# See this thread for more info:
# 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
-keepattributes **
# Keep all classes except the ones indicated by the patterns preceded by an exclamation mark
-keep class !android.support.v7.view.menu.**,!android.support.design.internal.NavigationMenu,!android.support.design.internal.NavigationMenuPresenter,!android.support.design.internal.NavigationSubMenu,** {*;}
# Skip preverification
-dontpreverify
# Specifies not to optimize the input class files
-dontoptimize
# Specifies not to shrink the input class files
-dontshrink
# Specifies not to warn about unresolved references and other important problems at all
-dontwarn **
# Specifies not to print notes about potential mistakes or omissions in the configuration, such as
# typos in class names or missing options that might be useful
-dontnote **
I strongly recommend that you use dex2jar and jd to confirm that the desired obfuscation has happened (described here: https://stackoverflow.com/a/10191338/315702). Unfortunately, this should be done each time you upgrade the support tools, in case packages are moved or renamed again.
Google moved the classes out of the "internal" package in the hope that this would avoid the classpath conflict that causes the NoClassDefFoundError. However, as Chris Banes stated on the thread for this bug (https://code.google.com/p/android/issues/detail?id=78377), they did not test to verify the fix -- and lots of people have since reported the same crash you're seeing:
Right, we've decided to do a one-time rename of the internal classes
which should fix this. I have done no testing on those devices
though, and don't plan on doing any either.
Crash: java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$layout
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.
Related Topics
How to Open a Unc Path from Linux in Java
Cannot Run Program "Mvn" Error=2, No Such File or Directory
Commons Vfs and Java.Net.Url - Adding Support for "Sftp://" Protocol
Issues with Corba Communication
Using Xdg Directory Specification on Java Application
Connect as User with No Password Set on Postgresql 8.4 via Jdbc
Determine If a String Is an Integer in Java
Linux Ulimit with Java Does Not Work Properly
What Is an Illegal Reflective Access
Image Encryption/Decryption Using Aes256 Symmetric Block Ciphers
How to Use Chef to Update-Alternatives for Java Using Execute
Using Visualvm to Connect to a Remote Jstatd Instance Through a Firewall
Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Org/Apache/Hadoop/Util/Platformname
No Swt-Mozilla-Gtk-4332 in Java.Library.Path
Java Version Still Showing as 1.4 Linux
Could Not Create the Java Virtual MAChine