4

When starting to build my first code generation annotation, I've found I can't generate Android classes, such as SharedPreferences, since I start with a Java Library module in order to extend AbstractProcessor. I'm using kotlinpoet to generate my class, but need to create a property that is of type SharedPreferences.Editor which doesn't seem to be supported. I'm trying to something like the following:

val editorProperty = PropertySpec.builder("editor", android.content.SharedPreferences.Editor)

but this fails since the android package is not available. Does anyone know a workaround for this or is it just not possible?

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
James B
  • 447
  • 3
  • 15

1 Answers1

3

You can simply use

PropertySpec.builder("editor",ClassName("android.content", "SharedPreferences.Editor"))

as kotlin poet doc says - Type names are dumb identifiers only and do not model the values they name.

Yrii Borodkin
  • 772
  • 5
  • 7
  • 1
    Didn't even think about `ClassName`. And to quote the docs: "The `ClassName` type is very important, and you'll need it frequently when you're using KotlinPoet." haha! Thank you! – James B Feb 21 '20 at 19:22