What does "-keepattributes SourceFile,LineNumberTable" mean for the new Android code shrinker R8?
Asked Answered
S

1

9

The auto-generated proguard-rules.pro has the following:

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

I have had this commented out for years and it worked perfectly until Android Studio 3.4 that uses R8 instead of ProGuard by default.

Let's use the following example:

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
    at net.foo.anroid.Foo.wb.d(SourceFile:453)
    at net.foo.anroid.Foo.wb.a(SourceFile:213)
    at net.foo.anroid.Foo.wb.n(SourceFile:103)
    at net.foo.anroid.Foo.qa.run(Unknown Source:2)
    at java.lang.Thread.run(Thread.java:764)

The line numbers (e.g. 453, 213...) used to be the actual lines numbers in a java code source file when ProGuard was used, but they are not anymore with R8. Even using the officially suggested de-obfuscating tool ReTrace does not restore the original line numbers.

What does "-keepattributes SourceFile,LineNumberTable" do exactly?

Shied answered 8/5, 2019 at 12:19 Comment(0)
I
0

When you obfuscate the code using R8, what happens is the names are replaced with smaller names like a,b,1 etc. The resulting impact is the lines of code in the class will reduce, because of lesser no of characters.

Now if you see the stacktrace it would be irrelevant for the modified code. As line numbers would have changed. Using the above -keepattributes, it makes sure that you get the proper line numbers in stack trace.

See the docs say The LineNumberTable attribute is needed for disambiguating between optimized positions inside methods.

Intercom answered 2/8, 2022 at 13:50 Comment(2)
The line numbers are not correct. Not sure what "disambiguating between optimized positions inside methods." means.Shied
Suppose you have a class and due to the long method and variable names, the code statement is split into multiple lines, after obfuscation it would reduce the number of lines as the names would have been shortened. So the no of lines in the class would reduce and older stack trace will not be relevant. Hope this answers your query.Intercom

© 2022 - 2024 — McMap. All rights reserved.