Android 应用因混淆规则而崩溃

Android app crash with proguard-rules

我的应用程序在不应用 pro-guard-rules 的情况下工作正常,但出于安全目的,我必须在我的应用程序中应用 pro-guard-rules 。 我在应用规则并尝试 运行 我的应用程序时遇到这些错误。

 FATAL EXCEPTION: main
                                                                    Process: com.nearely.nearelysales, PID: 4275
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nearely.nearelysales/com.nearely.nearelysales.activities.MainActivity}: java.lang.NullPointerException
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                                        at android.app.ActivityThread.access0(ActivityThread.java:135)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:136)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                     Caused by: java.lang.NullPointerException
                                                                        at com.nearely.nearelysales.activities.MainActivity.k(Unknown Source)
                                                                        at com.nearely.nearelysales.activities.MainActivity.onCreate(Unknown Source)
                                                                        at android.app.Activity.performCreate(Activity.java:5231)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                        at android.app.ActivityThread.access0(ActivityThread.java:135) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                        at dalvik.system.NativeStart.main(Native Method)  

这是我的 progurd-rules 文件:

proguard-rules.pro

-injars      libs
-outjars     bin/classes-processed.jar
# -libraryjars C:\dev\android-sdk\platforms\android-23\android.jar

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

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

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepnames class * { @butterknife.InjectView *;}

-keep class javax.ws.rs.** { *; }
-dontwarn org.immutables.gson.**

-dontwarn android.test.**
-dontwarn org.junit.**

似乎您使用过 volley 并且使用 proguard 使您的应用程序崩溃。 您可以将此行写入您的 proGuard.

-keep class org.apache.commons.logging.**

在您的应用程序中启用 Proguard 后,所有的 Proguard 自定义规则取决于您是否在应用程序初始阶段或中期或后期启用了 Proguard。

  1. 初始

It Means that if you integrate any third party library in your app , the rules gets set by its own if the library provides or supports proguard.

2.Middle 及更高版本

In Middle if you set up proguard in your app and you have already integrated many third party libraries , it means that every library is integrated without rules. Then at that you need to setup custom rules in your proguard-rules.pro file.

很多应用崩溃的情况depends.one是因为你使用的Pojo类或模型类被proguard混淆了,所以你需要在proguard中设置规则-rules.pro 文件 喜欢:

-keep class * {
     public private *;
}

或以其他方式根据要求使用以下代码

-dontwarn com.squareup.okhttp.**
-ignorewarnings
-keep class * {
    public private *;
}

-keep class com.google.android.gms.* { *; }
-dontwarn com.google.android.gms.**

-dontwarn org.xmlpull.v1.**
-dontnote org.xmlpull.v1.**
-keep class org.xmlpull.* { *; }

#retRofit
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
# Orm
-keep class * extends com.raizlabs.android.dbflow.config.DatabaseHolder.* { *; }
-keepclassmembers class * {
    native <methods>;
}
-dontwarn okio.**
-dontwarn com.squareup.okhttp.**
-dontwarn okhttp3.**
-dontwarn javax.annotation.**
-dontwarn com.android.volley.toolbox.**

# com.github.siyamed:android-shape-imageview
-dontwarn android.support.v7.**
-keepattributes *Annotation,Signature
-dontwarn com.github.siyamed.**
-keep class com.github.siyamed.shapeimageview.*{ *; }
-dontwarn org.xmlpull.v1.**
-dontwarn uk.co.senab.photoview.**

#Image Cropper
-keep class androidx.appcompat.widget.* { *; }



# Keep source file names, line numbers, and Parse class/method names for easier debugging
 -keepattributes SourceFile,LineNumberTable
 -keepnames class com.parse.* { *; }


 # Required for Parse
 -keepattributes *Annotation*
 -keepattributes Signature
 -dontwarn com.squareup.**
 -dontwarn okio.**


-keepattributes SourceFile,LineNumberTable
-keep class com.parse.*{ *; }
-dontwarn com.parse.**
-dontwarn com.squareup.picasso.**
-keepclasseswithmembernames class * {
    native <methods>;
}