Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Annotation Processing with Kotlin - KAPT

Annotation Processing with Kotlin - KAPT

Nearly everyone uses annotation processing copy-pasting code from github to gradle/maven, but not everyone know what that `kapt "libname"` actually does. In this talk we're going to explain what an annotation processor is, why it is used and how to build a simple annotation processor with kotlin. We're going to use KotlinPoet to generate our code with ease and take a look at most common pitfalls/errors done while building an annotation processor

Avatar for Dario Coletto

Dario Coletto

June 14, 2018
Tweet

Other Decks in Technology

Transcript

  1. Milan Kotlin Community Conf 2018 Annotation Processing with Kotlin -

    KAPT @Generate(code="kotlin", time="compile") android dev Dario Coletto @coletzLp
  2. Annotation Processing with Kotlin - KAPT • What is an

    annotation processor? • How does it work? • Why are we using (or should we use) it? • How can we build one? • Questions? 1/22
  3. What is an annotation processor? • It's a standard java/kotlin

    application... • ...that makes use of special classes and methods • Can read your code at compile time and • check for warnings/errors • generate code* based on annotations • [help] editing existing code (NOT recommended) *one can actually generate metadata, documentation, comments, even resources and any other stuff 2/22
  4. How does it work? • Processing is divided in rounds

    • Every round is a loop of operations • Round 1 will process existing code • Next round(s) will process generated code (if needed) • Every round will try to process previously processed code 4/22
  5. Round 1 Round 2 Source file Processing file Completed file

    Image credits: Mert Şimşek (iammert @ Medium) 5/22
  6. How does it work? • During every round every processor

    will be executed • Processors must declare which annotation they can process • A processor can claim a set of annotations after processing it • Processor execution order is sort-of-random 6/22
  7. ✓ ✓ Round 1 Processor 1 Processor 2 Processable annotations

    set Non-processable annotations set Processed annotations set ✓ Processor 3 Generated files ? ? ? ? ✓ ✓ ✓ ✓ ✓ ✓ ✓ ? ? 7/22
  8. Why are we using (or should we use) it? •

    Because of DRY principle • Because it runs at compile time... • ...so code is faster (than reflection) • ...so code is safer (no crash at runtime) • The processor is not packed inside our real code 8/22
  9. How can we build one? Actually it's pretty easy, let's

    create a Butterknife-like injector 9/22
  10. • setup the project • create an annotation • create

    a class extending AbstractProcessor • register the processor • write your own Processor code How can we build one? KCCBinder, bind XML views to Kotlin objects 10/22
  11. Setup the project 1) Create needed modules 2) Apply kotlin

    plugin 3) Apply kotlin + kotlin-kapt plugins, then add dependencies 12/22
  12. Annotation Indicates which type of object can be annotated Name

    of the annotation that will be created (@KCCView) Indicates if the annotation is retained at runtime, compile time or never Annotation parameters (@KCCView(value=123)) 14/22