3

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: import in the Common module import in the Android module

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 Issue

I googled it and it seems that the error notification is incorrect?

Toan Pham
  • 93
  • 8
  • 1
    I have the same resolution error in Android Studio 4.1.2, 4.2 beta 6, and Arctic Fox 2020.3.1 although the project compiles without any errors. Most likely this is a bug in the KMM plugin. – Aleksei Tirman Mar 15 '21 at 10:20
  • @AlekseiTirman is correct even I have faced this a lot of times and sometime i still face this. Normally i do `gradle` sync again and that works for me but few times I need to do `Invalidate Cache and restart`. Please try adding your code and run if that works then you don't have any issues except from the KMM Plugin itself. – WhiteSpidy. Mar 16 '21 at 06:26
  • I've tried sync gradle and invalidate cache + restart Android Studio, but none of them worked. I'm still lost in this issue! – Jeremias Moraes Mar 22 '21 at 10:07

1 Answers1

5

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()
    }
}
madim
  • 774
  • 10
  • 22