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

Kotlin DSL | Modularization for Mobile projects

Kotlin DSL | Modularization for Mobile projects

Dinorah Tovar

March 04, 2020
Tweet

More Decks by Dinorah Tovar

Other Decks in Technology

Transcript

  1. Kotlin DSL: Moduralización en proyectos Dinorah Tovar Mobile Engineer @ddinorahtovar

    @ddinorahtovar @dinorahto @dinorahto Doing code @ Konfio
  2. 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
  3. Lets create an DSL function class IsleOfDogs { var type:

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

    } fun isleOfDogs (lambda: IsleOfDogs.() -> Unit) : IsleOfDogs { return IsleOfDogs().apply(lambda) }
  5. A common example with Kotlin fun buildString(action: (StringBuilder).() -> Unit):

    String { val stringBuilder = StringBuilder() action(stringBuilder) return stringBuilder.toString() }
  6. A common example textView.text = "We love Kotlin at Konfio"

    textView.setOnClickListener { //This is a listener } textView.setTextColor(Color.BLACK)
  7. 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)
  8. This talk will not cover this, but here is something

    cool: https:!//www.youtube.com/watch?v=mAtrEPeAJSc&feature=youtu.be
  9. Gradle •Declarative elements describe the “what” • The underlying logic

    creates the “how” • Groovy provides an extensible DSL language
  10. How Gradle Works? Gradle Task Gradle Build Scripts Gradle Task

    Gradle Task Gradle task Execution Gradle task Executio Gradle task Executio
  11. 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?
  12. 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
  13. .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
  14. 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}" ] }
  15. The solution •You can create a buildSrc module with Kotlin

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

    code to manage dependencies and get IDE completion support.
  17. Kotlin DSL: Moduralización en proyectos Dinorah Tovar Mobile Engineer @ddinorahtovar

    @ddinorahtovar @dinorahto @dinorahto Doing code @ Konfio