Cannot import dependecies in kotlin multiplatform mobile - kotlin

I created a new kotlin multiplatform mobile project. I followed official documentation.
Basic project is working, I was able to build it on android without problems.
I wanted to add some api and I found ktor, which I have never used before. I followed docs here: https://kotlinlang.org/docs/mobile/use-ktor-for-networking.html and tutorial here: https://proandroiddev.com/kotlin-multiplatform-very-beginners-guide-part-2-api-d54f7326dc57 and all changes I did are:
I added ktor libraries into build.gradle.kts(:shared):
sourceSets {
val commonMain by getting {
dependencies {
implementation ("io.ktor:ktor-client-core:1.5.0")
}
}
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.1")
implementation("io.ktor:ktor-client-android:1.5.0")
}
}
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:1.5.0")
}
}
val iosTest by getting
}
And I created Api class where I wanted to create and use the HttpClient:
class Api() {
private val client = HttpClient()
suspend fun fetch(): String {
return ""
}
}
BUT HttpCLient() is "Unresolved reference" and it cannot be imported. I also tried to manually add import io.ktor.client.HttpClient
but io is "Unresolved reference".
Also I tried numerous rebuilds/cleans/syncs.
What am I doing wrong? Am I missing something?

I encountered the same problem on windows 7 with Android Studio 4.1.3. It was solved when in the build script of the project, in the dependencies section of the buildscript, it was updated the version of the kotlin-gradle-plugin artifact from 1.4.10 to 1.4.32. Below is a copy paste from project build.gradle.kts:
buildscript {
repositories {
gradlePluginPortal()
jcenter()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32")
classpath("com.android.tools.build:gradle:4.1.3")
}
}

Related

IntelliJ Show an Error on kotlinx.serialization, but compiling works

I have a problem where IntelliJ tells me that the #Serializable annotation cannot be accessed as it's internal to kotlin.io etc. The kotlinx.serialization package also shows as an unresolved reference.
Apparently, this has something to do with using Kotlin 1.7.x as when I downgrade to Kotlin 1.6.10 the issue no longer presents itself. However, Gradle compiles and runs the application without issue even on 1.7.10. Therefore, this seems like ultimately an issue with IntelliJ and/or one of my settings.
This is a Kotlin Multiplatform project. I've tried many things to fix this:
Reloading Gradle Project
Refreshing Gradle Dependencies
Completely refreshing all project indexes via the new Repair IDE tool
Deleting .idea and .gradle folders
Adjusting the project structure versions
Generally messing around with versions and settings
My build.gradle.kts:
plugins {
application
kotlin("multiplatform") version Versions.kotlin
kotlin("plugin.serialization") version Versions.kotlin
}
group = "com.example"
version = "4.0"
repositories {
mavenCentral()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "17"
}
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
js {
binaries.executable()
browser {
commonWebpackConfig {
cssSupport.enabled = true
}
webpackTask {
outputFileName = "app.js"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:${Versions.ktor}")
implementation("io.ktor:ktor-client-websockets:${Versions.ktor}")
implementation("io.ktor:ktor-serialization-kotlinx-json:${Versions.ktor}")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.serialization}")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jvmMain by getting {
dependencies {
implementation("io.ktor:ktor-server-core:${Versions.ktor}")
implementation("io.ktor:ktor-server-auth:${Versions.ktor}")
implementation("io.ktor:ktor-serialization:${Versions.ktor}")
implementation("io.ktor:ktor-server-html-builder:${Versions.ktor}")
implementation("io.ktor:ktor-client-cio:${Versions.ktor}")
implementation("io.ktor:ktor-server-cio:${Versions.ktor}")
implementation("io.ktor:ktor-server-websockets:${Versions.ktor}")
}
}
val jvmTest by getting
val jsMain by getting {
dependencies {
implementation("io.ktor:ktor-client-js:${Versions.ktor}")
implementation("io.ktor:ktor-client-json:${Versions.ktor}")
implementation("io.ktor:ktor-client-auth:${Versions.ktor}")
implementation("io.ktor:ktor-client-websockets:${Versions.ktor}")
implementation("io.ktor:ktor-client-serialization:${Versions.ktor}")
implementation("org.jetbrains.kotlin-wrappers:kotlin-react:${Versions.react}")
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom:${Versions.react}")
}
}
val jsTest by getting
}
}
application {
mainClass.set("com.example.app.ServerKt")
}
tasks.named<Copy>("jvmProcessResources") {
val jsBrowserDistribution = tasks.named("jsBrowserDistribution")
from(jsBrowserDistribution)
}
tasks.named<JavaExec>("run") {
dependsOn(tasks.named<Jar>("jvmJar"))
classpath(tasks.named<Jar>("jvmJar"))
}
object Versions {
const val ktor = "2.0.3"
const val kotlin = "1.7.10"
const val coroutines = "1.6.3"
const val serialization = "1.3.3"
const val react = "18.2.0-pre.358-compat"
}
The class that is having issues is:
import kotlinx.serialization.Serializable
#Serializable
data class Customer(val id: Int, val firstName: String, val lastName: String)
I appreciate any help I can get

Imports not resolving in generated GraphQL queries with Apollo3

I've noticed that when I generate class files from GraphQL queries, the imports for Apollo3 aren't resolving. See the screenshot below.
I'm using the Apollo3 GraphQL library in a Kotlin Multiplatform project. The Apollo Get Started With Multiplatform docs are pretty confusing and I may have my project configured incorrectly.
I can download the schema, and generate the files without a problem.
./build.gradle.kts
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21")
classpath("com.android.tools.build:gradle:7.0.0")
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
./shared/build.gradle.kts
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
id("com.apollographql.apollo3").version("3.0.0-alpha03")
}
kotlin {
android()
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iosTarget("ios") {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.apollographql.apollo:apollo-runtime-kotlin:2.5.7")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting
val iosTest by getting
}
}
android {
compileSdkVersion(30)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(23)
targetSdkVersion(30)
}
}
I was having the same issue. In my case, it was fixed by making sure the runtime dep is using the same version as the apollo plugin applied (3.0.0-alpha03).
So try changing the content in ./shared/build.gradle.kts file, replacing this line
implementation("com.apollographql.apollo:apollo-runtime-kotlin:2.5.7")
with
implementation("com.apollographql.apollo:apollo-runtime-kotlin:3.0.0-alpha03")

Kotlin Multiplatform Mobile: Project already has a CocoaPods dependency with name SDWebImage

I have created a demo project to integrate cocoapods into the KMM project. I have followed this link from the official website. At step 3 while reimporting the project, I am receiving the following error.
Project already has a CocoaPods dependency with name SDWebImage , after which I am unable to even import this lib in KMM.
Can anyone please help with this?
Update: Adding build.gradle.kts
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
kotlin("native.cocoapods")
}
// CocoaPods requires the podspec to have a version.
version = "1.0"
kotlin {
android()
ios {
cocoapods {
// Configure fields required by CocoaPods.
summary = "Some description for a Kotlin/Native module"
homepage = "Link to a Kotlin/Native module homepage"
pod("SDWebImage")
// You can change the name of the produced framework.
// By default, it is the name of the Gradle project.
frameworkName = "shared"
}
}
sourceSets {
val commonMain by getting
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.1")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting
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)
When you’re using cocoapods plugin you don’t need to manually declare packForXcode target, maybe that’s the problem. Try to remove everything after val packForXcode
cocoapods section should be inside kotlin, not inside ios

How to share kotlin multiplatform targets between submodules?

After an eternity I managed to setup a kotlin multi-platform and multi-module project. As there is almost no proper guide or documentation, I have just one more issue I couldn't solve.
Can I share kotlin multiplatform targets between the submodules?
Right now every submodule has a build.gradle.kts containing:
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "15"
}
}
js(IR) {
nodejs()
browser {
testTask {
useKarma {
useChromeHeadless()
webpackConfig.cssSupport.enabled = true
}
}
}
}
}
Obviously, I want all modules to compile to the same platforms using the same versions and it would be nice to not copy this snippet for every module.
Not to mention that the test dependencies are identical as well.
sourceSets {
val commonTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-api:$kotestVersion")
implementation("io.kotest:kotest-assertions-core:$kotestVersion")
implementation("io.kotest:kotest-property:$kotestVersion")
}
}
val jvmTest by getting {
dependsOn(commonTest)
dependencies {
implementation("io.kotest:kotest-runner-junit5:$kotestVersion")
}
}
val jsTest by getting {
dependsOn(commonTest)
dependencies {
implementation("io.kotest:kotest-framework-engine:$kotestVersion")
}
}
}

Cannot update kotlin to v1.3.50 for multiplatform project

I'm using kotlin 1.3.41 for my multiplatform project (included ios, jvm, js) and everything is ok. Now I'm trying to update to the latest version 1.3.50 but get this error:
Could not determine the dependencies of task ':kotlinNpmInstall'.
> org.jetbrains.kotlin.gradle.targets.js.npm.KotlinNpmResolutionManager$ResolutionState$Installed cannot be cast to org.jetbrains.kotlin.gradle.targets.js.npm.KotlinNpmResolutionManager$ResolutionState$Configuring
I have tried to search everywhere (github, kotlin issue tracker) but got nothing. Does anyone know what causes this error? Thanks.
Part of my configuration:
Root project configuration
buildscript {
apply from: "buildsystem/dependencies.gradle"
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/kotlinx/" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath deps.gradlePlugins.android
classpath deps.gradlePlugins.androidNavigation
classpath deps.gradlePlugins.node
classpath deps.gradlePlugins.kotlin
classpath deps.gradlePlugins.kotlinSerialization
classpath deps.gradlePlugins.kotlinAtomicfun
classpath deps.gradlePlugins.dokka
}
}
Multiplatform module configuration
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'kotlinx-serialization'
kotlin {
targets {
jvm("jvm")
js("js") {
nodejs {}
}
iosArm64("ios64")
iosX64("iosSim")
configure([ios64, iosSim]) {
binaries.framework {
baseName = "LIB"
}
}
}
sourceSets {
def commonDependencies = rootProject.ext.deps.common
commonMain {
dependencies {
implementation commonDependencies.kotlin
implementation commonDependencies.kotlinCoroutines
}
}
commonTest {
dependencies {
implementation commonDependencies.kotlinTest
implementation commonDependencies.kotlinTestAnnotations
implementation commonDependencies.mockk
}
}
def jvmDependencies = rootProject.ext.deps.jvm
jvmMain {
dependencies {
implementation jvmDependencies.kotlin
implementation jvmDependencies.kotlinCoroutines
}
}
jvmTest {
dependencies {
implementation jvmDependencies.kotlinTest
implementation jvmDependencies.kotlinTestJunit
implementation jvmDependencies.kotlinCoroutinesTest
implementation jvmDependencies.mockk
}
}
def jsDependencies = rootProject.ext.deps.js
jsMain {
dependencies {
implementation jsDependencies.kotlin
implementation jsDependencies.kotlinCoroutines
// npm
implementation npm("uuid", "^3.3.2")
}
}
jsTest {
dependsOn jsMain
dependsOn commonMain
dependencies {
implementation jsDependencies.kotlinTest
}
}
def iosDependencies = rootProject.ext.deps.ios
iosMain {
dependencies {
implementation iosDependencies.kotlinCoroutines
}
}
ios64Main {
dependsOn iosMain
}
iosSimMain {
dependsOn iosMain
}
}
}
// other configuration
I can confirm that the issue is in Android Studio.
When I run the official KotlinJS react app tutorial on IntelliJ, the app works. But on AS, I get the above error when executing any Gradle task dependent on npm.
Until the issue is resolved, I recommend using the Community version of IntelliJ. You can download it here