proguard icon indicating copy to clipboard operation
proguard copied to clipboard

ProGuard introduces NPE into obfuscated code because it removes a null check

Open OlafRoeder opened this issue 4 years ago • 1 comments

Hello folks,

I have a situation where a final variable in a subclass is initialized in its constructor, but the superclass calls a method accessing that variable before it is initialized. Works without obfuscation because of a null check, but this very check is removed by obfuscation. SSCCE: https://github.com/OlafRoeder/ProGuardNPEFinalVarWithSetterFromParent

This situation should be detectable by some static code analysis.

Would very much appreciate a fix, but a workaround that does not involve changing code would do, too.

Greetings, Olaf

OlafRoeder avatar Feb 23 '21 22:02 OlafRoeder

It's a known issue; such use before initialization unfortunately occurs once in a while and is not entirely trivial to detect. You can keep the affected fields so their non-null initialization values are not propagated, e.g.

-keepclassmembers,allowshrinking,allowobfuscation class somepackage.SomeClass {
    somepackage.SomeType someField;
}

Alternatively, you can disable value propagation for all fields:

-optimizations !field/propagation/value

EricLafortune avatar Feb 24 '21 09:02 EricLafortune