preface

When Gradle plugin version reaches 3.4.0 or above, R8 will be enabled for code optimization by default. In the past, we used to restore stack information by using proGuardgui.bat which comes with SDK, but this method is invalid after R8 confusion is enabled. The reason is that R8 does all sorts of internal code optimizations such as inline operations and line number mapping, where the line number thrown in the stack information is not the actual crash location……

How to solve it?

Let’s take a look at the exception message thrown after release obfuscation is enabled

As you can see, we actually threw a NullPointerException on line 45, but the stack information in Logcat was thrown on line 8, and clicking on the IDE led us directly to the top of the package, so we couldn’t locate the actual location.

There are two solutions:

  1. Compare the mapping file generated after the confusion and find out the corresponding true implementation number, as shown in the figure below, it can be seen that line 8 corresponds to line 45.

  1. Download the R8 jars of confusion, point at the top of the TGZ can begin download, need science online (android.googlesource.com/platform/pr…). In this folder you can see the original Jar packages used by ProGuard. In case you need to make a backup of the files that will be replaced. When the replacement is complete, open the folder where proGuardgui.bat was used before, and proGuardgui.bat is no longer available. You must use the command line to resolve the confusion, which is not complicated. The steps are as follows:

    • Create a stacktrace. TXT file to copy the stack information you want to unscramble, or if you have your own stackTrace files you can dump them directly into the folder.

    • Copy the mapping file to the folder

    • Bat -verbose mapping.txt stacktrace. TXT > out. TXT. Press Enter to generate an out. TXT file in the current folder. This is where the actual stack information is stored, as shown in the figure below.

The last

If you want to disable R8 and use Proguard, add android.enableR8=false to gradle.properties. / / proGuardGui. Bat: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / proGuardGui: / / This is if you have a backup of the old ProGuard.jar and retrace.jar.