带有 Android 的 ProGuard:java.lang.NoSuchMethodError:android.util.Xml.asAttributeSet

Posted

技术标签:

【中文标题】带有 Android 的 ProGuard:java.lang.NoSuchMethodError:android.util.Xml.asAttributeSet【英文标题】:ProGuard with Android: java.lang.NoSuchMethodError: android.util.Xml.asAttributeSet 【发布时间】:2013-05-04 16:53:38 【问题描述】:

当 ProGuard 被禁用时,我的应用程序可以正常运行。

启用ProGuard后,应用导出为apk并安装到模拟器中。

然后当我在模拟器中运行它时,强制关闭...

05-10 11:14:10.582: E/androidRuntime(759): FATAL EXCEPTION: main
05-10 11:14:10.582: E/AndroidRuntime(759): java.lang.NoSuchMethodError: android.util.Xml.asAttributeSet
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.actionbarsherlock.view.MenuInflater.inflate(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.g.myapp.e.onCreateOptionsMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.support.v4.app.Watson.onCreatePanelMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchInvalidateOptionsMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.support.v4.app.Fragment.setHasOptionsMenu(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.g.myapp.e.onCreate(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.support.v4.app.BackStackRecord.run(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.g.myapp.n.onTabChanged(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:391)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.widget.TabHost.setCurrentTab(TabHost.java:376)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.widget.TabHost.addTab(TabHost.java:236)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.g.myapp.n.a(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.g.myapp.MainActivity.onCreate(Unknown Source)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.app.Activity.performCreate(Activity.java:5008)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.os.Looper.loop(Looper.java:137)
05-10 11:14:10.582: E/AndroidRuntime(759):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-10 11:14:10.582: E/AndroidRuntime(759):  at java.lang.reflect.Method.invokeNative(Native Method)
05-10 11:14:10.582: E/AndroidRuntime(759):  at java.lang.reflect.Method.invoke(Method.java:511)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-10 11:14:10.582: E/AndroidRuntime(759):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-10 11:14:10.582: E/AndroidRuntime(759):  at dalvik.system.NativeStart.main(Native Method)

proguard-project.txt:

-keep class android.support.v4.app.**  *; 
-keep interface android.support.v4.app.**  *; 
-keep class com.actionbarsherlock.**  *; 
-keep interface com.actionbarsherlock.**  *; 

-keepattributes *Annotation*

-dontwarn org.w3c.dom.bootstrap.**
-dontwarn org.joda.time.**
-dontwarn org.xmlpull.v1.**

我该如何解决这个错误?

非常感谢!


更新:

在proguard-project.txt中添加一行后,应用程序可以在模拟器中运行:

-keep class *  *; 

虽然app可以运行,但我觉得这个方案可能不太好……

有更好的解决方案吗?

请帮忙。

非常感谢。

【问题讨论】:

-保持类 * *; 是徒劳的,因为它使所有类保持不变。 【参考方案1】:

在 proguard-project.txt 中添加一行:

-keep class org.xmlpull.v1.**  *; 

【讨论】:

非常感谢。一直在为 proguard 提供错误的异常而苦苦挣扎。 为什么必须这样做?多么神秘。几个小时的调试才能找到这个神秘的解决方案,多么可悲。 :(

以上是关于带有 Android 的 ProGuard:java.lang.NoSuchMethodError:android.util.Xml.asAttributeSet的主要内容,如果未能解决你的问题,请参考以下文章

带有proguard的Android应用程序在发布时崩溃

带有 proguard 的 Android ant build.xml 未在发行版中构建

无法在 Android Studio 中生成签名的 APK,因为缺少 proguard-rules.txt

使用带有 proguard 关闭力的 jsoup 关闭

dagger android支持proguard规则

Android Proguard 跳过外部 jar