Hong
Hong

Reputation: 18501

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

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?

Upvotes: 9

Views: 797

Answers (1)

vishal_ratna
vishal_ratna

Reputation: 116

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.

Upvotes: 0

Related Questions