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

Gradle + Kotlin DSL = ⚡🚀

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

B6bf611a9e4f70a8455cb175339e0587?s=128

Gerard

June 14, 2019
Tweet

Transcript

  1. Gradle + Kotlin DSL = ⚡" @GerardPaligot

  2. What is a DSL? Domain Specific Language

  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") } } }
  4. Promises Why should I care about it?

  5. Statically typed type-safe DSL

  6. IDE experience

  7. Interoperability with existing scripts

  8. Maximum readability

  9. 100% Kotlin

  10. Migration Groovy to Kotlin

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

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

  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" } }
  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" } }
  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") + }
  17. include(":app", ":agenda", ":shared") rootProject.name = "DevFestLille" :app :agenda :shared :buildSrc

    settings.gradle > settings.gradle.kts
  18. :app :agenda :shared :buildSrc → build.gradle.kts plugins { `kotlin-dsl` }

    repositories { jcenter() }
  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
  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
  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
  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
  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
  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
  25. Personal Feedback Pros & Cons

  26. # 100% Kotlin

  27. # 100% Kotlin API incompatible $

  28. % IDE experience

  29. % IDE experience New module Groovy only &

  30. ' Refactoring code

  31. ' Refactoring code Autocomplete slow (

  32. ) Official documentation

  33. ) Official documentation Not many blog posts *

  34. So what? Should I migrate to Kotlin?

  35. Thank you! @GerardPaligot

  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