I’m trying add kotlinx-serialization(and something...
# announcements
h
I’m trying add kotlinx-serialization(and something pure kotlin library) dependencies for KMM project, Is there a need to add for each platform(Android/iOS) separately, or just add COMMON dependencies?
m
With Kotlin 1.4 and Serialization 1.0 only common should be needed.
👍 1
Check the channels #serialization and #multiplatform for further questions and or search for existing answers 🙂
👍 1
h
Thank you, i’m still confuse You mean, if some thing pure Kotlin, it should add in common dependencies right? Some KMM sample project on github, it add on all sourceSets, look like this:
Copy code
sourceSets {
        commonMain {
            dependencies {
                implementation "io.ktor:ktor-client-core:1.2.6"
                implementation "io.ktor:ktor-client-json:1.2.6"
                implementation "io.ktor:ktor-client-serialization:1.2.6"
            }
        }
        androidMain {
            dependencies {
                implementation "io.ktor:ktor-client-core-jvm:1.2.6"
                implementation "io.ktor:ktor-client-json-jvm:1.2.6"
                implementation "io.ktor:ktor-client-serialization-jvm:1.2.6"
            }
        }
        iosMain {
            dependencies {
                implementation "io.ktor:ktor-client-ios:1.2.6"
                implementation "io.ktor:ktor-client-core-native:1.2.6"
                implementation "io.ktor:ktor-client-json-native:1.2.6"
                implementation "io.ktor:ktor-client-serialization-native:1.2.6"
            }
        }
}
m
That’s probably for an older version of Kotlin 🙂
👍 1
h
What about Coroutine?
m
Yes. Do you have any issues when doing so?
h
i just add coroutines dependencies to commonMain, but in Android (MainActivity.kt) coroutine are ‘Unresolved’, it can only be used in common. Is that because it’s not pure kotlin like Serialization or for some reason?
m
What Kotlin version are you using?
h
Copy code
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10")
1.4.10 i think!
m
That’s the Gradle plugin version. The Kotlin version is somewhere else. Usually
kotlin("jvm") version "…"
h
image.png
m
No, it needs to be stated in your
build.gradle
or
build.gradle.kts
, usually around the top.
h
Copy code
buildscript {
    repositories {
        gradlePluginPortal()
        jcenter()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10")
        classpath("com.android.tools.build:gradle:4.0.1")
    }
}
group = "com.haitrvn.basekmm"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}
level project
Copy code
plugins {
    id("com.android.application")
    kotlin("android")
    id("kotlin-android-extensions")
}
group = "com.haitrvn.basekmm"
version = "1.0-SNAPSHOT"

repositories {
    gradlePluginPortal()
    google()
    jcenter()
    mavenCentral()
}
dependencies {
    implementation(project(":shared"))
    implementation("com.google.android.material:material:1.2.0")
    implementation("androidx.appcompat:appcompat:1.2.0")
    implementation("androidx.constraintlayout:constraintlayout:1.1.3")
}
android {
    compileSdkVersion(29)
    defaultConfig {
        applicationId = "com.haitrvn.basekmm.androidApp"
        minSdkVersion(24)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
}
android level
Copy code
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("kotlin-android-extensions")
}
group = "com.haitrvn.basekmm"
version = "1.0-SNAPSHOT"

repositories {
    gradlePluginPortal()
    google()
    jcenter()
    mavenCentral()
}
kotlin {
    android()
    ios {
        binaries {
            framework {
                baseName = "shared"
            }
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("com.google.android.material:material:1.2.0")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.12")
            }
        }
        val iosMain by getting
        val iosTest by getting
    }
}
android {
    compileSdkVersion(29)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
}
val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework =
        kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
share level
m
Oh interesting it doesn’t need a version anymore So it’s indeed 1.4.10
What code is not working?
h
you can see Coroutine dependencies already added!
Copy code
package com.haitrvn.basekmm.androidApp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.haitrvn.basekmm.shared.Greeting
import android.widget.TextView
import kotlin.coroutines.CoroutineContext

fun greet(): String {
    return Greeting().greeting()
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tv: TextView = findViewById(R.id.text_view)
        tv.text = greet()
    }

    fun test(){
        CoroutineScope(Dispatchers.Main).launch {
        }
    }
}
Copy code
package com.haitrvn.basekmm.shared

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class Greeting {
    fun greeting(): String {
        return "Hello, ${Platform().platform}!"
    }
    fun test(){
        CoroutineScope(Dispatchers.Main).launch {

        }
    }
}
in shared Package it’s working but in androidApp it’s not
Unresolved reference: CoroutineScope
m
What if you import it?
import kotlin.coroutines.CoroutineScope
h
image.png
m
Ah sorry.
import kotlinx.coroutines.CoroutineScope
Forgot the x
h
still need add dependencies in androidMain
m
Copy code
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1")
change that to:
Copy code
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1")
In shared
Otherwise projects that depend on the shared project won’t see its coroutines dependency.
Alternatively add
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1
to your Android project directly.
h
Ok, it work 🙂
👍 1
can you explain, why implementation not work?
m
implementation
means that the library dependency (coroutines) is only visible to your shared module.
api
means that the library dependency (coroutines) is visible to your shared module and to all modules that depend on your shared module.
👍 1
h
once again thank you so much!
m
You’re welcome