Proguard后Android应用程序崩溃

Posted

技术标签:

【中文标题】Proguard后Android应用程序崩溃【英文标题】:Android application crashes after proguard 【发布时间】:2014-03-09 21:06:28 【问题描述】:

proguard 后应用程序崩溃(eclipse IDE 生成的proguard)。

logcat 堆栈跟踪

W/SupportMenuInflater(13657): Cannot instantiate class: android.support.v7.widget.ShareActionProvider
W/SupportMenuInflater(13657): java.lang.ClassNotFoundException: android.support.v7.widget.ShareActionProvider
W/SupportMenuInflater(13657):   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime(13657): FATAL EXCEPTION: main
E/AndroidRuntime(13657): java.lang.NullPointerException
E/AndroidRuntime(13657):    at com.mypack.app.MainActivity.onCreateOptionsMenu(Unknown Source)
E/AndroidRuntime(13657):    at android.app.Activity.onCreatePanelMenu(Activity.java:2571)`

proguard.cfg 文件的默认版本

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
#-keep public class android.support.v7.widget.ShareActionProvider

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * 
    native <methods>;


# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View 
   void set*(***);
   *** get*();


# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity 
   public void *(android.view.View);


# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * 
    public static **[] values();
    public static ** valueOf(java.lang.String);


-keep class * implements android.os.Parcelable 
  public static final android.os.Parcelable$Creator *;


-keepclassmembers class **.R$* 
    public static <fields>;


# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

`

编辑:要解决问题,您需要将此行添加到 proguard.cfg 并且没有错误。新行告诉 proguard 忽略 android.support 库。

-dontwarn android.support.v4.**
-keep class android.support.v4.**  *; 
-dontwarn android.support.v7.**
-keep class android.support.v7.**  *; 

【问题讨论】:

你能发布 proguard.cfg 吗? 这是我的 proguard-android.txt pastebin.com/icLNnUxM 抱歉,由于代理限制,无法访问 pastebin,但在这种情况下,nateZor 的回答似乎是首先要检查的。 请看这个pastie.org/8725071 您可以将其添加到您的原始帖子中吗? 【参考方案1】:

在你的 proguard 配置中,你已经注释掉了它找不到的类的保留:

#-keep public class android.support.v7.widget.ShareActionProvider

您是否尝试取消注释并重新构建?

编辑:因为这并没有解决你的问题,也许试试这里描述的全部内容:

Android Proguard configuration for the v7 Support Library ActionBar

具体来说,尝试添加:

-keep class android.support.v7.internal.**  *; 
-keep interface android.support.v7.internal.**  *; 
-keep class android.support.v7.**  *; 
-keep interface android.support.v7.**  *; 

如果可行,您可以使其更具体,因为这显然只会保留所有支持库类,即使是您不使用的类。

【讨论】:

我试图取消注释这一行,但出现了同样的问题 谢谢,问题已解决,我在原帖中添加了解决方案,只是忽略了proguard的支持库

以上是关于Proguard后Android应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

通过在Android中实现Proguard,使用JDBC在Asynctask中崩溃

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

当我在 GSON(使用枚举)上运行 Proguard 时应用程序崩溃

启用 Proguard 时,Xamarin Android 应用程序因缺少 google play 服务类而崩溃

使用 GSON 库和 ProGuard 时 Android 崩溃

应用程序使用 proguard 崩溃