How to keep original line numbers in stack traces with R8?

1.7k views Asked by At

I'm trying to figure out how to keep original line numbers with R8.

Doing an app with current AndroidStudio and obfuscating it with R8, and even uploading mapping.txt file to Google Play Console, the Stack Traces of the users are useless in some cases, because the lines of the crash are not the same as in the real non obfuscated file.

This is a sample, my class doesn't have 3000 lines, but the error is reported in line 3052 ( com.mypackage.activities.ManagerActivity.onCreate (ManagerActivity.java:3052) ):

Caused by: java.lang.NullPointerException: 
  at com.mypackage.activities.ManagerActivity.onCreate (ManagerActivity.java:3052)
  at android.app.Activity.performCreate (Activity.java:7136)
  at android.app.Activity.performCreate (Activity.java:7127)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1271)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2990)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3148)
  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1861)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:193)
  at android.app.ActivityThread.main (ActivityThread.java:6819)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:497)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:912)

Is there a way to get the real line number of the error with R8 and AndroidStudio? Remember that this code is already deobfuscated with mapping.txt file

1

There are 1 answers

0
sgjesse On BEST ANSWER

In order for correct retracing of obfuscated stack traces it is required to have the following in the configuration file

 -keepattributes LineNumberTable,SourceFile

See https://developer.android.com/studio/build/shrink-code#decode-stack-trace for moe information.