Hi! I have 2 KMP libraries ( A and B). B is import...
# multiplatform
c
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?
k
Can you share details like part of gradle file etc
c
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