Reputation: 1085
When I compile my kotlin multiplatform project I get Unresolved reference
on stuffs that I use in the common module. But the gradle synchronisation works fine. You can found my project here
In details:
I followed the tutorial from Jetbrain to build a mobile multiplatform project. Everything works fine.
Then I implemented inside the common module a ktor client to request an api (jsonplaceholder). I added all the required dependencies and everything seems good. All references are resolved, I have no errors.
But when I compile my project with make Project
then all the dependencies added to get ktor and so on cannot be resolved.
I guess I have a problem with the configuration of my project, when I run ./gradlew androidDependencies
it seems that there is a problem with SharedCode
since it is sometimes marked in the command´s output as \--- :SharedCode
.
The build.gradle
for the project is:
buildscript {
ext.kotlin_version = '1.3.71'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0-rc01'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
The build.gradle.kts
for the common SharedCode
module is:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
}
kotlin {
//select iOS target platform depending on the Xcode environment variables
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iOSTarget("ios") {
binaries {
framework {
baseName = "SharedCode"
}
}
}
jvm("android")
val serializationVersion = "0.20.0"
val ktorVersion = "1.3.2"
sourceSets["commonMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-json:$ktorVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")//Kotlinxserializer
}
sourceSets["androidMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib")
}
}
val packForXcode by tasks.creating(Sync::class) {
val targetDir = File(buildDir, "xcode-frameworks")
/// selecting the right configuration for the iOS
/// framework depending on the environment
/// variables set by Xcode build
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets
.getByName<KotlinNativeTarget>("ios")
.binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
from({ framework.outputDirectory })
into(targetDir)
/// generate a helpful ./gradlew wrapper with embedded Java path
doLast {
val gradlew = File(targetDir, "gradlew")
gradlew.writeText("#!/bin/bash\n"
+ "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
+ "cd '${rootProject.rootDir}'\n"
+ "./gradlew \$@\n")
gradlew.setExecutable(true)
}
}
tasks.getByName("build").dependsOn(packForXcode)
And the build.gradle
for the android app is:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.mpp.mpptest"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation project(':SharedCode')
}
Do you know what I am doing wrong?
I read Building Multiplatform Projects with Gradle but it did not help.
The only working project that I found is from KaMPKit. I have set my build.gradle(.kts)
files as they are in KaMPKit (see the branch Set_build_gradle_as_KaMPKit
) but the `unresolved references´ problem remains.
Upvotes: 3
Views: 5500
Reputation: 1085
I fixed my problem. The error came because I did not declare the plugin android.lybrary
.
Even if it is not necessary to fix the problem, I have decided to switch my build.gradle
files to kotlin DLS, I rewrote all the build.gradle
and I made the following changes for the shared module:
AndroidManifest.xml
com.android.library
pluginsandroid
At the end the build.gradle.kts
for the shared module looks like this:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("kotlinx-serialization")
id("com.android.library")
}
android {
compileSdkVersion(29)
defaultConfig {
minSdkVersion(Versions.min_sdk)
targetSdkVersion(Versions.target_sdk)
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
}
kotlin {
//select iOS target platform depending on the Xcode environment variables
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iOSTarget("ios") {
binaries {
framework {
baseName = "SharedCode"
}
}
}
jvm("android")
sourceSets["commonMain"].dependencies {
implementation(kotlin("stdlib-common", Versions.kotlin))
implementation(Deps.Ktor.commonCore)
implementation(Deps.Ktor.commonJson)
implementation(Deps.Coroutines.common)
implementation(Deps.Ktor.commonSerialization)
}
sourceSets["androidMain"].dependencies {
implementation(kotlin("stdlib", Versions.kotlin))
implementation(Deps.Coroutines.jdk)
implementation(Deps.Ktor.androidSerialization)
}
sourceSets["iosMain"].dependencies {
implementation(Deps.Ktor.iosSerialization)
}
}
val packForXcode by tasks.creating(Sync::class) {
val targetDir = File(buildDir, "xcode-frameworks")
/// selecting the right configuration for the iOS
/// framework depending on the environment
/// variables set by Xcode build
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets
.getByName<KotlinNativeTarget>("ios")
.binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
from({ framework.outputDirectory })
into(targetDir)
/// generate a helpful ./gradlew wrapper with embedded Java path
doLast {
val gradlew = File(targetDir, "gradlew")
gradlew.writeText("#!/bin/bash\n"
+ "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
+ "cd '${rootProject.rootDir}'\n"
+ "./gradlew \$@\n")
gradlew.setExecutable(true)
}
}
tasks.getByName("build").dependsOn(packForXcode)
All dependency version numbers come from a Dependencies.kt
files (see KaMPKit)
All dependencies are now resolved.
Upvotes: 1