Does proguard automatically converts enums to integer for memory optimization or I have to configure it to do this? If I do have to configure what is the configuration?
2 Answers
The optimization is listed on ProGuard's optimizations page. It appears to be one of the default optimizations, but it (like other optimizations) can be specified explicitly if you need more control (e.g. disabling all class/*
optimizations aside from enum unboxing).
class/unboxing/enum
Simplifies enum types to integer constants, whenever possible.
-
1I just tried this and decompiled the code, but a simple enum class (only enums, no fields) still appears as an enum instead of an int. Is there anything I'm doing wrong? I'm using the 'proguard-android-optimize.txt' file. Commented Dec 22, 2017 at 22:26
-
@ThomasVos Note that that file has a default specification to keep static class members on enums, which may interfere with unboxing. I'd try removing that first, as long as you don't use the
values()
method orString
conversion (which pretty much limits you to equality checks). Commented Dec 22, 2017 at 22:30 -
2Thanks for the reply. I already removed those lines by copying the file into my source code, and remove the android proguard rules file in gradle. However, the enums still appear in the decompiled code. Commented Dec 22, 2017 at 22:35
-
@ThomasVos I also tried to verify enum conversion and for the first time it failed. But i managed to force a conversion. Look at this repository github.com/maxim-pandra/ProguardEnumIntDefTest Commented Sep 27, 2018 at 10:23
-
@MaksimTuraev What do you mean by "force a conversion"? I've checked out that repo, and it seems like it's still not converting enums to ints.– yiatiCommented Oct 15, 2018 at 15:36
Proguard Settings
The Proguard needs to have the configuration 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.
Optimization
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 the ints at runtime!