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

Embracing commonMain for Android Development - ...

Embracing commonMain for Android Development - Droidcon SF 2022

Video: https://www.droidcon.com/2022/06/28/embracing-commonmain-for-android-development/

We are all excited to use Kotlin Multiplatform, but on large projects it’s hard to figure out where to start using it without introducing risk. In this talk, I’ll give you techniques to start writing your code in commonMain right now. The code you write in commonMain is just Kotlin code, so it’s no different than what you write today, but with some slightly different configuration and guidelines that need to be followed. I’ll show you how to quickly set up your project configuration, and what guidelines to follow so you can start exercising your Multiplatform muscles. Writing code in commonMain helps promote modularity, and makes you think like a “Mobile developer” instead of just an “Android” developer.

Sam Edwards

June 03, 2022
Tweet

More Decks by Sam Edwards

Other Decks in Programming

Transcript

  1. Sam Edwards - @HandstandSam Kotlin & Android GDE @ Dropbox

    Embracing commonMain 🫂 for Android Development Droidcon SF 2022
  2. “By writing code in a non-standard fashion, we took on

    overhead that we would have not had to worry about had we stayed with the widely used platform defaults. This overhead ended up being more expensive than just writing the code twice.”
  3. The Overhead of: 1. Custom frameworks and libraries 2. Custom

    development environment 3. Addressing di ff erences between the platforms 4. Training, hiring, and retaining developers
  4. Plant a seed for Kotlin Multiplatform 🌱 and we’ll see

    where it takes us • Same Kotlin Code, di ff erent Gradle plugin • Forces modularity • Makes developers more likely to try coding in the other platform
  5. commonMain and commonTest Con fi gurations Kotlin + Gradle kotlin

    { jvm() sourceSets { maybeCreate("commonMain").apply { dependencies { implementation(libs.kotlin.coroutines) } } maybeCreate("commonTest").apply { dependencies { implementation(libs.kotlin.test.common) implementation(libs.kotlin.test.annotations.common) } } } }
  6. commonMain and commonTest Con fi gurations Kotlin + Gradle kotlin

    { jvm() sourceSets { maybeCreate("commonMain").apply { dependencies { implementation(libs.kotlin.coroutines) } } maybeCreate("commonTest").apply { dependencies { implementation(libs.kotlin.test.common) implementation(libs.kotlin.test.annotations.common) } } } }
  7. commonMain and commonTest Con fi gurations Kotlin + Gradle kotlin

    { jvm() sourceSets { maybeCreate("commonMain").apply { dependencies { implementation(libs.kotlin.coroutines) } } maybeCreate("commonTest").apply { dependencies { implementation(libs.kotlin.test.common) implementation(libs.kotlin.test.annotations.common) } } } }
  8. commonMain and commonTest Con fi gurations Kotlin + Gradle kotlin

    { jvm() sourceSets { maybeCreate("commonMain").apply { dependencies { implementation(libs.kotlin.coroutines) } } maybeCreate("commonTest").apply { dependencies { implementation(libs.kotlin.test.common) implementation(libs.kotlin.test.annotations.common) } } } }
  9. commonMain and commonTest Con fi gurations Kotlin + Gradle kotlin

    { jvm() sourceSets { maybeCreate("commonMain").apply { dependencies { implementation(libs.kotlin.coroutines) } } maybeCreate("commonTest").apply { dependencies { implementation(libs.kotlin.test.common) implementation(libs.kotlin.test.annotations.common) } } } }
  10. commonMain and commonTest Con fi gurations Kotlin + Gradle kotlin

    { jvm() sourceSets { maybeCreate("commonMain").apply { dependencies { implementation(libs.kotlin.coroutines) } } maybeCreate("commonTest").apply { dependencies { implementation(libs.kotlin.test.common) implementation(libs.kotlin.test.annotations.common) } } } }
  11. JVM Project Structure plugins { kotlin("jvm") } dependencies { implementation(libs.kotlin.stdlib)

    implementation(project(":samples:jvm_kmp4free")) testImplementation(libs.kotlin.test.common) testImplementation(libs.truth) }
  12. JVM Project Structure plugins { id("com.handstandsam.kmp4free") } dependencies { implementation(libs.kotlin.stdlib)

    implementation(project(":samples:jvm_kmp4free")) testImplementation(libs.kotlin.test.common) testImplementation(libs.truth) }
  13. Primary Goals of kmp4free • No Risk, Single Line Change

    to Enable Multiplatform • Support JVM & Multiplatform Con fi g • Allow Experimentation and Possible Migration to Multiplatform
  14. kmp4free=true • SourceSet Mapping • src/main ➡ src/commonMain • src/test

    ➡ src/jvmTest • Con fi guration Mapping • implementation ➡ commonMainImplementation • testImplementation ➡ jvmTestImplementation • Task Aliasing • :module:test ➡ :module:jvmTest
  15. kmp4free=false • SourceSet Mapping • src/commonMain ➡ src/main • src/jvmMain

    ➡ src/main • src/commonTest ➡ src/test • src/jvmTest ➡ src/test • Con fi guration Mapping • commonMainImplementation ➡ implementation • commonTestImplementation ➡ testImplementation • jvmTestImplementation ➡ testImplementation
  16. Migrating tests from androidTest ➡ jvm androidTest (instrumented) test (jvm)

    • Does not require any devices • Faster (usually)