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

Gradle Kotlin Buildscripts

Avatar for Jayashankar Jayashankar
September 28, 2019

Gradle Kotlin Buildscripts

Gradle has started supporting kotlin for its build scripts
This presentation describes the pros and cons and helps to drive towards writing kotlin DSL for Android project.

Please follow code samples in below links
Gradle Kotlin Sample: https://github.com/jayashankar987/Gradle_scripts_kotlin
Gradle Groovy with Kotlin DSL (BuildSrc): https://github.com/jayashankar987/GradleGroovyBuildSrc

Avatar for Jayashankar

Jayashankar

September 28, 2019

Other Decks in Programming

Transcript

  1. Supports Multiple Ecosystems • Java, Kotlin, Scala etc • C,

    C++, Swift • Android • Python, Go etc
  2. How gradle works Build scripts Configuration of tasks Execution of

    tasks Gradle Engine: resolves dependencies, work avoidance
  3. Plugins Reusable and configurable tasks Configurable gradle extensions Contribute: Core

    Plugins: Java, maven-publish etc.. Community Plugins: Android, kotlin, golang etc.. Models to configure in build scripts using DSL
  4. Android Library plugins { id("com.android.application") id("kotlin-android") id("kotlin-kapt") id("kotlin-android-extensions") } android

    { compileSdkVersion(28) defaultConfig { applicationId = "com.jay.launcherdemo" minSdkVersion(25) targetSdkVersion(28) versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" }
  5. Beginning of Gradle Help developers for easy declaration of elements

    How to generate build using tasks Provided extensible DSL Language
  6. Problems Tooling difficulty for IDEs (Dinamacity, Metaprogramming) Impact due to

    missing tool Stuck with no solution Note: other platform/language developers had issues working with groovy
  7. Solution Avoid surprise at runtime • More checks at compile

    time (statically typed) • Avoid trial and error • Performance issue due to dynamism IDE support to understand and write build logic Developer experience • Writing build logic • Identify and fix build issues (Type Safe Logic)
  8. Migration Strategies • Go through a step by step process

    • Doesn’t block team • Prepare your build to migrate easily • Making groovy scripts closer to kotlin • Unify quotes, rewrite properties / functions • Apply gradle fundamentals and best practices (BuildSrc, plugins{}) • Let kotlin logic drive groovy logic
  9. Key Points in kotlin scripts • Organise build logic •

    Apply Gradle fundamentals • Use best practices
  10. Kotlin DSL Performance • Consumes time only in configuration phase

    • Build Configuration when nothing has changed can take up to only a second • If not, check for Gradle Performance Guide • Gradle help https://github.com/gradle/gradle/issues/1628 • Performance compared to Groovy DSL
  11. Kotlin DSL vs Groovy DSL Kotlin can be slower Script

    compilation Can be even more for large projects (can be up to a minute more) • During first use (checkout, pulls, switching branches) • Changes in build configurations and buildSrc
  12. Gradle Build Cache Script compilation can be cached in Gradle

    Build Cache org.gradle.caching = true in gradle.properties or --build-cache on command line
  13. Improve performance • Parallel Compilation (org.gradle.parallel = true) • Compilation

    avoidance • Faster kotlin compilations • Optimisations Project with more than one second for build configuration without any change needs to look in for optimizing scripts