2017-08-15 155 views
2

当设置Proguard的导致崩溃

minifyEnabled真正

useProguard

在我gradle这个我得到这样一个NullPointerException真:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jafari.farhad.ubazdidcontroller/com.jafari.farhad.ubazdidcontroller.LoginActivity}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.access$600(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5041) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
    at com.jafari.farhad.ubazdidcontroller.LoginActivity.onCreate(Unknown Source) 
    at android.app.Activity.performCreate(Activity.java:5104) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)  
    at android.app.ActivityThread.access$600(ActivityThread.java:141)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)  
    at android.os.Handler.dispatchMessage(Handler.java:99)  
    at android.os.Looper.loop(Looper.java:137)  
    at android.app.ActivityThread.main(ActivityThread.java:5041)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:511)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)  
    at dalvik.system.NativeStart.main(Native Method)  

摇篮是:

buildTypes { 
    debug { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
    release { 
     minifyEnabled true 
     useProguard true 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 

,同时通过StackOverflow的阅读中,我发现了一些设置,像这样的proguard-rules.pro文件:

 
    -dontusemixedcaseclassnames 
    -dontskipnonpubliclibraryclasses 
    -verbose 


    -dontoptimize 
    -dontpreverify 

    -keepattributes *Annotation* 
    -keep public class com.google.vending.licensing.ILicensingService 
    -keep public class com.android.vending.licensing.ILicensingService 

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

    # 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); 
    } 

    -keepclassmembers class * implements android.os.Parcelable { 
     public static final android.os.Parcelable$Creator CREATOR; 
    } 

    -keepclassmembers class **.R$* { 
     public static ; 
    } 

    -dontwarn android.support.** 

    # Understand the @Keep support annotation. 
    -keep class android.support.annotation.Keep 

    -keep @android.support.annotation.Keep class * {*;} 

    -keepclasseswithmembers class * { 
     @android.support.annotation.Keep ; 
    } 

    -keepclasseswithmembers class * { 
     @android.support.annotation.Keep ; 
    } 

    -keepclasseswithmembers class * { 
     @android.support.annotation.Keep (...); 
    } 

当我确定停用minifyEnabled寄托都工作正常。 Thancks

+0

,什么是你的ProGuard文件? –

+0

是的,发送它? @MatiasOlocco – handerianous

+0

显示proguard文件和完整的gradle.build文件 –

回答

0

您可以在Proguard的

-keep class com.jafari.farhad.ubazdidcontroller.LoginActivity** 

使用此代码,但这样会导致Proguard的忽略LoginActivity类,你的代码不会混淆。

最好的方法是查看哪些元素已将此创建引入Proguard。

+0

虽然这是真的,但他没有发布文件,这将启用LoginActivity,但不是其他的... –

0

我还没有看到你的ProGuard的文件,但我敢肯定,加入这样的:

-keep class com.jafari.farhad.ubazdidcontroller.** { *; } 

将解决这个问题。

一些文档in here

以所添加的proguard的文件,它应该是:

-keep class com.jafari.farhad.ubazdidcontroller.** { *; } 
-dontusemixedcaseclassnames 
-dontskipnonpubliclibraryclasses 
-verbose 


-dontoptimize 
-dontpreverify 

-keepattributes *Annotation* 
-keep public class com.google.vending.licensing.ILicensingService 
-keep public class com.android.vending.licensing.ILicensingService 

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

# 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); 
} 

-keepclassmembers class * implements android.os.Parcelable { 
public static final android.os.Parcelable$Creator CREATOR; 
} 

-keepclassmembers class **.R$* { 
public static ; 
} 

-dontwarn android.support.** 

# Understand the @Keep support annotation. 
-keep class android.support.annotation.Keep 

-keep @android.support.annotation.Keep class * {*;} 

-keepclasseswithmembers class * { 
@android.support.annotation.Keep ; 
} 

-keepclasseswithmembers class * { 
@android.support.annotation.Keep ; 
} 

-keepclasseswithmembers class * { 
@android.support.annotation.Keep (...); 
} 
+0

谢谢你回答matias,但这个包是重要的,我想proguard它:) – handerianous

+0

但是这就是... –

+0

你有一个标准的proguard文件吗? – handerianous