Hai Tran
10/20/2020, 10:00 AMMarc Knaup
10/20/2020, 10:02 AMMarc Knaup
10/20/2020, 10:02 AMHai Tran
10/20/2020, 10:10 AMsourceSets {
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"
}
}
}
Marc Knaup
10/20/2020, 10:11 AMHai Tran
10/20/2020, 10:15 AMMarc Knaup
10/20/2020, 10:17 AMHai Tran
10/20/2020, 10:23 AMMarc Knaup
10/20/2020, 10:24 AMHai Tran
10/20/2020, 10:24 AMclasspath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10")
1.4.10 i think!Marc Knaup
10/20/2020, 10:25 AMkotlin("jvm") version "…"
Hai Tran
10/20/2020, 10:27 AMMarc Knaup
10/20/2020, 10:27 AMbuild.gradle
or build.gradle.kts
, usually around the top.Hai Tran
10/20/2020, 10:29 AMbuildscript {
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 projectHai Tran
10/20/2020, 10:29 AMplugins {
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 levelHai Tran
10/20/2020, 10:30 AMimport 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 levelMarc Knaup
10/20/2020, 10:30 AMMarc Knaup
10/20/2020, 10:30 AMHai Tran
10/20/2020, 10:32 AMHai Tran
10/20/2020, 10:32 AMpackage 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 {
}
}
}
Hai Tran
10/20/2020, 10:32 AMpackage 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 {
}
}
}
Hai Tran
10/20/2020, 10:33 AMHai Tran
10/20/2020, 10:33 AMMarc Knaup
10/20/2020, 10:34 AMimport kotlin.coroutines.CoroutineScope
Hai Tran
10/20/2020, 10:35 AMMarc Knaup
10/20/2020, 10:36 AMimport kotlinx.coroutines.CoroutineScope
Marc Knaup
10/20/2020, 10:36 AMHai Tran
10/20/2020, 10:37 AMMarc Knaup
10/20/2020, 10:37 AMimplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1")
change that to:
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1")
Marc Knaup
10/20/2020, 10:37 AMMarc Knaup
10/20/2020, 10:38 AMMarc Knaup
10/20/2020, 10:38 AMorg.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1
to your Android project directly.Hai Tran
10/20/2020, 10:38 AMHai Tran
10/20/2020, 10:39 AMMarc Knaup
10/20/2020, 10:40 AMimplementation
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.Hai Tran
10/20/2020, 10:42 AMMarc Knaup
10/20/2020, 10:42 AM