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

Gradle Kotlin DSL for Android Projects

Avatar for Claudia Claudia
October 25, 2019

Gradle Kotlin DSL for Android Projects

Migrate your Android projects to Gradle Kotlin DSL

Avatar for Claudia

Claudia

October 25, 2019
Tweet

More Decks by Claudia

Other Decks in Programming

Transcript

  1. #droidconUK clau_cookie “A domain-specific language (DSL) is a computer language

    specialized to a particular application domain.” Wikipedia 12 Fine… what is grade kotlin DSL? Kotlin dsl for android projects
  2. #droidconUK clau_cookie 14 Android studio + groovy dsl Groovy DSL

    build.gradle Kotlin dsl for android projects
  3. #droidconUK clau_cookie 15 Android studio + Kotlin dsl Kotlin DSL

    build.gradle.kts Kotlin dsl for android projects
  4. #droidconUK clau_cookie 16 Android studio + Kotlin dsl Kotlin DSL

    build.gradle.kts Kotlin dsl for android projects
  5. #droidconUK clau_cookie Get started Syntax Gotchas Cool stuff Questions 17

    Tell me more !?! Kotlin dsl for android projects
  6. How do I get started !?! #droidconUK clau_cookie build.gradle 1)

    Import your android project 18 Kotlin dsl for android projects
  7. How do I get started !?! #droidconUK clau_cookie build.gradle 1)

    Import your android project WHAT IF I DONT DO IT???? 19 Kotlin dsl for android projects
  8. How do I get started !?! #droidconUK clau_cookie 2) Rename

    a script settings.gradle 20 Kotlin dsl for android projects
  9. How do I get started !?! #droidconUK clau_cookie 2) Rename

    a script settings.gradle.kts 21 build.gradle Kotlin dsl for android projects
  10. How do I get started !?! #droidconUK clau_cookie 2) Rename

    a script What will I see??? 22 settings.gradle.kts build.gradle Kotlin dsl for android projects
  11. How do I get started !?! #droidconUK clau_cookie 23 Groovy

    DSL settings.gradle.kts Kotlin dsl for android projects
  12. How do I get started !?! #droidconUK clau_cookie 2) Rename

    a script That’s it! 24 settings.gradle.kts build.gradle Kotlin dsl for android projects
  13. And the syntax !?! #droidconUK clau_cookie Equals=Equals group 'com.acme' Groovy

    DSL group = "com.acme" Kotlin DSL 27 Kotlin dsl for android projects
  14. And the syntax !?! #droidconUK clau_cookie Function(parenthesis) implementation 'com.acme:example:1.0' Groovy

    DSL implementation("com.acme:example:1.0") Kotlin DSL 28 Kotlin dsl for android projects
  15. And the syntax !?! #droidconUK clau_cookie Function(parenthesis) group ‘com.acme' implementation

    ‘com.acme:example:1.0' Groovy DSL group = "com.acme" implementation("com.acme:example:1.0") Kotlin DSL Kotlin dsl for android projects
  16. And the syntax !?! #droidconUK clau_cookie Wrap up group ‘com.acme'

    implementation ‘com.acme:example:1.0' Groovy DSL group = "com.acme" implementation("com.acme:example:1.0") Kotlin DSL 30 “Strings” equals=equals function(parenthesis) Kotlin dsl for android projects
  17. Gotchas… errors… we are human #droidconUK clau_cookie Very First Step

    Auto-Reload Dependencies Tips 31 Kotlin dsl for android projects
  18. Gotchas… errors… we are human #droidconUK clau_cookie Very First Step

    group ‘com.acme' implementation ‘com.acme:example:1.0' group = "com.acme" implementation("com.acme:example:1.0") Groovy DSL 33 Kotlin dsl for android projects
  19. Gotchas… errors… we are human #droidconUK clau_cookie Tips: Adding plugins

    plugins { java `maven-publish` kotlin(“android”) id("org.springframework.boot") version "2.0.2.RELEASE" } apply<DetektPlugin>() apply(plugin = "com.random.library") apply(from = “rootproject/folder/myPlugin.gradle.kts”) Kotlin DSL 38 build.gradle.kts Kotlin dsl for android projects
  20. Gotchas… errors… we are human #droidconUK clau_cookie 39 Tips: Adding

    plugins plugins { java // Extension property `maven-publish` // Extension property kotlin(“android”) // Extension function id("org.springframework.boot") version "2.0.2.RELEASE" } apply<DetektPlugin>() apply(plugin = "com.random.library") apply(from = “rootproject/folder/myPlugin.gradle.kts”) Gradle Plugin Portal: https://plugins.gradle.org/search?term=org.jetbrains.kotlin Kotlin DSL build.gradle.kts Kotlin dsl for android projects
  21. Gotchas… errors… we are human #droidconUK clau_cookie 40 Tips: Adding

    plugins plugins { java `maven-publish` kotlin(“android”) id("org.springframework.boot") version "2.0.2.RELEASE" } apply<DetektPlugin>() // Extension function apply(plugin = “com.random.library") apply(from = “rootproject/folder/myPlugin.gradle.kts") Kotlin DSL build.gradle.kts Kotlin dsl for android projects
  22. Gotchas… errors… we are human #droidconUK clau_cookie Tips: Use $

    ./Gradlew tasks $ ./gradlew tasks 41 Kotlin dsl for android projects
  23. Gotchas… errors… we are human #droidconUK clau_cookie Tips: Use latest

    stable versions 42 3.5 Kotlin dsl for android projects
  24. Gotchas… errors… we are human #droidconUK clau_cookie builld.gradle.kts Tips: Be

    careful renaming scripts 43 ??? Kotlin dsl for android projects
  25. Can we see something cool, please ?? Kotlin dsl for

    android projects #droidconUK clau_cookie 44
  26. Dependencies hell #droidconUK clau_cookie dependencies { implementation sdkDependencies.sdkCore implementation sdkDependencies.commonUtil

    testImplementation project(projectModules.androidTestHelpers) testImplementation project(projectModules.javaTestHelpers) implementation projectDependencies.kotlin implementation projectDependencies.kotlinCoroutines implementation projectDependencies.kotlinCoroutinesRx2 implementation projectDependencies.nullabilityAnnotations implementation projectDependencies.dagger implementation projectDependencies.javaInject kapt projectDependencies.daggerAnnotationProcessor compileOnly projectDependencies.dexguardAnnotations implementation projectDependencies.dexguardRuntime implementation projectDependencies.jodaTimeAndroid implementation projectDependencies.orbitDataLayer api projectDependencies.rxPreferences implementation projectDependencies.facebookSdk implementation projectDependencies.mixpanel implementation projectDependencies.appsFlyerAnalyticsTracking implementation projectDependencies.snowplowAnalyticsTracking implementation projectDependencies.firebaseAnalytics implementation projectDependencies.crashlyticsBugReporting implementation projectDependencies.newRelicCrashAndPerformanceMonitoringAgent implementation projectDependencies.zopim ... 45 Groovy DSL build.gradle Kotlin dsl for android projects
  27. Dependencies hell #droidconUK clau_cookie dependencies { implementation sdkDependencies.sdkCore implementation sdkDependencies.commonUtil

    testImplementation project(projectModules.androidTestHelpers) testImplementation project(projectModules.javaTestHelpers) implementation projectDependencies.kotlin implementation projectDependencies.kotlinCoroutines implementation projectDependencies.kotlinCoroutinesRx2 implementation projectDependencies.nullabilityAnnotations implementation projectDependencies.dagger implementation projectDependencies.javaInject kapt projectDependencies.daggerAnnotationProcessor compileOnly projectDependencies.dexguardAnnotations implementation projectDependencies.dexguardRuntime implementation projectDependencies.jodaTimeAndroid implementation projectDependencies.orbitDataLayer api projectDependencies.rxPreferences implementation projectDependencies.facebookSdk implementation projectDependencies.mixpanel implementation projectDependencies.appsFlyerAnalyticsTracking implementation projectDependencies.snowplowAnalyticsTracking implementation projectDependencies.firebaseAnalytics implementation projectDependencies.crashlyticsBugReporting implementation projectDependencies.newRelicCrashAndPerformanceMonitoringAgent implementation projectDependencies.zopim ... Groovy DSL build.gradle What If 46 Kotlin dsl for android projects
  28. Dependencies heaven #droidconUK clau_cookie object Libraries { const val dokkaLib

    = "org.jetbrains.dokka:dokka-android-gradle-plugin:${Versions.dokka}" const val androidLib = "com.android.tools.build:gradle:${Versions.gradleAndroid}" const val dexcountLib = "com.getkeepsafe.dexcount:dexcount-gradle-plugin:${Versions.dexcount}" const val kotlinLib = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}" const val fabricLib = "io.fabric.tools:gradle:${Versions.fabric}" const val googlePlayServicesLib = "com.google.gms:google-services:${Versions.googlePlayServices}" … } buildSrc / Libraries.kt 47 Kotlin dsl for android projects
  29. Dependencies hell #droidconUK clau_cookie 48 Kotlin dsl for android projects

    dependencies { implementation Libraries.sdkCore implementation Libraries.commonUtil testImplementation project(Libraries.androidTestHelpers) testImplementation project(Libraries.javaTestHelpers) implementation Libraries.kotlin implementation Libraries.kotlinCoroutines implementation Libraries.kotlinCoroutinesRx2 implementation Libraries.nullabilityAnnotations implementation Libraries.dagger implementation Libraries.javaInject kapt Libraries.daggerAnnotationProcessor compileOnly Libraries.dexguardAnnotations implementation Libraries.dexguardRuntime implementation Libraries.jodaTimeAndroid implementation Libraries.orbitDataLayer api Libraries.rxPreferences implementation Libraries.facebookSdk implementation Libraries.mixpanel implementation Libraries.appsFlyerAnalyticsTracking implementation Libraries.snowplowAnalyticsTracking implementation Libraries.firebaseAnalytics implementation Libraries.crashlyticsBugReporting implementation Libraries.newRelicCrashAndPerformanceMonitoringAgent implementation Libraries.zopim ... Groovy DSL build.gradle
  30. dependencies { implementation Libraries.sdkCore implementation Libraries.commonUtil testImplementation project(Libraries.androidTestHelpers) testImplementation project(Libraries.javaTestHelpers)

    implementation Libraries.kotlin implementation Libraries.kotlinCoroutines implementation Libraries.kotlinCoroutinesRx2 implementation Libraries.nullabilityAnnotations implementation Libraries.dagger implementation Libraries.javaInject kapt Libraries.daggerAnnotationProcessor compileOnly Libraries.dexguardAnnotations implementation Libraries.dexguardRuntime implementation Libraries.jodaTimeAndroid implementation Libraries.orbitDataLayer api Libraries.rxPreferences implementation Libraries.facebookSdk implementation Libraries.mixpanel implementation Libraries.appsFlyerAnalyticsTracking implementation Libraries.snowplowAnalyticsTracking implementation Libraries.firebaseAnalytics implementation Libraries.crashlyticsBugReporting implementation Libraries.newRelicCrashAndPerformanceMonitoringAgent implementation Libraries.zopim ... Dependencies hell #droidconUK clau_cookie Groovy DSL build.gradle What If 49 Kotlin dsl for android projects
  31. Dependencies heaven #droidconUK clau_cookie sealed class DepConfig(open var name: String)

    { data class Impl(override var name: String) : DepConfig(name) data class Kapt(override var name: String) : DepConfig(name) data class TestImpl(override var name: String) : DepConfig(name) data class AndroidTestImpl(override var name: String) : DepConfig(name) } object Config { val libs = listOf( DepConfig.Impl(Libraries.recyclerView), DepConfig.Impl(Libraries.appCompat), DepConfig.Impl(Libraries.coreKtx), DepConfig.Impl(DepsLibraries.constraintLayout), DepConfig.Impl(.fragmentKtx), … ) } buildSrc / Config.kt 51 Kotlin dsl for android projects
  32. Dependencies heaven #droidconUK clau_cookie dependencies { Config.libs.forEach { when(it) {

    is DepConfig.Impl -> implementation(it.nameAndVersion) is DepConfig.Kapt -> kapt(it.nameAndVersion) is DepConfig.TestImpl -> testImplementation(it.nameAndVersion) is DepConfig.AndroidTestImpl -> androidTestImplementation(it.nameAndVersion) } } } Kotlin DSL build.gradle.kts Kotlin dsl for android projects
  33. Dependencies hell #droidconUK clau_cookie 53 Kotlin dsl for android projects

    Groovy DSL build.gradle dependencies { implementation sdkDependencies.sdkCore implementation sdkDependencies.commonUtil testImplementation project(projectModules.androidTestHelpers) testImplementation project(projectModules.javaTestHelpers) implementation projectDependencies.kotlin implementation projectDependencies.kotlinCoroutines implementation projectDependencies.kotlinCoroutinesRx2 implementation projectDependencies.nullabilityAnnotations implementation projectDependencies.dagger implementation projectDependencies.javaInject kapt projectDependencies.daggerAnnotationProcessor compileOnly projectDependencies.dexguardAnnotations implementation projectDependencies.dexguardRuntime implementation projectDependencies.jodaTimeAndroid implementation projectDependencies.orbitDataLayer api projectDependencies.rxPreferences implementation projectDependencies.facebookSdk implementation projectDependencies.mixpanel implementation projectDependencies.appsFlyerAnalyticsTracking implementation projectDependencies.snowplowAnalyticsTracking implementation projectDependencies.firebaseAnalytics implementation projectDependencies.crashlyticsBugReporting implementation projectDependencies.newRelicCrashAndPerformanceMonitoringAgent implementation projectDependencies.zopim ... Groovy DSL
  34. Dependencies heaven #droidconUK clau_cookie Kotlin dsl for android projects dependencies

    { Config.libs.forEach { when(it) { is DepConfig.Impl -> implementation(it.nameAndVersion) is DepConfig.Kapt -> kapt(it.nameAndVersion) is DepConfig.TestImpl -> testImplementation(it.nameAndVersion) is DepConfig.AndroidTestImpl -> androidTestImplementation(it.nameAndVersion) } } } Kotlin DSL build.gradle.kts
  35. What else ???? #droidconUK clau_cookie … configure<MarkdownLint> { rules {

    +LineLengthRule(codeBlocks = false, tables = false, headings = false) +NoDuplicateHeaderRule(allowDifferentNesting = true) { excludes = listOf(".*/kotlin-style-guide.md") } +NoTrailingPunctuationRule(punctuation = ".,;:!") } excludes = listOf(".*/jadx-0\\.6\\.1/README\\.md") threshold = 0 } … 60 Kotlin dsl for android projects Kotlin DSL build.gradle.kts AppMattus Markdown-lint
  36. #droidconUK clau_cookie 1) First clean build 0 5.5 11 16.5

    22 First use clean build (sec) Groovy DSL Kotlin DSL 63 Fine, you convinced me… Kotlin dsl for android projects 3x Slower
  37. #droidconUK clau_cookie 2) Update BuildSrc folder 0 5.5 11 16.5

    22 First use clean build (sec) Groovy DSL Kotlin DSL 64 Fine, you convinced me… Kotlin dsl for android projects 3x Slower ???
  38. #droidconUK clau_cookie 3) Build cache enabled 0 2 4 6

    8 Other clean build (sec) Groovy DSL Kotlin DSL 65 Fine, you convinced me… Kotlin dsl for android projects
  39. #droidconUK clau_cookie Is it worth to migrate? 66 Fine, you

    convinced me… Kotlin dsl for android projects
  40. #droidconUK clau_cookie How difficult is it to maintain? 68 Fine,

    you convinced me… Kotlin dsl for android projects
  41. #droidconUK clau_cookie 70 Kotlin dsl for android projects There is

    a converter!!! bernaferrari Gradle Kotlin Converter
  42. References #droidconUK clau_cookie https://docs.gradle.org/current/userguide/kotlin_dsl.html https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/ https://github.com/gradle/kotlin-dsl/issues/1320 https://github.com/gradle/kotlin-dsl/issues/821#issue-314655076 https://gradle.com/blog/kotlin-dsl/ https://docs.gradle.org/current/userguide/plugins.html#sec:types_of_plugins https://kotlinlang.org/docs/reference/using-gradle.html#targeting-android

    https://github.com/antoniolg/Bandhook-Kotlin https://antonioleiva.com/kotlin-dsl-gradle/ https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources https://github.com/arturogutierrez/gradle-script-kotlin-example/blob/master/build.gradle.kts https://github.com/gradle/kotlin-dsl/tree/master/samples/multi-kotlin-project-with-buildSrc/buildSrc https://docs.gradle.org/current/userguide/managing_dependency_configurations.html https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html https://docs.gradle.org/current/userguide/userguide_single.html? _ga=2.81255895.1790701507.1567255356-585497352.1559761848#custom_plugins https://docs.gradle.org/current/userguide/build_lifecycle.html https://proandroiddev.com/takeaways-from-migrating-a-complex-android-project-to-gradle-kotlin-dsl-eaeb5ccd8c84 https://docs.gradle.org/5.0/userguide/plugin_reference.html https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/ https://github.com/gradle/kotlin-dsl-samples/issues/902 https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources Kotlin dsl for android projects