I have created a very simple KMP project, with the following structure:
-Root
--app
--gradle
--SharedCode
--src\commonMain\kotlin\actual.kt
--src\iosMain\kotlin\actual.kt
--scr\androidMain\kotlin\actual.kt
--build.gradle.kts
--native
--KotlinIOS
--iOS project (xcodeproj, etc)
Everything works, and the basic project work on both Android and iOS platforms.
But when I try to use an android-specific import statement in my androidMain directory, the import statement won't resolve:
import android.os.build // Android Studio can't find this
actual fun platformName(): String {
return "Android"
}
It is weird, since the iOS package is using iOS-specific imports successfully:
import platform.UIKit.UIDevice // This import works
actual fun platformName(): String {
return UIDevice.currentDevice.systemName() +
" " + UIDevice.currentDevice.systemVersion
}
Any suggestions about what I may need to configure to get my Android import to work?
This is my build.gradle.kts file for completeness:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
}
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("org.jetbrains.kotlin:kotlin-stdlib-common")
}
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)