Share
Embed
Gradle Kotlin DSL for Android Projects
Migrate your Android projects to Gradle Kotlin DSL
Claudia
October 25, 2019
Transcript
73 slides
-
#droidconUK clau_cookie 10 YES ! The solution is… Kotlin dsl
for android projects -
fine… What is gradle Kotlin dsl? #droidconUK clau_cookie 11 Kotlin
dsl for android projects -
#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 -
#droidconUK clau_cookie 13 Fine… what is grade kotlin DSL? dsL
VS dsl Kotlin dsl for android projects -
#droidconUK clau_cookie 14 Android studio + groovy dsl Groovy DSL
build.gradle Kotlin dsl for android projects -
#droidconUK clau_cookie 15 Android studio + Kotlin dsl Kotlin DSL
build.gradle.kts Kotlin dsl for android projects -
#droidconUK clau_cookie 16 Android studio + Kotlin dsl Kotlin DSL
build.gradle.kts Kotlin dsl for android projects -
#droidconUK clau_cookie Get started Syntax Gotchas Cool stuff Questions 17
Tell me more !?! Kotlin dsl for android projects -
How do I get started !?! #droidconUK clau_cookie build.gradle 1)
Import your android project 18 Kotlin dsl for android projects -
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 -
How do I get started !?! #droidconUK clau_cookie 2) Rename
a script settings.gradle 20 Kotlin dsl for android projects -
How do I get started !?! #droidconUK clau_cookie 2) Rename
a script settings.gradle.kts 21 build.gradle Kotlin dsl for android projects -
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 -
How do I get started !?! #droidconUK clau_cookie 23 Groovy
DSL settings.gradle.kts Kotlin dsl for android projects -
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 -
And the syntax !?! #droidconUK clau_cookie 25 Kotlin dsl for
android projects -
And the syntax !?! #droidconUK clau_cookie “Strings” project(':search') project(":search") Groovy
DSL Kotlin DSL 26 Kotlin dsl for android projects -
And the syntax !?! #droidconUK clau_cookie Equals=Equals group 'com.acme' Groovy
DSL group = "com.acme" Kotlin DSL 27 Kotlin dsl for android projects -
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 -
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 -
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 -
Gotchas… errors… we are human #droidconUK clau_cookie Very First Step
Auto-Reload Dependencies Tips 31 Kotlin dsl for android projects -
Gotchas… errors… we are human #droidconUK clau_cookie Auto-Reload Dependencies Tips
Very First Step 32 Kotlin dsl for android projects -
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 -
Gotchas… errors… we are human #droidconUK clau_cookie 34 build.gradle.kts Kotlin
dsl for android projects -
Gotchas… errors… we are human #droidconUK clau_cookie 35 build.gradle.kts Kotlin
dsl for android projects -
Gotchas… errors… we are human #droidconUK clau_cookie Enable Auto-Reload Dependencies
36 Kotlin dsl for android projects -
Gotchas… errors… we are human #droidconUK clau_cookie 37 build.gradle.kts Kotlin
DSL Kotlin dsl for android projects -
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 -
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 -
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 -
Gotchas… errors… we are human #droidconUK clau_cookie Tips: Use $
./Gradlew tasks $ ./gradlew tasks 41 Kotlin dsl for android projects -
Gotchas… errors… we are human #droidconUK clau_cookie Tips: Use latest
stable versions 42 3.5 Kotlin dsl for android projects -
Gotchas… errors… we are human #droidconUK clau_cookie builld.gradle.kts Tips: Be
careful renaming scripts 43 ??? Kotlin dsl for android projects -
Can we see something cool, please ?? Kotlin dsl for
android projects #droidconUK clau_cookie 44 -
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 -
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 -
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 -
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 -
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 -
Dependencies hell #droidconUK clau_cookie Implementation Kapt TestImplementation … 50 Kotlin
dsl for android projects -
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 -
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 -
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 -
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 -
#droidconUK clau_cookie 58 What else can I do with Kotlin????
Kotlin dsl for android projects -
What else ???? #droidconUK clau_cookie fun Project.isCiBuild() = System.getenv("JENKINS_HOME") !=
null build.gradle.kts 59 Kotlin dsl for android projects -
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 -
Fine, you convinced me… #droidconUK clau_cookie 61 Kotlin dsl for
android projects -
#droidconUK clau_cookie But…What are the trade-offs? 62 Fine, you convinced
me… Kotlin dsl for android projects -
#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 -
#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 ??? -
#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 -
#droidconUK clau_cookie Is it worth to migrate? 66 Fine, you
convinced me… Kotlin dsl for android projects -
#droidconUK clau_cookie What about big projects? 67 Fine, you convinced
me… Kotlin dsl for android projects -
#droidconUK clau_cookie How difficult is it to maintain? 68 Fine,
you convinced me… Kotlin dsl for android projects -
#droidconUK clau_cookie 70 Kotlin dsl for android projects There is
a converter!!! bernaferrari Gradle Kotlin Converter -
Let’s do this ! #droidconUK clau_cookie 71 Kotlin dsl for
android projects -
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