When can Proguard optimize enums out?

Viewed 310

https://www.guardsquare.com/en/proguard/manual/optimizations says

class/unboxing/enum

Simplifies enum types to integer constants, whenever possible.

But the obvious question is, when is it possible? I assume the enum must not have fields/methods? Does it apply only to local variables or to method arguments as well?

In particular, if I have a enum with a field and a getter for this field, I could convert it to a static method switching on the enum; will this enable the optimization?

1 Answers

Optimization

Your assumption is correct, for the Proguard to optimize an enum, that enum should not have methods and associated values(fields). Proguard converts these simple enums to ints so, you get the type-safety of the enums at compile-time and the performance of ints at runtime.

It applies both to variables as well as method arguments.

So for your case where you have a field in an enum, the optimization won't apply.

Jake Wharton who worked for the Android team and others have discussed enum optimization in this Reddit post.


Proguard Settings

The Proguard settings should look like following:

minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

Note the inclusion of the proguard-android-optimize.txt file and not the proguard-android.txt file.

ProguardEnumIntDefTest is a sample project on Github that tries to find out if Proguard converts enums into ints.


Related