Slide 1

Slide 1 text

Kotlin DSL: Moduralización en proyectos Dinorah Tovar Mobile Engineer @ddinorahtovar @ddinorahtovar @dinorahto @dinorahto Doing code @ Konfio

Slide 2

Slide 2 text

What the heck is DSL?

Slide 3

Slide 3 text

What is DSL? A domain-specific language (DSL) is a computer language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains. Wikipedia® Domain Specific Language

Slide 4

Slide 4 text

Like, for real? •Provides you a flexible tool •Particular applications •Kotlin used it already

Slide 5

Slide 5 text

Has anyone used this before? YES

Slide 6

Slide 6 text

For real? Extension.function() + Lambda { //Code }

Slide 7

Slide 7 text

Extension Functions //Extension function fun Int.someCoolStuff { this.stuff() }

Slide 8

Slide 8 text

Extension Functions //Receiver fun Int.someCoolStuff { this.stuff() }

Slide 9

Slide 9 text

Extension Functions //Lambda { () -> doStuff() }

Slide 10

Slide 10 text

Extension Functions //Lambda with receiver { () -> this.doStuff() }

Slide 11

Slide 11 text

Lets create an DSL function class IsleOfDogs { var type: String? = "" } fun isleOfDogs (lambda: IsleOfDogs.() -> Unit) : IsleOfDogs { return IsleOfDogs().apply(lambda) }

Slide 12

Slide 12 text

Extension Functions class IsleOfDogs { var type: String? = "" } fun isleOfDogs (lambda: IsleOfDogs.() -> Unit) : IsleOfDogs { return IsleOfDogs().apply(lambda) }

Slide 13

Slide 13 text

A common example with Kotlin fun buildString(action: (StringBuilder).() -> Unit): String { val stringBuilder = StringBuilder() action(stringBuilder) return stringBuilder.toString() }

Slide 14

Slide 14 text

A common example with Kotlin buildString { append("<") append(“We love Kotlin at Konfio!”) append(">") }

Slide 15

Slide 15 text

A common example textView.text = "We love Kotlin at Konfio" textView.setOnClickListener { //This is a listener } textView.setTextColor(Color.BLACK)

Slide 16

Slide 16 text

Examples: textView.apply { text = "Hola Konfio!" setOnClickListener { //This is a listener } textColor(Color.BLACK) }

Slide 17

Slide 17 text

Type-safe 
 Logic in Gradle

Slide 18

Slide 18 text

Type-safe model accessors •Dependency and artifact configurations (such as implementation and runtimeOnly contributed by the Java Plugin) •Project extensions and conventions (such as sourceSets) •Elements in the tasks and configurations containers •Elements in project-extension containers (for example the source sets contributed by the Java Plugin that are added to the sourceSets container)

Slide 19

Slide 19 text

This talk will not cover this, but here is something cool: https:!//www.youtube.com/watch?v=mAtrEPeAJSc&feature=youtu.be

Slide 20

Slide 20 text

Gradle

Slide 21

Slide 21 text

Gradle •Declarative elements describe the “what” • The underlying logic creates the “how” • Groovy provides an extensible DSL language

Slide 22

Slide 22 text

How Gradle Works? Gradle Task Gradle Build Scripts Gradle Task Gradle Task Gradle task Execution Gradle task Executio Gradle task Executio

Slide 23

Slide 23 text

Gradle and Kotlin

Slide 24

Slide 24 text

Gradle + Kotlin DSL

Slide 25

Slide 25 text

Gradle and Android dependencies { implementation("com.squareup.okio:okio:2.0.0") }

Slide 26

Slide 26 text

The real problem: •Editing magic strings is error-prone. •How do I centralize dependencies in a multi-modules project? •Are there newer versions for my libs?

Slide 27

Slide 27 text

.kt vs .kts vs .gradle.kts.

Slide 28

Slide 28 text

Differences and similarities •They all contain Kotlin Code •.kt files are compiled by the Kotlin compiler •.kts files are executed by the Kotlin scripting support •.gradle.kts are hosted by Gradle

Slide 29

Slide 29 text

.gradle.kts ❤ •Kotlin friendly extension of the Gradle API •Delegated properties for Gradle properties and collections •Dynamically generates Kotlin extensions •For models elements contributed by plugins, like task or configuration

Slide 30

Slide 30 text

The solution

Slide 31

Slide 31 text

The real problem:

Slide 32

Slide 32 text

Multimodule projects •Manual Management •Google’s Recommendation using “ext” •Kotlin + buildSrc + DSL

Slide 33

Slide 33 text

Multimodule projects //ModuleA - build.gradle implementation "com.android.support:support-annotations:27.0.2" implementation "com.android.support:appcompat-v7:27.0.2" implementation "com.squareup.retrofit2:retrofit:2.3.0" implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"

Slide 34

Slide 34 text

Multimodule projects //ModuleB - build.gradle implementation "com.android.support:support-annotations:27.0.2" implementation "com.android.support:appcompat-v7:27.0.2" implementation "com.squareup.retrofit2:retrofit:2.3.0" implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"

Slide 35

Slide 35 text

Multimodule projects ext { versions = [ support_lib: "27.0.2", retrofit: "2.3.0", ] libs = [ support_annotations: "com.android.support:support-annotations:${versions.support_lib}", support_appcompat: "com.android.support:appcompat-v7:${versions.support_lib}", retrofit :"com.squareup.retrofit2:retrofit:${versions.retrofit}" ] }

Slide 36

Slide 36 text

Multimodule projects //Module-A / build.gradle implementation libs.support_annotations implementation libs.support_appcompat_v7 implementation libs.retrofit implementation libs.retrofit_rxjava_adapter implementation libs.rxjava

Slide 37

Slide 37 text

Multimodule projects //Module-A / build.gradle implementation libs.support_annotations implementation libs.support_appcompat_v7 implementation libs.retrofit implementation libs.retrofit_rxjava_adapter implementation libs.rxjava

Slide 38

Slide 38 text

The solution •You can create a buildSrc module with Kotlin code to manage dependencies and get IDE completion support.

Slide 39

Slide 39 text

The solution •You can create a buildSrc module with Kotlin code to manage dependencies and get IDE completion support.

Slide 40

Slide 40 text

The solution •Inside build.gradle.kts

Slide 41

Slide 41 text

The solution •Inside Dependencies.kt

Slide 42

Slide 42 text

The solution •Inside your App Gradle

Slide 43

Slide 43 text

Kotlin DSL: Moduralización en proyectos Dinorah Tovar Mobile Engineer @ddinorahtovar @ddinorahtovar @dinorahto @dinorahto Doing code @ Konfio