So I am a newbie to Kotlin Multiplatform Mobile and mobile development in general. I am trying to follow this tutorial here on KMM tutorials to use Ktor in my project.
After adding dependencies, as shown in the build.gradle.kts below (dependencies for commonMain, androidMain, and iosMain):
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
kotlin {
android()
ios {
binaries {
framework {
baseName = "shared"
}
}
}
val ktorVersion = "1.5.2"
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:$ktorVersion")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation ("io.ktor:ktor-client-android:$ktorVersion")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting {
dependencies {
implementation ("io.ktor:ktor-client-ios:$ktorVersion")
}
}
val iosTest by getting
}
}
android {
compileSdkVersion(29)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(29)
}
}
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)
I tried to import io.ktor.client.*
to a class in the common module, but it won't resolve. However, when I tried to do the same thing for a class in the Android module then it works. Please see the screenshots below:
So my question is: Where did I go wrong? Or, is it supposed to be like this? From the documentation, I believe that the networking I am trying to do is supposed to be done in the common module, instead of in the platform specific module.
Please help, I have been looking around to understand what the problem is but no luck. Thank you!
Edit:
I have been having this notification in Android Studio
I googled it and it seems that the error notification is incorrect?