https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

Shawn Karber_

03/12/2020, 8:19 PM
and no matter what I do, the content variable at the bottom, whether its async, launch, runBlocking, etc. none of them are able to be resolved
o

octylFractal

03/12/2020, 8:20 PM
did you add the coroutines common library?
s

Shawn Karber_

03/12/2020, 8:24 PM
Copy code
import 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)
thats my gradle file
i have it in the common dependencies, unless i didnt do it right
Copy code
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")
    }
o

octylFractal

03/12/2020, 8:26 PM
looks right to me
s

Shawn Karber_

03/12/2020, 8:30 PM
i was having issues with ktor the other day, too, seems like some times i have random problems getting libraries to function, at work I am on a mac, at home a pc, and i seem to have better luck on the pc, without understanding why, anyways, any other ideas I could try? i've been banging my head on this for a while
o

octylFractal

03/12/2020, 8:30 PM
not from my end, I don't work with MPP yet so I don't really know how it works
best I can suggest is to try importing to intellij again / invalidate caches
for me when something is weird it's because intellij's caches broke
s

Shawn Karber_

03/12/2020, 8:33 PM
ill give it a shot, thanks
l

louiscad

03/13/2020, 9:09 AM
The fact that
async
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.
Though you probably should never use
runBlocking
on Android or iOS.
3 Views