$30 off During Our Annual Pro Sale. View Details »

Gradle + Kotlin DSL = ⚡🚀

Gerard
June 14, 2019

Gradle + Kotlin DSL = ⚡🚀

100% Kotlin codebase for your project? Why not?! Maybe it is time to use only one language for the codebase of our project and for all Gradle scripts. See together news about Gradle 5.0 and Kotlin DSL support production ready!

Find source code of this talk on this GitHub project: https://github.com/GerardPaligot/devfestlille-android

Gerard

June 14, 2019
Tweet

More Decks by Gerard

Other Decks in Programming

Transcript

  1. Gradle + Kotlin DSL = ⚡"
    @GerardPaligot

    View Slide

  2. What is a DSL?
    Domain Specific Language

    View Slide

  3. node(name = "resources") {
    element(name = "string") {
    attr(key = "name", value = "example")
    string(content = "I'm a bugdroid")
    }
    element(name = "plurals") {
    element(name = "string") {
    attr(key = "name", value = "one")
    string(content = "I have 1 song")
    }
    element(name = "string") {
    attr(attribute = "name" to "other")
    string(content = "I have multiple songs")
    }
    }
    }

    View Slide

  4. Promises
    Why should I care about it?

    View Slide

  5. Statically typed
    type-safe DSL

    View Slide

  6. IDE experience

    View Slide

  7. Interoperability with
    existing scripts

    View Slide

  8. Maximum readability

    View Slide

  9. 100% Kotlin

    View Slide

  10. Migration
    Groovy to Kotlin

    View Slide

  11. View Slide

  12. - implementation project(':agenda')
    + implementation project(":agenda")

    View Slide

  13. - classpath("com.android.tools.build:gradle:$versions.androidPlugin")
    + classpath("com.android.tools.build:gradle:${versions.androidPlugin}")

    View Slide

  14. android {
    defaultConfig {
    - applicationId "com.paligot.devfestlille"
    + applicationId = "com.paligot.devfestlille"
    minSdkVersion versions.minSdk
    targetSdkVersion versions.targetSdk
    - versionCode 1
    - versionName "1.0"
    - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    + versionCode = 1
    + versionName = "1.0"
    + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    }

    View Slide

  15. android {
    defaultConfig {
    applicationId = "com.paligot.devfestlille"
    - minSdkVersion versions.minSdk
    - targetSdkVersion versions.targetSdk
    + minSdkVersion(versions.minSdk)
    + targetSdkVersion(versions.targetSdk)
    versionCode = 1
    versionName = "1.0"
    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    }

    View Slide

  16. - apply plugin: "com.android.library"
    - apply plugin: "kotlin-android"
    - apply plugin: "kotlin-android-extensions"
    - apply plugin: "kotlin-kapt"
    - apply plugin: "androidx.navigation.safeargs.kotlin"
    + plugins {
    + id("com.android.library")
    + id("kotlin-android")
    + id("kotlin-android-extensions")
    + id("kotlin-kapt")
    + id("androidx.navigation.safeargs.kotlin")
    + }

    View Slide

  17. include(":app", ":agenda", ":shared")
    rootProject.name = "DevFestLille"
    :app :agenda :shared :buildSrc
    settings.gradle > settings.gradle.kts

    View Slide

  18. :app :agenda :shared
    :buildSrc
    → build.gradle.kts
    plugins {
    `kotlin-dsl`
    }
    repositories {
    jcenter()
    }

    View Slide

  19. :app :agenda :shared
    object Versions {
    val library = "1.0"
    // ...
    }
    object Dependencies {
    val library = "my.package:library:${Versions.library}"
    // ...
    }
    :buildSrc
    → src/main/kotlin
    → Dependencies.kt

    View Slide

  20. :app :agenda :shared
    buildscript {
    repositories {
    google()
    jcenter()
    }
    dependencies {
    classpath(Dependencies.androidPlugin)
    classpath(Dependencies.androidx.navigation.plugin)
    classpath(Dependencies.kotlin.plugin)
    }
    }
    :buildSrc
    build.gradle > build.gradle.kts

    View Slide

  21. :app :agenda :shared
    buildscript {
    // ...
    }
    - task clean(type: Delete) {
    - delete rootProject.buildDir
    - }
    + tasks {
    + val clean by registering(Delete::class) {
    + delete(buildDir)
    + }
    + }
    :buildSrc
    build.gradle > build.gradle.kts

    View Slide

  22. :agenda
    → build.gradle.kts
    :shared
    → build.gradle.kts
    plugins {
    // ...
    }
    android {
    compileSdkVersion(Versions.targetSdk)
    defaultConfig {
    minSdkVersion(Versions.minSdk)
    targetSdkVersion(Versions.targetSdk)
    versionCode = 1
    versionName = "1.0"
    }
    buildTypes {
    // ...
    }
    dataBinding {
    // ...
    }
    }
    :buildSrc
    :app
    → build.gradle.kts

    View Slide

  23. :agenda
    → build.gradle.kts
    :shared
    → build.gradle.kts
    plugins {
    // ...
    }
    android {
    compileSdkVersion(Versions.targetSdk)
    defaultConfig {
    // ...
    }
    buildTypes {
    - release {
    - minifyEnabled = false
    + named("release") {
    + isMinifyEnabled = false
    proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
    }
    }
    dataBinding {
    // ...
    }
    }
    :buildSrc
    :app
    → build.gradle.kts

    View Slide

  24. :app
    → build.gradle.kts
    :agenda
    → build.gradle.kts
    :shared
    → build.gradle.kts
    plugins {
    // ...
    }
    android {
    compileSdkVersion(Versions.targetSdk)
    defaultConfig {
    // ...
    }
    buildTypes {
    // ...
    }
    dataBinding {
    - enable = true
    + isEnabled = true
    }
    }
    :buildSrc

    View Slide

  25. Personal Feedback
    Pros & Cons

    View Slide

  26. # 100% Kotlin

    View Slide

  27. # 100% Kotlin
    API incompatible $

    View Slide

  28. % IDE experience

    View Slide

  29. % IDE experience
    New module Groovy only &

    View Slide

  30. ' Refactoring code

    View Slide

  31. ' Refactoring code
    Autocomplete slow (

    View Slide

  32. ) Official documentation

    View Slide

  33. ) Official documentation
    Not many blog posts *

    View Slide

  34. So what?
    Should I migrate to Kotlin?

    View Slide

  35. Thank you!
    @GerardPaligot

    View Slide

  36. References
    • DevFest Android Sample https://github.com/GerardPaligot/devfestlille-android
    • Gradle Kotlin DSL Primer https://docs.gradle.org/current/userguide/kotlin_dsl.html
    • Migration blog post https://medium.com/@stoltmanjan/migrating-gradle-to-
    kotlin-dsl-in-android-studio-3-3-18651f37227f
    • Migration blog post https://proandroiddev.com/migrating-android-build-scripts-
    from-groovy-to-kotlin-dsl-f8db79dd6737

    View Slide