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

Gradle with Kotlin

Manpreet
October 14, 2018

Gradle with Kotlin

We'll first study the Kotlin-DSL by going through the language features that enable it. Next, we'll see how it's better than Groovy-DSL. In the end, we'll convert the existing Gradle script written in Groovy-DSL to Kotlin-DSL

Manpreet

October 14, 2018
Tweet

Other Decks in Technology

Transcript

  1. Introduction to Gradle • Build automation system • Default for

    Android projects • Built-in dependency management
  2. Groovy based DSL apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android

    { compileSdkVersion 28 defaultConfig { applicationId "com.manpreets.gdgdevfest" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
  3. android { compileSdkVersion 28 defaultConfig { applicationId "com.manpreets.gdgdevfest" minSdkVersion 15

    targetSdkVersion 28 versionCode 1 versionName "1.0" } } android({ BaseExtension be -> be.compileSdkVersion(28) be.defaultConfig({ DefaultConfig df -> df.applicationId("com.manpreets.gdgdevfest") df.minSdkVersion(15) df.targetSdkVersion(28) df.versionCode(1) df.versionName("1.0") }) })
  4. What’s missing? • Documentation and navigation sources • Feedback loop

    in the IDE • Auto-complete! • Understanding of groovy • Extension of build logic
  5. plugins { id("com.android.application") id("kotlin-android") } android { compileSdkVersion(28) defaultConfig {

    applicationId = "com.manpreets.gdgdevfest" minSdkVersion(15) targetSdkVersion(28) versionCode = 1 versionName = "1.0" } } dependencies { implementation(fileTree(mapOf("dir" to "libs", "include" to mutableListOf("*.jar")))) implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION)) } gradle/Kotlin-DSL
  6. How? • Higher order functions • Use of lambdas outside

    of method parenthesis • Extension function • Operator overloading
  7. class BaseAndroid(var compileSdkVersion: Int = 23, var defaultConfig: DefaultConfig =

    DefaultConfig()) android { compileSdkVersion = 28 .. } fun android(android: BaseAndroid.() -> Unit) = BaseAndroid().apply(android)
  8. android { compileSdkVersion = 28 defaultConfig { applicationId = "com.manpreets.gdgdevfest"

    .. } } class BaseAndroid(var compileSdkVersion: Int = 23, var defaultConfig: DefaultConfig = DefaultConfig())
  9. class HelloGDG { operator fun invoke(msg: String) { print(msg) }

    } var helloGDG = HelloGDG() helloGDG("Hello")
  10. class BaseAndroid(var compileSdkVersion: Int = 23, var defaultConfig: DefaultConfig =

    DefaultConfig()) android { .. defaultConfig { applicationId = "com.manpreets.gdgdevfest" .. } } class DefaultConfig(var applicationId: String = "default", var ndkOptions: NdkOptions = NdkOptions()) { operator fun invoke(defaultConfig: DefaultConfig.() -> Unit) { this.apply(defaultConfig) } }
  11. class NdkOptions(var abiFilter: String = "") { operator fun invoke(f:

    NdkOptions.() -> Unit) { this.apply(f) } } class DefaultConfig(var applicationId: String = "default", var ndkOptions: NdkOptions = NdkOptions()) { operator fun invoke(defaultConfig: DefaultConfig.() -> Unit) { this.apply(defaultConfig) } } defaultConfig { applicationId = "com.manpreets.gdgdevfest" ndkOptions { abiFilter = "armeabi-v7a" } }
  12. android { compileSdkVersion = 28 defaultConfig { applicationId = "com.manpreets.gdgdevfest"

    ndkOptions { abiFilter = "armeabi-v7a" } } } class BaseAndroid(var compileSdkVersion: Int = 23, var defaultConfig: DefaultConfig = DefaultConfig()) class DefaultConfig(var applicationId: String = "default", var ndkOptions: NdkOptions = NdkOptions()) class NdkOptions(var abiFilter: String = "")
  13. Summary • Use buildSrc • Use Action<T> • Legacy support

    • Not stable yet • Slower • https://github.com/manpreetsgarha/gdgdevfest • https://github.com/gradle/kotlin-dsl • https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin • https://docs.gradle.org/5.0-milestone-1/userguide/userguide.html