https://kotlinlang.org logo
#gradle
Title
# gradle
c

Császár Ákos

03/07/2024, 1:30 PM
Hi! I have 2 KMP libraries ( A and B). B is imported to A with api() method. When I import A to an android project with implementation(), the classes from B are not visible. Can someone explain to me, what am I doing wrong?
c

CLOVIS

03/07/2024, 3:39 PM
Do you have a more concrete example? From what you said, it should work, so there's something missing.
c

Császár Ákos

03/08/2024, 7:34 AM
This is the gradle of the B library
Copy code
plugins {
    alias(libs.plugins.multiplatform.swiftpackage)
    alias(libs.plugins.app.cash.sqldelight)
    alias(libs.plugins.detektPlugin)

    kotlin("plugin.serialization") version libs.versions.kotlin
    kotlin("native.cocoapods")
    kotlin("multiplatform")

    id("com.android.library")
    id("kotlin-parcelize")
    id("maven-publish")
}

kotlin {

    // ---------------------------------  TARGETS ---------------------------------

    applyDefaultHierarchyTemplate()

    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
        publishLibraryVariants("release", "debug")
    }

    iosX64()
    iosArm64()
    iosSimulatorArm64()
    tvosArm64()
    tvosSimulatorArm64()

    js(IR) {
        moduleName = ""
        browser {
            webpackTask {
                mainOutputFileName.set("")
                output.library = ""
            }
            testTask {
                useKarma {
                    useChrome()
                }
            }
        }
        generateTypeScriptDefinitions()
        binaries.library()
        binaries.executable()

        // add sourceMaps to web
        binaries.withType<JsIrBinary>().all {
            linkTask.configure {
                kotlinOptions {
                    sourceMap = true
                    sourceMapEmbedSources = "always"
                }
            }
        }
    }

    targets.forEach { target ->
        target.compilations.all {
            kotlinOptions {
                freeCompilerArgs += listOf("-Xexpect-actual-classes")
            }
        }
    }

    // ---------------------------------  SOURCE SETS ---------------------------------

    sourceSets {
        val commonMain by getting {
            dependencies {
                api(libs.koin.core)
            }
        }

        val commonMobile by creating {
            dependsOn(commonMain)
            dependencies {
               ...
            }
        }

        appleMain {
            dependsOn(commonMain)
            dependsOn(commonMobile)
            dependencies {
               ....
            }
        }

        androidMain {
            dependsOn(commonMobile)
            dependencies {
                api(libs.koin.android)
            }
        }

        jsMain {
            dependencies {
               ...
            }
        }

        commonTest {
            dependencies {
                implementation(kotlin("test"))
            }
        }

        val androidUnitTest by getting {
            dependencies {
               ...
            }
        }
    }

    // ---------------------------------  PUBLISHING ---------------------------------

    cocoapods {
        summary = ""
        homepage = ""
        ios.deploymentTarget = "14.1"
        framework {
            baseName = ""
        }
    }

    multiplatformSwiftPackage {
        packageName("")
        swiftToolsVersion("5.3")
        targetPlatforms {
            iOS { v("13") }
            macOS { v("10_15") }
            tvOS { v("13") }
        }
    }

    val publicationsFromMainHost = listOf(js()).map { it.name } + "kotlinApiLib"

    publishing {
        publications {

            matching { it.name in publicationsFromMainHost }.all {
                val targetPublication = this@all
                tasks.withType<AbstractPublishToMaven>()
                    .matching {
                        it.publication == targetPublication
                    }
                    .configureEach { onlyIf { findProperty("isMainHost") == "true" } }
            }

            repositories {
                maven(url = "repoUrl") {
                    name = "name"
                    credentials {
                        username = "user"
                        password = "password"
                    }
                }
            }

            register<MavenPublication>("release") {
                groupId = "my.group.id"
                artifactId = mavenArtifactId
                version = libVersion

                afterEvaluate {
                    from(components["release"])
                }
            }
        }
    }
}

project.tasks.findByPath("jsProductionLibraryCompileSync")?.dependsOn("jsBrowserProductionWebpack")

android {
    defaultConfig {
        aarMetadata {
            minCompileSdk = 23
            minSdk = 23
        }
    }
    compileSdk = 33
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    testOptions {
        animationsDisabled = true
        unitTests.apply {
            isReturnDefaultValues = true
        }
    }
}
It is imported to the commonMain of library A with api(). The publishing logic for library A is the same, just the name and the version is different. Let me know if you need other information
For publishing to maven, I use ./gradlew commonbuild and ./gradlew publishAllPublicationsToMyRepoRepository
c

CLOVIS

03/08/2024, 8:24 AM
What kind of module is this? Why is it both a library and an executable?
Is the repository public?
Is it just IntelliJ that is complaining? or Gradle itself (do you have the same problem when running in the CLI?)
c

Császár Ákos

03/08/2024, 9:18 AM
Its a KMP module. When I publish it to maven looks like this. I tried to add the core (which is the common implementation of the module) to the commonMain source set of the lib A with the api() method, but the classes from lib B wasn't visible in an android project. Only the classes from lib B. After that I added the core-android to the androidMain source set of lib A with api(). This resolved the previous issue, but now I am getting
Duplicate class <MyClassName> found in modules core-debug-runtime (<LibA>:core-android-debug:0.0.18) and core-release-runtime (<LibA>:core-android:0.0.18)
when I try to run the android application. Unfortunately its not a public repo.
3 Views