更多 ProGuard 混淆配置参考 : https://www.guardsquare.com/en/products/proguard/manual/usage
前提 : proguard-rules.pro 混淆配置中配置保留行数 ;
# 保留行数
-keepattributes SourceFile,LineNumberTable
当代码混淆后 , 如果出现报错信息 , 此时报错信息全部都是被混淆过的信息, 无法从中找出可用的信息 ;
混淆后 报错信息不可读 ;
下面是截取出的混淆后的报错信息 :
2020-11-10 12:01:59.426 28142-28142/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: kim.hsl.handler, PID: 28142
java.lang.RuntimeException: Unable to start activity ComponentInfo{kim.hsl.handler/kim.hsl.handler.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'b.a.a.d b.a.a.b.a' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3897)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4076)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
Caused by: java.lang.NullPointerException: Attempt to read from field 'b.a.a.d b.a.a.b.a' on a null object reference
at b.a.a.a.<init>(Handler.java:30)
at kim.hsl.handler.MainActivity.onCreate(MainActivity.kt:24)
at android.app.Activity.performCreate(Activity.java:8085)
at android.app.Activity.performCreate(Activity.java:8073)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3870)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4076)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
报错信息 , at b.a.a.a.(Handler.java:30) , 报错的是混淆后的信息 , 可读性很差 ;
前提 : proguard-rules.pro 混淆配置中配置保留行数 ;
# 保留行数
-keepattributes SourceFile,LineNumberTable
Proguard 混淆后 , 会提供一个 原始代码 与 混淆过的代码 的对应关系文件 mapping.txt ;
该文件存放在 app\build\outputs\mapping\debug 目录下 ;
上面的报错信息 :
Caused by: java.lang.NullPointerException: Attempt to read from field 'b.a.a.d b.a.a.b.a' on a null object reference
at b.a.a.a.<init>(Handler.java:30)
在 mapping.txt 查找 b.a.a.a 类 , 对应的原始类是 kim.hsl.handler.Handler ;
解读 mapping.txt 文件中的信息 :
kim.hsl.handler.Handler -> b.a.a.a:
kim.hsl.handler.MessageQueue mQueue -> a
13:31:void <init>() -> <init>
39:42:void sendMessage(kim.hsl.handler.Message) -> a
kim.hsl.handler.Handler 类名被混淆成了 b.a.a.a ,
kim.hsl.handler.MessageQueue mQueue 成员名称被混淆成了 a ,
39:42:void sendMessage(kim.hsl.handler.Message) 方法名称被混淆成了 a ;