Shawn Karber_
03/12/2020, 8:19 PMoctylFractal
03/12/2020, 8:20 PMoctylFractal
03/12/2020, 8:20 PMShawn Karber_
03/12/2020, 8:24 PMimport org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
/*
These plugins are declaring the plugins our gradle build will need in order to use android and
kotlin multiplatform.
*/
plugins {
id("com.android.library")
kotlin("multiplatform")
}
/*
These plugins tell gradle to include android platform libraries so that we may access them in the
kotlin files associated with the android build of the project.
*/
apply(plugin = "com.android.library")
apply(plugin = "kotlin-android-extensions")
/*
In order to use the android plugins, we must tell gradle which versions of android we are
supporting.
*/
android {
compileSdkVersion(29)
defaultConfig {
minSdkVersion(23)
targetSdkVersion(29)
}
}
/*
This block informs gradle what our intentions are with the kotlin code being transpiled. The iOS
target is conditionally selected via the system environment, and used if the environment is iOS.
Then the iOS code is transpiled down to native code, as well as the android code being transpiled
to android bytecode.
*/
kotlin {
// Select the 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
// Describe the framework being built for the chosen iOS target.
iOSTarget("ios") {
binaries {
framework {
baseName = "shiftHDsdk"
}
}
}
/*
Targeting android instead of jvm gives us an .aar file instead of a .jar, but gives us access
to android platform libraries through the "com.android.library" plugin applied above.
*/
android()
/*
Source sets are collections of Kotlin source code, dependencies, and settings which facilitate
the compilation into various targets. The common source set calls for the use of features
shared amongst platforms, while platform specific source sets exist for platforms requiring
specific compilation commands.
*/
sourceSets["commonMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.3.70")
implementation("io.ktor:ktor-client-core:1.3.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.4")
}
sourceSets["androidMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.3.70")
implementation("io.ktor:ktor-client-android:1.3.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4")
}
sourceSets["iosMain"].dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.4")
}
}
/*
Task for building of the xcode-framework allowing for importation of the code into an xcode
project.
*/
val packForXcode by tasks.creating(Sync::class) {
val targetDir = File(buildDir, "xcode-frameworks")
/*
Select the right configuration for the iOS framework dependant on the environment variables
set by the 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 ./gradlew wrapper with embedded Java path for iOS framework compilation.
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)
Shawn Karber_
03/12/2020, 8:24 PMShawn Karber_
03/12/2020, 8:24 PMShawn Karber_
03/12/2020, 8:24 PMsourceSets["commonMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.3.70")
implementation("io.ktor:ktor-client-core:1.3.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.4")
}
octylFractal
03/12/2020, 8:26 PMShawn Karber_
03/12/2020, 8:30 PMoctylFractal
03/12/2020, 8:30 PMoctylFractal
03/12/2020, 8:31 PMoctylFractal
03/12/2020, 8:31 PMShawn Karber_
03/12/2020, 8:33 PMlouiscad
03/13/2020, 9:09 AMasync
is not resolved is expected: it needs a coroutine scope, to avoid breaking structured concurrency. Same for launch
. You need a root or local CoroutineScope
. runBlocking
should be available anywhere though.louiscad
03/13/2020, 9:10 AMrunBlocking
on Android or iOS.