Search code examples
androidkotlinpoet

Android Annotation Code Generation - Android Classes


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?


Solution

  • 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.