1

I have three targets commonMain/androidMain/iOSMain respectively. Because I need to access the assets in Android devices in androidMain module. I found I cannot use the Android API... The following is part of my build.gradle.kts:

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    id("com.android.application")
    kotlin("multiplatform")
}
repositories {
    google()
    jcenter()
}

android {
    compileSdkVersion(29)
    buildToolsVersion("29.0.1")
    defaultConfig {
        minSdkVersion(19)
        targetSdkVersion(29)
    }
    sourceSets {
        getByName("main") {
            manifest.srcFile("src/androidMain/AndroidManifest.xml")
            java.srcDirs(file("src/androidMain/kotlin"))
            res.srcDirs(file("src/androidMain/res"))
        }
    }
}

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 = "Example"
            }
        }
    }

    jvm("android")

    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
    }

    sourceSets["androidMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib")
    }

    sourceSets["commonTest"].dependencies {
        implementation ("org.jetbrains.kotlin:kotlin-test")
        implementation ("org.jetbrains.kotlin:kotlin-test-junit")
    }
}

How can I use Android library in androidMain? For example,

val inputStream = assets.open("Test.txt")
6
  • Maybe this paragraph can help? Commented Oct 28, 2019 at 14:26
  • I updated the build.gradle.kts as above. And I still cannot use val inputStream = assets.open("Test.txt") in androidMain. What's wrong with my configuration of multiplatform? Commented Oct 29, 2019 at 4:06
  • It looks like in the example you're using, there is no way to utilize Android SDK. I think, you're trying to use this sample? In there, SDK is available only for the app module, not the SharedCode. Maybe this article could help to deal with this problem, please habe a look. Commented Oct 29, 2019 at 7:57
  • Yes, I'd like to use Android SDK in androidMain in SharedCode instead of the app. I've read the article you provided but I still cannot use the Android SDK in the SharedCode... Have you tried it is possible? Cause I can access bundle for iOS using kotlin multiplatform SDK, but I cannot access for Android... it is really strange... I just want to access storage from the SharedCode. Commented Oct 29, 2019 at 9:42
  • Then try to add a dependency in the SharedCode script, as it is in the app. Also, I personally recommend you to join Kotlin Community Slack(get invite here). There are a lot of Kotlin/Native and MPP users, who can share more experience than I. Commented Oct 29, 2019 at 9:48

2 Answers 2

1

Make sure to create an AndroidManifest.xml with (library module) package name different than the (app module) package name underneath the SharedCode/src directory. And as Nagy Robi said use android() instead of jvm('android'). Please check the below references also:

Sign up to request clarification or add additional context in comments.

Comments

0

Try using android() instead of jvm('android') to load the target from the presets.

2 Comments

Yes, I've changed from jvm('android') to android(), but the IDE produce another error. It said "The feature multi platform projects is experimental and should be enabled explicitly" in my android's actual class. Is there any working example in the GitHub using android API as a reference?
Try adding enableFeaturePreview('GRADLE_METADATA') to the settings.gradle of your project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.