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

Android Kotlin - Annotation Processors Frameworks

JoseLuisRF
November 08, 2017

Android Kotlin - Annotation Processors Frameworks

This presentantion it's about how to configure your android project using Kotlin 1.1.51, Gradle 3.0.0 and Android Studio 3.0 in order to support frameworks like Dagger2, Android Data Binding and Room persistence becaues all of them uses annotation processing.

Demo: https://github.com/JoseLuisRF/mylyricsappjlrf

JoseLuisRF

November 08, 2017
Tweet

More Decks by JoseLuisRF

Other Decks in Programming

Transcript

  1. Annotation Processors! There are many popular android frameworks which uses

    annotation processing such as Dagger2, Data Binding, Room Persistence Library and so on.
  2. Annotation processing with Kotlin Annotation processors are supported in Kotlin

    with the kapt compiler plugin. Apply the kotlin-kapt Gradle plugin: apply plugin: 'kotlin-kapt'
  3. Annotation processing with Kotlin Or you can apply it using

    the plugins DSL: plugins { id "org.jetbrains.kotlin.kapt" version "1.1.51" } Then add the respective dependencies using the kapt configuration in your dependencies block: dependencies { kapt 'groupId:artifactId:version' }
  4. Migrate Annotation Support Replace annotationProcessor with kapt Replace androidTest with

    kaptAndroidTest Replace test with kaptTest By default, Kapt replaces every unknown type (including types for the generated classes) to NonExistentClass. To enable error type inferring in stubs: kapt { correctErrorTypes = true }
  5. 1. Intro How to configure your android project using kotlin

    and java as programming languages. ➔ Android 3.0 Use the latest AS version ➔ Gradle 3.0.0 Update your gradle dependency ➔ Kotlin 1.1.51 Target to this kotlin version
  6. 2. Frameworks Let’s see how to use android frameworks such

    as Dagger2, Data Binding and Room Persistence Library. ➔ Dagger 2 For dependency Injection ➔ Room For storing data into the database ➔ Data Binding Use this to bind your views
  7. Dagger2 Dagger is a dependency injection framework. As in Java,

    you use @Inject to annotate the constructor used by Dagger to create instances of a class. Kotlin has a short syntax for declaring a property and a constructor parameter at the same time. To annotate the constructor, use the constructor keyword explicitly and put the @Inject annotation before it:
  8. Dagger2 Example class MusicRepositoryImpl @Inject constructor( val musicCloudDataSource: MusicCloudDataSource, val

    musicDiskDataSource: MusicDiskDataSource, val dataMapper: MusicDataMapper) : MusicRepository { … }
  9. Dagger2 In your application build.gradle you have add the following

    lines in the dependencies section. implementation "com.google.dagger:dagger:2.12" ... kapt "com.google.dagger:dagger-compiler:2.12"
  10. Room Persistence Library The Room persistence library provides an abstraction

    layer over SQLite to allow fluent database access while harnessing the full power of SQLite. To add it to your project, open the build.gradle file for your project (not the ones for your app or module) and add the highlighted line as shown below:
  11. Room Persistence Library • For Room, add: ◦ implementation "android.arch.persistence.room:runtime:1.0.0"

    ◦ annotationProcessor "android.arch.persistence.room:compiler:1.0.0" ◦ For testing Room migrations, add: ▪ testImplementation "android.arch.persistence.room:testing:1.0.0" ◦ For Room RxJava support, add: ▪ implementation "android.arch.persistence.room:rxjava2:1.0.0"
  12. Android Data Binding The Data Binding Library allows you to

    bind your application data to the layouts in a concise way. You enable the library using the same configuration as in Java:
  13. Android Data Binding You have to use the kotlin annotation

    processor keyword in gradle kapt "com.android.databinding:compiler:3.0.0"
  14. KaptError: Error while annotation processing Kotlin errors related to annotation

    processing are displayed in the Gradle Console. When any of your annotation tags are missing in your code the kapt won’t work so watch out!.
  15. Finding the issue 1) Look into the Gradle Console 2)

    Look for a legible error like “@Provides methods can only be present within a @Module or @ProducerModule” 3) Rebuild project after you solved it
  16. You can find all these information in the following links.

    Google it please.. • https://kotlinlang.org/docs /reference/kapt.html • https://kotlinlang.org/docs /tutorials/android-framew orks.html#data-binding • https://google.github.io/da gger// • https://developer.android.c om/topic/libraries/architec ture/room.html • https://developer.android.c om/topic/libraries/data-bin ding/index.html
  17. Bye :) …. And Thank you all. Twitter: @JoseLuisRF93 Github:

    https://github.com/JoseLuisRF/myl yricsappjlrf