NoClassDefFoundError的:android.support.v7.internal.view.menu.MenuBuildersupport、NoClassDefFoundError、

2023-09-12 00:19:25 作者:シ主宰丗屆︶ㄣ

有一个问题,在运行Android 4.2三星设备在Android appcompat V7库。我不断收到崩溃,并显示以下堆栈跟踪我的开发者控制台:

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:

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$5.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)

这是CustomActivity.java行215:

This is line 215 of CustomActivity.java:

PopupMenu popup = new PopupMenu(CustomActivity.this, mImageViewMenu);

的崩溃来自器件的阵列,但总是三星,始终机器人4.2

The crashes come from an array of devices, but always Samsung, and always 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:

检查Android项目的属性,确保appcompat库正确添加。 检查Java构建路径顺序和出口项目属性,确保Android的依赖和Android私家藏书被选中。 确认类包括在库(android.support.v7.internal.view.menu.MenuBuilder)。 在确认R.java位于根目录android.support.v7.appcompat。 确认AppCompat主题包含在manifest.xml活动。 清理并重建项目。

尽管有这些措施,尽管它的工作对所有其他设备和Android版本的崩溃报告还来过。

Despite these steps, and despite it working on all other devices and Android versions the crash reports still come through.

推荐答案

编辑:

这是为我工作的解决方案是(使用Proguard的)来代替这一点:

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.** { *; }

这一点:

# 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.** {*;}

幸得谷歌组: https://$c$c.google.com/p/android/issues/detail?id=78377 #138

Credit goes to the google group: https://code.google.com/p/android/issues/detail?id=78377 #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");
}

然后在活动的onCreate方法:

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.