Annotation Processing: Supercharge Your Development | raywenderlich.com

Annotation processing is a powerful tool for generating code for Android apps. In this tutorial, you’ll create one that generates RecyclerView adapters.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/8574679-annotation-processing-supercharge-your-development

For the Where to Go From Here? section I would expect a link to other tutorials about packaging the generated modules. While having a processor is cool, how do you reuse one? Do you need to create two submodules and copy&paste the files everywhere?

How would the packaging work, a .jar or .aar file? Maybe some gradle magic? How can you make the packaged processor automatically import kotlinpoet?

Hi! Reusing an AP is just like reusing any other dependency - either pack and ship it directly (with :install Gradle command) or host is on a remote repo (like Jitpack or BinTray).

A JAR would be created if the modules were pure JVM libs, which these are not since they have Android-specific dependencies (RecyclerViews and such), hence the resulting package is an AAR (not that it really matters, though). The package processor will always automatically have KotlinPoet as it is specified as its dependency.

Hello @gordan-glavas ! I have some annoying problem when trying to use javax Annotation Processor related code.
I’ve described it here: https://stackoverflow.com/q/62485630/5434346

It is about ability to compile the project, but IDE treat some code as unresolved reference error

WHAT IS IMPORTANT:
When I created new libary module “autoadapter-process” by default Android Studio created it with this implementation:

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

To be able to compile the project I replaced it to:

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

Maybe you know, or anyone here, what can be the issue?

@gordan-glavas Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi! I see that you’ve answered your own question by now on SO. My initial guess would be that JDK version shouldn’t affect the availability of java.io nor anything javax, so I figured it’d be a caching issue. Have you tried clearing caches manually from AS, or it didn’t affect jdk.table.xml?

Hi Gordan, thanks for the nice article.

When I update Kotlin version from 1.3.61 to 1.4.30 the bind fun generated for PersonAdapter changes from item.name to item.getName.

Do you have any clue how to fix this?

Yeah, they seem to have mangled something with property naming after Kotlin 1.4. You can solve this by changing:

  1. ViewHolderBinding.kt → @Target to AnnotationTarget.FIELD
  2. Person.kt add field: as use-site for both properties: @field:ViewHolderBinding(R.id.name).
  3. Processor.kt remove line 85 and just use elementName instead of fieldName: ViewHolderBindingData(fieldName, viewHolderBinding.viewId).

Hi Gordan,

Thanks for the fast reply.

I was trying only the step 3.

Already changed the code and it worked like charm, thanks a lot =]

Very best regards

Hello Gordan.
I noticed that when I use a resource id as an annotation class attribute(e.g. @AdapterModel(R.layout.layout_person)) then Android Studio IDE displays warning: “Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them as annotation attributes”. I wonder if there is some workaround for this issue.
Best Regards