使用ProGuard会导致ClassNotFoundException

时间:2016-02-12 09:52:05

标签: android proguard classnotfoundexception

我已经使用ProGuand进行发布版本。在启动时我收到以下错误:

Class not found when unmarshalling: [myPackage.A]
                                        java.lang.ClassNotFoundException: [myPackage.A]
                                            at java.lang.Class.classForName(Native Method)
                                            at java.lang.Class.forName(Class.java:308)
                                            at java.lang.Class.forName(Class.java:272)
                                            at android.os.Parcel.readParcelableCreator(Parcel.java:2275)
                                            at android.os.Parcel.readParcelable(Parcel.java:2239)
                                            at android.os.Parcel.readValue(Parcel.java:2146)
                                            at android.os.Parcel.readArrayMapInternal(Parcel.java:2479)
                                            at android.os.BaseBundle.unparcel(BaseBundle.java:221)
                                            at android.os.BaseBundle.getString(BaseBundle.java:918)
                                            at android.content.Intent.getStringExtra(Intent.java:5386)
                                            at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1780)
                                            at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1334)
                                            at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:4801)
                                            at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:4638)
                                            at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:142)
                                            at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3138)
                                            at android.os.Binder.execTransact(Binder.java:446)
                                         Caused by: java.lang.ClassNotFoundException: Didn't find class "[myPackage.A]" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
                                            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                            at java.lang.Class.classForName(Native Method) 
                                            at java.lang.Class.forName(Class.java:308) 
                                            at java.lang.Class.forName(Class.java:272) 
                                            at android.os.Parcel.readParcelableCreator(Parcel.java:2275) 
                                            at android.os.Parcel.readParcelable(Parcel.java:2239) 
                                            at android.os.Parcel.readValue(Parcel.java:2146) 
                                            at android.os.Parcel.readArrayMapInternal(Parcel.java:2479) 
                                            at android.os.BaseBundle.unparcel(BaseBundle.java:221) 
                                            at android.os.BaseBundle.getString(BaseBundle.java:918) 
                                            at android.content.Intent.getStringExtra(Intent.java:5386) 
                                            at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1780) 
                                            at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1334) 
                                            at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:4801) 
                                            at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:4638) 
                                            at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:142) 
                                            at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3138) 
                                            at android.os.Binder.execTransact(Binder.java:446) 
                                            Suppressed: java.lang.ClassNotFoundException: [myPackage.A]
                                            at java.lang.Class.classForName(Native Method)
                                            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                    ... 18 more
                                         Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

该类是Parcelable,因为它在某些时候使用AndroidAnnotations注入。它看起来像这样(有2个内部类也可以使用Parcelable):

public class A implements Parcelable{
  public static final Parcelable.Creator<CFPageHeader>  CREATOR ....

 static class B implements Parcelable{
  public static final Parcelable.Creator<CFPageHeader>  CREATOR ....
 }

 static class C implements Parcelable{
  public static final Parcelable.Creator<CFPageHeader>  CREATOR ....
 }

}

Pro-Guard规则如下所示:

 -optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keepattributes **

-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# - reuqired to keep JS methods active
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}
-keep class * implements android.os.Parcelable {
   public static final android.os.Parcelable$Creator *;
}
#keep libraries
-keep class com.viewpagerindicator.**  { *; }
-keep class android.support.** {*;}
-keep class com.facebook.** {*;}


# Don't warn
-dontwarn android.support.**
-dontwarn com.actionbarsherlock.internal.**
-dontwarn com.viewpagerindicator.**
-dontwarn com.androidannotations.**
-dontwarn com.fasterxml.jackson.**
-dontwarn com.google.android.gms.**
-dontwarn com.facebook.**
-dontwarn com.j256.ormlite.**
-dontwarn org.w3c.**
-dontwarn it.sephiroth.android.library.imagezoom.**
-dontwarn org.androidannotations.**
-dontwarn com.google.zxing.**


# assumenosideffects
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** i(...);
    public static *** w(...);
    public static *** e(...);
    public static *** wtf(...);
}

我尝试的是让我的包中的所有类都不被混淆,但它会抛出相同的错误。如果我关闭ProGuard,错误就消失了。我对ProGuard一点也不熟悉。我添加了keep类/ classmembers实现Parcelable但仍然相同。

0 个答案:

没有答案