27

In Java we are told to strictly avoid using enums on Android because they take up twice the memory.

Does this apply to enum class in Kotlin aswell? Will a Kotlin enum be compiled into a Java enum?

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • 8
    You might be also interested in whether you should really avoid enums on Android or not: https://stackoverflow.com/questions/29183904/should-i-strictly-avoid-using-enums-on-android and https://stackoverflow.com/questions/5143256/why-was-avoid-enums-where-you-only-need-ints-removed-from-androids-performanc – Ilya Jun 04 '17 at 03:45

3 Answers3

26

It would appear so, yes.

I created this in Kotlin:

enum class Thingies {
    Red,
    Green,
    Blue
}

And decompiled it with javap -v, and here is the header:

public final class Thingies extends java.lang.Enum<Thingies>
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_FINAL, ACC_SUPER, ACC_ENUM

Bottom line: they are identical, so you probably have to treat them the same way.

Todd
  • 30,472
  • 11
  • 81
  • 89
6

They are exactly the same thing, a Kotlin Enum is a Java JVM Enum.

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
5

You can most definitely use enums in Android now, they specifically discussed this topic in this Google I/O video: https://www.youtube.com/watch?v=IrMw7MEgADk (if you don't want to watch the whole video, then start from 11:30 ish to 14:40).

Rambo7
  • 1,175
  • 1
  • 10
  • 18
  • 1
    That video he said that it still isn't as efficient as using an int. Android Framework devs still avoid them for this reason. – phreakhead May 14 '19 at 21:19
  • 1
    @phreakhead you don't have listened until the 14:40. https://youtu.be/IrMw7MEgADk?t=858 – alexpfx Sep 21 '19 at 02:49
  • I would recommend watching from 10:15 instead if you have time for that extra minute to get the context: It was comparing dalvik and ART. – Teng-pao Yu Oct 08 '19 at 07:02
  • 2
    Yes agree, it's still maybe not as efficient as using int, but still for normal size project, using 10-50 enums will not have that huge impact on your performance and memory, so I would say use ONLY enums and be happy :) – Viktor Apoyan Oct 22 '20 at 09:35