https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

Jordi Saumell

03/30/2021, 2:48 PM
Hello everyone! I am trying to split my kmp ‘shared’ module into smaller modules and I am stuck. Inside the ‘shared’ module I’ve created a new multiplatform module (I tried with java/kotlin library too, but I was not able to import it). I have included it in “commonMain” as “api(project(“sharedcore”))” and it syncs correctly, but when I try to use a class which is in the shared:core module (from the shared module) it cannot be reached. I have also tried to look into the kmpp-template from moko but as most of the gradle setup is done by their library I was not able to understand how to set it up.  I attach the gradle files in a thread. Any hint or reference to a guide will be much appreciated! Thank you!
✔️ 1
shared module build.gradle:
Copy code
plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("org.jetbrains.kotlin.native.cocoapods")
    id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"
}

// CocoaPods requires the podspec to have a version.
version = "1.0"

android {
    val versionConfig = Version()
    compileSdkVersion(AndroidSdk.compile)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(AndroidSdk.min)
        targetSdkVersion(AndroidSdk.target)
        versionCode = versionConfig.versionCode
        versionName = versionConfig.versionName
    }
}

// workaround for <https://youtrack.jetbrains.com/issue/KT-43944>
android {
    configurations {
        create("androidTestApi")
        create("androidTestDebugApi")
        create("androidTestReleaseApi")
        create("testApi")
        create("testDebugApi")
        create("testReleaseApi")
    }
}

kotlin {
    val sdkName: String? = System.getenv("SDK_NAME")

    val isiOSDevice = sdkName.orEmpty().startsWith("iphoneos")
    if (isiOSDevice) {
        iosArm64("ios")
    } else {
        iosX64("ios")
    }

    android()
//    jvm()

    cocoapods {
        // Configure fields required by CocoaPods.
        summary = "Some description for a Kotlin/Native module"
        homepage = "Link to a Kotlin/Native module homepage"
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(Dependencies.Coroutines.common) {
                    version {
                        strictly(Versions.coroutines)
                    }
                }
                // koin
                api(Dependencies.Koin.core)
                api(Dependencies.Koin.test)
                // kermit
                api(Dependencies.Other.kermit)

                api(project(":shared:core"))
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation(Dependencies.Android.lifecycle)
                implementation(Dependencies.Compose.runtimeSaveable)
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation(Dependencies.Test.junit)
            }
        }
        val iosMain by getting
        val iosTest by getting

    }
}

multiplatformSwiftPackage {
    packageName("KMMBase")
    swiftToolsVersion("5.3")
    targetPlatforms {
        iOS { v("13") }
    }
}
core module (inside of shared) build.gradle:
Copy code
plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("org.jetbrains.kotlin.native.cocoapods")
    id("com.chromaticnoise.multiplatform-swiftpackage")
}

// CocoaPods requires the podspec to have a version.
version = "1.0"

android {
    val versionConfig = Version()
    compileSdkVersion(AndroidSdk.compile)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(AndroidSdk.min)
        targetSdkVersion(AndroidSdk.target)
        versionCode = versionConfig.versionCode
        versionName = versionConfig.versionName
    }
}

// workaround for <https://youtrack.jetbrains.com/issue/KT-43944>
android {
    configurations {
        create("androidTestApi")
        create("androidTestDebugApi")
        create("androidTestReleaseApi")
        create("testApi")
        create("testDebugApi")
        create("testReleaseApi")
    }
}

kotlin {
    val sdkName: String? = System.getenv("SDK_NAME")

    val isiOSDevice = sdkName.orEmpty().startsWith("iphoneos")
    if (isiOSDevice) {
        iosArm64("ios")
    } else {
        iosX64("ios")
    }

    android()
//    jvm()

    cocoapods {
        // Configure fields required by CocoaPods.
        summary = "Some description for a Kotlin/Native module"
        homepage = "Link to a Kotlin/Native module homepage"
    }

    sourceSets {
        val commonMain by getting
        val commonTest by getting
        val androidMain by getting
        val androidTest by getting
        val iosMain by getting
        val iosTest by getting
    }
}
Also, I have noticed that in the moko kmpp-template they export libraries. Is that necessary for the library to be consumed from the ‘shared’ module? And is it necessary to have access to some classes from the android/ios code?
j

Javier

03/30/2021, 3:03 PM
if you run gradlew build, it works?
If I understand, you should be able with that setup to use the core classes inside shared module
j

Jordi Saumell

03/30/2021, 3:12 PM
it does not build. It says it cannot resolve several test libraries in ‘shared’, but it could before adding the module
j

Javier

03/30/2021, 4:10 PM
I would try to add those libraries manually
if it works, then you know what is the problem
indeed, project A can't use project B tests dependencies
so that should be the problem
j

Jordi Saumell

03/30/2021, 4:37 PM
I’ve tried both adding the same test libraries that I have in shared in the shared:core gradle; and removing all the
val XTest by getting
from shared:core but none of this works. I don’t know if you meant this or something else
ok, removing the
val XTest by getting
from the shared gradle fixes the build, but I still cannot use the classes from ‘shared:core’ in ‘shared’
j

Javier

03/30/2021, 4:51 PM
Is it public?
j

Jordi Saumell

03/30/2021, 4:53 PM
the repo? I can give you access
I’ve noticed something else. The color highlighting doesn’t work on shared:core, and if I try to add a file Kotlin does not show up as an option. There seems to be something wrong with the kotlin setup
Ok I’ve figured it out. I had to remove
Copy code
id("com.chromaticnoise.multiplatform-swiftpackage")
from the core gradle. Now it works. Sorry and thank you!
3 Views