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

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. 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") } } }
  2. 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" } }
  3. 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" } }
  4. - 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") + }
  5. :app :agenda :shared object Versions { val library = "1.0"

    // ... } object Dependencies { val library = "my.package:library:${Versions.library}" // ... } :buildSrc → src/main/kotlin → Dependencies.kt
  6. :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
  7. :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
  8. :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
  9. :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
  10. :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
  11. 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