proguard icon indicating copy to clipboard operation
proguard copied to clipboard

`code/simplification/object` optimisation causing `NullPointerException`

Open remi-sf opened this issue 1 year ago • 0 comments

I'm experiencing unexpected behaviour running Proguard 7.4.0.

My project uses Gson to deserialise a simple JSON file to an object. To achieve this, the code uses several objects containing fields annotated with @com.google.gson.annotations.SerializedName. Those fields then don't need to be initialised or set within the code.

I have referred to an example proguard config from the Gson repo: https://github.com/google/gson/blob/570d91194e223132982d56b6fa499af15fd7b1ea/examples/android-proguard-example/proguard.cfg

When I run the application, reading the response JSON and deserialising it to an object, I expect to be able to access every field in the object. However, Proguard modifies several methods to throw null at the end, which naturally causes a crash when I try to e.g. print out the deserialised object.

java -jar proguard-issue/target/proguard-issue-0.0.1-SNAPSHOT.jar

Exception in thread "main" java.lang.NullPointerException: Cannot throw exception because "null" is null
        at uk.co.screamingfrog.d.toString(Unknown Source)
        at java.base/java.lang.String.valueOf(String.java:4461)
        at uk.co.screamingfrog.e.toString(Unknown Source)
        at java.base/java.lang.String.valueOf(String.java:4461)
        at uk.co.screamingfrog.Main.main(Unknown Source)

This issue goes away if you add -optimizations !code/simplification/object to the Proguard config.

My example project: proguard_issue.zip

The result when I run mvn package on my machine: proguard-issue-0.0.1-SNAPSHOT.jar.zip

To reproduce:

  • Update the path to the Java 21 JDK in jdk.home for each pom.xml
  • Run the following:
mvn clean package
java -jar proguard-issue/target/proguard-issue-0.0.1-SNAPSHOT.jar

Is this expected behaviour for this optimisation? If so, is there a way to remove the optimisation only for fields annotated as in this example?

remi-sf avatar May 03 '24 15:05 remi-sf