I followed the Intellij IDEA File->New Project-> (Kotlin Tab) -> Kotlin (Mobile Android/iOS) generator tool
to setup a project for testing a shared codebase between iOS and Android.
The coroutine setup works well for android but for some reason I can't get a clean build for iOS
with the following setup:
kotlin {
targets {
// For ARM, preset should be changed to presets.android_arm32 or presets.android_arm64
fromPreset(presets.android, 'android')
// For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
fromPreset(presets.iosX64, 'ios')
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
androidMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
}
}
androidTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
}
}
iosMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
}
}
iosTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
}
}
}
}
I get the following exception:
> Task :proj
proj/src/commonMain/kotlin/com/proj/sdk/Sample.kt:3:16: error: unresolved reference: coroutines
import kotlinx.coroutines.*
^
proj/src/commonMain/kotlin/com/proj/sdk/Sample.kt:17:5: error: unresolved reference: GlobalScope
GlobalScope.launch { delay(1000)
^
proj/src/commonMain/kotlin/com/proj/sdk/Sample.kt:17:26: error: unresolved reference: delay
GlobalScope.launch { delay(1000)
^
> Task :proj:compileKotlinIos FAILED
FAILURE: Build failed with an exception.
I do assume I did something wrong, but I i've tried using both native+common as dependency for iOS but it does not accept it. Can someone point me towards the right direction?
Can confirm the same issue happens for 0.30.2 and 0.30.2-eap13 and that this happens for implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:0.30.2" as dependency as well
What version of Gradle are you using? It currently works only when you use Gradle 4.7
Hi @elizarov 4.10.2 like the latest 1.3 guide for mpp guide described
Please, try with Gradle 4.7
@elizarov I changed to distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip
but the same issue is still there. I'm curious tho, since the latest dependency on actually being able to use Native, the documentation(https://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html) states "Kotlin/Native plugin requires a newer version of Gradle" do you have in mind that I should still use the old Konan setup?
I read a few of the other issues reported on this subject, this seems to be related to coroutines not yet supports using the plugin: 'kotlin-multiplatform' ?
Is this issue still valid? Should I use an older version of coroutine library with mpp?
@dcoletto This issue is still valid, the mpp-ios-android documentation does not match the recommendation for using coroutines. Judging from https://github.com/Kotlin/kotlinx.coroutines/issues/564 this seems to be quite an annoying fix cause it does not allow for a dynamic solution. Even tho as stated, I do get everything to work with Android, but it seems like the missmatch in gradle version is one of the reasons why it wont work for iOS (Just my own guess)
Hey, @Syrou.
First, you shouldn't add -common dependencies to the platform-specific (iosMain & iosTest in your case) modules, such a configuration is considered to be incorrect. So a proper one looks like:
kotlin {
targets {
fromPreset(presets.android, 'android')
// This preset is for iPhone emulator
// Switch here to presets.iosArm64 (or iosArm32) to build library for iPhone device
fromPreset(presets.iosX64, 'ios') {
// compilations.main.outputKinds('FRAMEWORK')
}
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
androidMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
}
}
androidTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
}
}
iosMain {
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
}
}
iosTest {
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
}
}
}
}
Second, kotlin-multiplatform is expected to work well with coroutines, you may take a look at KotlinConf app to get the idea. Note, though, that to be able to use coroutines with Native targets, you need to enable Gradle metadata in the project so far. This is subject to change and the requirement may be removed in the final release. Does it work for you this way?
Thanks for the reply @wild-lynx
As stated below my code snippet I did try with "kotlinx-coroutines-core-native" as dependency as well without any success.
The KotlinConf app uses an older setup than the one specified in https://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html, and from my tests only work if you use the -conf2 on the two more important dependencies, and I do not want to go back to something that is not gonna work with the 1.3.0RC/1.3.0
Try cloning the KotlinConf app and bump the versions up to the latest releases and you will notice it does not work anymore.
@Syrou You speak right from my soul - I did the same steps with the same result and do not know how to continue
@Flocksserver The issue seems to be that coroutines does rely on using an older Gradle implementation one that the rest of the 1.3.0RC wants them to. Sure I could be wrong, but since they are pointing towards that they want us to use Gradle version 4.7 it means that coroutines is not ready yet for the 1.3.0 Release as told by the IntelliJ interface Picture Link
It does not fully explain why it does not work if you downgrade to 4.7 and it still wont work, but I do understand it is part of the reason.
@Syrou, doesn't kotlinx-coroutines-core-native work even having Gradle version 4.7 and Gradle metadata enabled? Can you share a sample project, please, so I'd be able to take a look at the whole configuration and try to figure out what went wrong?
Also, the following versions should be compatible with each other:
* kotlin: 1.3.0-rc-146
* kotlinx.coroutines: 0.30.2-eap13
* ktor: 1.0.0-alpha-3
* kotlinx.atomifcu: 0.11.10-eap13
* kotlinx.io (http://kotlinx.io/): 0.1.0-alpha-17-rc13
* kotlinx.serialization: 0.8.2-rc-13
* Gradle: 4.7
@wild-lynx https://github.com/Syrou/kotlin-mpp-debug-project ./gradlew :SharedCode:build
contains a println setup to show which versions of the dependencies of interest I am using
@Syrou, source set names are case sensitive. Please use iOSMain.dependencies instead of iosMain.dependencies to get the required dependencies for the corresponding target.
Did it help?
@wild-lynx Well spotted! This does indeed solve the issue I am having with kotlinx.coroutines using gradle 4.7. I am guessing support for gradle 4.10.2 is coming soon(ish)
Meanwhile, I suggest your clarification is added to the documentation since both me and others have ran into the same issue!
Thank you, will do
oh well.... same here, thanks a lot! nevertheless my code does not compile, but thats another topic/problem with coroutines and "Job"... https://github.com/JetBrains/kotlin-native/issues/2167 =(
@Syrou try to add enableFeaturePreview('GRADLE_METADATA') in settings.gradle
@armcha, why do you suggest me to do so?
@wild-lynx Sorry, I fixed it
I have the same problem.
If i don't use coroutines it works perfect, but when i use "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.0.0" in iosMain - my project build FAILED.
This happens for me too when running 1.0.0 with previous fix suggestion, that fix being
iOSMain {
dependencies {
}
}
as the target option. When running without enableFeaturePreview('GRADLE_METADATA') I get
error: unresolved reference
to all coroutines when trying to compile iOS.
If I instead run with enableFeaturePreview('GRADLE_METADATA') I get
exception: java.lang.IllegalStateException: Could not find "/Users/RANDOM/.gradle/caches/modules-2/files-2.1/io.ktor/ktor-client-ios_debug_ios_x64/1.0.0-beta-3/e0b2ceb8cc0c95da9adb9001949b7f980d427f16/ktor-client-ios.klib"
I can confirm that the file actually does exist. So something is wrong here
@R12rus You can check me answering myself in https://github.com/Kotlin/kotlinx.coroutines/issues/805 I got it work work by bumping up kotlin native version
closed as outdated
Most helpful comment
Thanks for the reply @wild-lynx
As stated below my code snippet I did try with "kotlinx-coroutines-core-native" as dependency as well without any success.
The KotlinConf app uses an older setup than the one specified in https://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html, and from my tests only work if you use the
-conf2on the two more important dependencies, and I do not want to go back to something that is not gonna work with the 1.3.0RC/1.3.0Try cloning the KotlinConf app and bump the versions up to the latest releases and you will notice it does not work anymore.