16

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?

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
karma
  • 1,282
  • 18
  • 23

2 Answers2

24

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.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • 1
    I 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. – Thomas Vos Dec 22 '17 at 22:26
  • @ThomasVos Note that that file has a default specification to [keep static class members on enums](https://android.googlesource.com/platform/sdk/+/master/files/proguard-android-optimize.txt#48), which may interfere with unboxing. I'd try removing that first, as long as you don't use the `values()` method or `String` conversion (which pretty much limits you to equality checks). – Jeff Bowman Dec 22 '17 at 22:30
  • 2
    Thanks 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. – Thomas Vos Dec 22 '17 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 https://github.com/maxim-pandra/ProguardEnumIntDefTest – Maksim Turaev Sep 27 '18 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. – yiati Oct 15 '18 at 15:36
  • @yiati did you ran debug or release build? proguard in my repo configured to debug build. Then with apk analizer i could see converted enums. Please check build type. And tell if this was the problem. Are you using apk analyzer to see see convertion? – Maksim Turaev Oct 15 '18 at 16:43
  • @MaksimTuraev I ran both debug and release, and used the apk analyzer. In both cases the `StateEnum` class is still 458 bytes with all of the class information. – yiati Oct 15 '18 at 18:45
  • @yiati. I have updated a sample. There was a mistake I forgot to push. Now enum converts to int, but the class itself doesn't get cleaned up entirely. You can see an accepted issue here https://sourceforge.net/p/proguard/bugs/724/. – Maksim Turaev Oct 15 '18 at 20:03
  • @MaksimTuraev awesome that worked for me, thanks! I did add disable obfuscation to make it easier to confirm. – yiati Oct 15 '18 at 20:43
0

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!


Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105