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?
Updating org.jetbrains.kotlin:kotlin-gradle-plugin from 1.4.10 to 1.4.31 in root build.gradle.kts fixed the issue for me.
This is how my build.gradle.kts file looks like:
buildscript {
repositories {
gradlePluginPortal()
jcenter()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")
classpath("com.android.tools.build:gradle:4.2.0-beta06")
classpath("com.squareup.sqldelight:gradle-plugin:1.4.4")
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With