Java NoClassDefFoundError: android.support.v7.internal.view.menu.MenuBuilder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24809580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoClassDefFoundError: android.support.v7.internal.view.menu.MenuBuilder
提问by Matt K
There is an issue with the Android appcompat v7 library on Samsung devices running Android 4.2. I keep getting crashes with the following stack trace in my Developer Console:
运行 Android 4.2 的三星设备上的 Android appcompat v7 库存在问题。我的开发人员控制台中的以下堆栈跟踪不断导致崩溃:
java.lang.NoClassDefFoundError: android.support.v7.internal.view.menu.MenuBuilder
at android.support.v7.widget.PopupMenu.<init>(PopupMenu.java:66)
at com.[my-package-name].CustomActivity.onClick(CustomActivity.java:215)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
This is line 215 of CustomActivity.java:
这是 CustomActivity.java 的第 215 行:
PopupMenu popup = new PopupMenu(CustomActivity.this, mImageViewMenu);
The crashes come from an array of devices, but always Samsung, and always Android 4.2.
崩溃来自一系列设备,但总是三星,总是 Android 4.2。
A quick web search leads me to believe that many people have the same issue, some of the steps I have tried to solve the issue are:
快速的网络搜索让我相信很多人都有同样的问题,我试图解决这个问题的一些步骤是:
- Check the Android project properties, make sure the appcompat library is added properly.
- Check the Java Build Path Order and Export project properties, make sure Android Dependencies and Android Private Libraries is checked.
- Confirm the class is included in the library (android.support.v7.internal.view.menu.MenuBuilder).
- Confirm R.java is located in gen directory for android.support.v7.appcompat.
- Confirm the AppCompat theme is included in the Manifest.xml activity.
- Clean and rebuild project.
- 检查 Android 项目属性,确保正确添加了 appcompat 库。
- 检查 Java Build Path Order 和 Export 项目属性,确保选中 Android Dependencies 和 Android Private Libraries。
- 确认该类包含在库 (android.support.v7.internal.view.menu.MenuBuilder) 中。
- 确认 R.java 位于 android.support.v7.appcompat 的 gen 目录中。
- 确认 AppCompat 主题包含在 Manifest.xml 活动中。
- 清理并重建项目。
Despite these steps, and despite it working on all other devices and Android versions the crash reports still come through.
尽管有这些步骤,尽管它适用于所有其他设备和 Android 版本,但崩溃报告仍然存在。
采纳答案by unify
EDIT:
编辑:
The solution that worked for me was (Using Proguard) to replace this:
对我有用的解决方案是(使用 Proguard)来替换它:
-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.
归功于google 组,#138。
Old answer (Temporary Workaround):
It happens in a project where I use an spinner in theActionBar. My solution was to check for those conditions and change the app flow:
旧答案(临时解决方法):它发生在我在ActionBar 中使用微调器的项目中。我的解决方案是检查这些条件并更改应用程序流程:
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:
然后在活动的 onCreate 方法中:
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.
正如所指出的,这不是一个确定的解决方案,它只是一种允许用户访问有限功能的方法,同时找到更持久的解决方案。
回答by Eldar Miensutov
回答by u2tall
I enabled proguard with the default proguard properties provided with an eclipse project and the problem was fixed for me. Based on some comments here https://code.google.com/p/android/issues/detail?id=78377, some people might have to repackage using: -repackageclasses "android.support.v7"
我使用 eclipse 项目提供的默认 proguard 属性启用了 proguard,问题已为我解决。根据此处https://code.google.com/p/android/issues/detail?id=78377 的一些评论,有些人可能需要使用以下方法重新打包: -repackageclasses "android.support.v7"
回答by Ganesh AB - Android
On which device you are facing this problem ? (Samsung/HTC etc.)
您在哪个设备上遇到此问题?(三星/HTC等)
If it is Samsung,
如果是三星,
Various Samsung phones are included older versions of the android support library in the framework or classpath. If you use the new material support library, you'll see this crash on those Samsung devices:
各种三星手机都包含在框架或类路径中的旧版本 android 支持库。如果您使用新的材料支持库,您将在这些三星设备上看到此崩溃:
java.lang.NoClassDefFoundError: android.support.v7.internal.view.menu.MenuBuilder
To fix this, you must rename that class. Easiest way to do that is by running proguard. If you don't want to obfuscate, here's a 1 liner to rename just the offending classes:
要解决此问题,您必须重命名该类。最简单的方法是运行 proguard。如果你不想混淆,这里有一个 1 liner 来重命名有问题的类:
-keep class !android.support.v7.internal.view.menu.**,** {*;}
There's an issue tracking this problem, but since it's really a Samsung bug, it's never going to get fixed on their end. Only way to fix it on the Google/AOSP side is to rename these internal classes.
有一个问题跟踪此问题,但由于它确实是三星的错误,因此永远不会在他们结束时得到修复。在 Google/AOSP 端修复它的唯一方法是重命名这些内部类。
回答by Pongpat
As #150 from google groupssaid
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.
因为小心 -keep class !android.support.v7.internal.view.menu.**。其中有许多类是从 appcompat 的资源中引用的。
The better solution is add the following lines instead:
更好的解决方案是添加以下几行:
-keep class !android.support.v7.internal.view.menu.*MenuBuilder*, android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
回答by FAQi
I was having the same problem of this MenuBuilder class not found in USB debugging mode. I solved this problem by simply setting the minifyEnabledto truein both release and debug buildTypesblock of build.gradle. like this:
我在 USB 调试模式下找不到这个 MenuBuilder 类的相同问题。我通过简单的设置解决了这个问题minifyEnabled到真正的发布和调试buildTypes块的build.gradle。像这样:
buildTypes {
debug {
minifyEnabled true
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
I set the minifyEnabledto true in debugtype to prevent app from crashing via USB debugging to a live handset.
我在调试类型中将minifyEnabled设置为 true以防止应用程序通过 USB 调试崩溃到实时手机。
回答by Jazib Hasan
Change the Compile Sdk Version of your project to "API 18:(JellyBean)"
将项目的编译 Sdk 版本更改为“API 18:(JellyBean)”
The default is set to "Lollipop
默认设置为“棒棒糖
STEPS
脚步
- Right Click on your project and select Open Module Settings (or press F4)
- In the properties tab Compiled Sdk Version
- 右键单击您的项目并选择打开模块设置(或按 F4)
- 在属性选项卡中编译的 SDK 版本
回答by JulianDavid
I got the same error when trying to run a 'Hello World' app on my Samsung Galaxy Tab 3 tablet via Android Studio. The app would appear to launch and then it would crash instantly and that error would show in the console in Android Studio. I did a system update on the tablet and now I am able to run the 'Hello World' app and I'm not getting the error anymore. I hope this helps someone to resolve their issue.
尝试通过 Android Studio 在我的三星 Galaxy Tab 3 平板电脑上运行“Hello World”应用程序时,我遇到了同样的错误。该应用程序看起来会启动,然后会立即崩溃,并且该错误会显示在 Android Studio 的控制台中。我在平板电脑上进行了系统更新,现在我可以运行“Hello World”应用程序,并且不再出现错误。我希望这有助于某人解决他们的问题。
Note: The system update I performed on the tablet did not update the Android OS version, because it still says that the version is 4.2.2.
注意:我在平板上进行的系统更新并没有更新Android OS版本,因为它仍然说版本是4.2.2。
回答by RWIL
This issue returned in AppCompat 23.1.1
where the .internal
package was removed from the library jar.
此问题在从库 jar 中删除包的AppCompat 23.1.1
位置返回.internal
。
As suggested in the comments above (credits to the people who suggested it there), now also the proguard configuration has to change.
正如上面的评论中所建议的(感谢在那里提出建议的人),现在 proguard 配置也必须改变。
To get the answer suggested above working again, try adding these lines to your proguard files:
要使上面建议的答案再次起作用,请尝试将这些行添加到您的 proguard 文件中:
#FOR APPCOMPAT 23.1.1:
-keep class !android.support.v7.view.menu.*MenuBuilder*, android.support.v7.** { *; }
-keep interface android.support.v7.* { *; }
In stead of the old fix:
而不是旧的修复:
#FOR OLDER APPCOMPAT VERSION:
-keep class !android.support.v7.internal.view.menu.*MenuBuilder, android.support.v7.** { ; }
-keep interface android.support.v7.* { *; }
回答by android developer
According to the last posts of the bug-report, this should be fixed on the new version of the support library (24.0.0) : https://code.google.com/p/android/issues/detail?id=78377#c374
根据错误报告的最新帖子,这应该在支持库的新版本(24.0.0)上修复:https: //code.google.com/p/android/issues/detail?id =78377 #c374
Someone even claimedit fixed it.
有人甚至声称它修复了它。
This version is available since last month, so you should update to it.
这个版本从上个月开始可用,所以你应该更新它。