My Proguard release builds are stripping out my initialization values of my member fields. This is causing my default values to get lost, resulting in different behavior between my debug and release builds. How can I stop this happening?
Here's my source code
public class MyClass implements Serializable {
private static final long serialVersionUID = 1L;
@SerializedName("myField")
private boolean myFieldEnabled = true;
public boolean isMyFieldEnabled() {
return myFieldEnabled;
}
public void setMyFieldEnabled(boolean myFieldEnabled) {
this.myFieldEnabled = myFieldEnabled;
}
...
The obfuscated code looks like this (the setters and getters are removed):
public final class ai implements Serializable
{
private static final long serialVersionUID = 1L;
@z(x="myField")
public boolean myFieldEnabled;
Here's a snippet from my build.gradle:
release {
minifyEnabled true
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'custom-proguard-rules-optimize.pro'
}
Here are the key Proguard optimization rules from my custom rules (from custom-proguard-rules-optimize.pro):
# Disable the "code/allocation/variable" optimizations to workaround a
# Proguard bug. Source: http://stackoverflow.com/a/7587680/112705
-optimizations !code/simplification/arithmetic,field/removal/writeonly,!class/merging/*,!code/allocation/variable
-allowaccessmodification
Here are the key Proguard optimization rules from the Android default rules that we are using (from proguard-android-optimize.txt):
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 5
I checked the Proguard optimizations documentation and I couldn't see an optimization or obfuscation option that covered this.
Updates
- Adding
!field/*
into our optimizations did not help - the field still did not seem to get initialized. Hopefully this optimization was already being picked up from the Android default Proguard rules... - Removing
field/removal/writeonly
did not help. - Adding
!code/simplification/field
did not help.