Android应用程序在Proguard之后崩溃

14
应用程序在使用由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.** { *; }

抱歉,由于代理限制,无法访问pastebin,但nateZor的答案似乎是在这种情况下要检查的第一件事。 - Niko
请查看此链接:http://pastie.org/8725071 - John
您能将其添加到您的原始帖子中吗? - Niko
在原帖中添加了"produard"。 - John
我的问题:在onCreateOptionsMenu中出现了java.lang.NullPointerException。 - Eric JOYÉ
显示剩余2条评论
1个回答

25
在您的Proguard配置中,您已经注释掉了无法找到的类的保留(keep)代码。
#-keep public class android.support.v7.widget.ShareActionProvider

你尝试取消注释并重新构建了吗?

编辑:既然那个方法没有解决你的问题,也许可以尝试这里描述的通用方法:

Android Proguard配置v7支持库ActionBar

具体来说,请尝试添加以下内容:

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

如果这样做可以的话,您可以使其更具体,因为这显然会保留所有支持库类,即使您不使用其中的某些类。


我尝试取消注释这行代码,但是出现了相同的问题。 - John
谢谢,问题已解决,我在原帖中添加了解决方案,请忽略Proguard的支持库。 - John

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接