badvok
badvok

Reputation: 561

Kotlin / Native trouble importing android logs into android shared code module

I am trying to use Android logs in my shared code so wanted to make use of the 'expected/actual' functionality in order to make the android side use logs to be read in log cat. However I cannot get the android module(not app module) to import the android.util.Log.

I have seen this answer but it did not work for me. I cannot get the import to resolve.

I think I need to implement a specific dependency in order to have access to the import but I'm not sure what that is.

Here is my build.gradle.kts

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")
    implementation("io.ktor:ktor-client:1.0.0-beta-3")

    implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.2")
 //   implementation ("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.14.0")
}

sourceSets["androidMain"].dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib") //Allows _androidMain to have java imports
    implementation("io.ktor:ktor-client-android:1.0.0-beta-3")
    api("org.jetbrains.kotlin:kotlin-stdlib:1.3.61")
    api("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61")
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61")
    }
}

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)

Upvotes: 1

Views: 939

Answers (3)

Roman Vasilyev
Roman Vasilyev

Reputation: 159

Just fixed same issue, finally used this tutorial https://medium.com/icerock/how-to-start-use-kotlin-multiplatform-for-mobile-development-1d3022742178 so my build.gradle looks like:

apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.multiplatform'

android {
    compileSdkVersion 29
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 29
    }
}

kotlin {
    targets {
        android()
        iosArm64()
        iosX64()
    }

sourceSets {
    commonMain {
        dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
        }
    }
    androidMain {
        dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        }
    }
}
}

Upvotes: 1

R&#243;bert Nagy
R&#243;bert Nagy

Reputation: 7612

I had the same problem. Try using android() instead of only the jvm("android").

Also I've added my dependencies to android with android.sourceSets.foreach{ _ -> dependencies{ ... } }

Upvotes: 1

Artyom Degtyarev
Artyom Degtyarev

Reputation: 2888

Here you got JVM target with the name "android" instead of actually Android target. The same problem occurred in the linked question. Can you tell, what's going on when you use the script from the answer? It seems like that one should work correctly.
As described in the documentation, one has to use an Android-specific Gradle plugin to make the Android target available. If you want to see how it can be done, consider having a look at this sample.

Upvotes: 2

Related Questions