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

Ashu Tyagi

03/21/2023, 1:51 PM
Hey folks, So I created a pure kotlin KMM library (i.e., all the code is in
shared:commonMain
)
& published it as a package on Github packages. Then I added it as a gradle dependency in my commonMain but it is not accessible in my project's commonMain package. It is accessible in the androidMain package. I guess the reason is the artifact is being published as
.arr
Any idea on how to fix this? I want to be able to access the published library from commonMain.
m

mkrussel

03/21/2023, 1:52 PM
You cannot access a Android specific library in commonMain. How would you expect Kotlin to call it on the other platforms? What happens when it calls an Android API?
a

Ashu Tyagi

03/21/2023, 1:53 PM
The library is pure kotlin (no java/android specific imports)
m

mkrussel

03/21/2023, 1:54 PM
One option is to create a wrapper around the library. You can use expect/actual or define in interface that the platform specific code will use to inject the correct version. You will either have to make the other platforms do no ops for everything or find a library for the other platforms that do the same thing.
I misunderstood the problem
This is likely then a publishing problem. You need to publish all the platforms to the maven repository and then add a dependency on the non platform specific dependency.
p

Pablichjenkov

03/21/2023, 1:57 PM
What happens if you use mavenLocal?
m

mkrussel

03/21/2023, 1:58 PM
mavenLocal should behave the same, but I don't have a lot of experience publishing to either.
a

Ashu Tyagi

03/21/2023, 1:59 PM
It is the same behaviour with mavenLocal.
m

mkrussel

03/21/2023, 1:59 PM
Also does the library you publish have the same platforms as the one trying to consume it?
p

Pablichjenkov

03/21/2023, 2:00 PM
Just run the publishtomavenlocal task
In your home dir check .m2 directory
a

Ashu Tyagi

03/21/2023, 2:01 PM
let me give it a try & update.
p

Pablichjenkov

03/21/2023, 2:02 PM
Make sure you see all the platforms you care of with respective klibs and jars
a

Ashu Tyagi

03/21/2023, 2:10 PM
Below is my build.gradle script:
Copy code
import java.io.FileInputStream
import java.util.*

@Suppress("DSL_SCOPE_VIOLATION")
plugins {
    alias(libs.plugins.gradleVersions)
    alias(libs.plugins.kotlinSerialization)
    id("com.android.library") version libs.versions.android.gradle.plugin.get() apply false
    kotlin("multiplatform") version libs.versions.kotlin.get() apply false
    id("maven-publish")
}

val properties = File(rootDir, "gradle.properties").inputStream().use {
    Properties().apply { load(it) }
}

val githubProperties = Properties()
githubProperties.load(FileInputStream(rootProject.file("github.properties")))

fun getLibGroupId(): String = "com.internal.library"
fun getLibArtifactId(): String = "core-network"
fun getLibVersion(): String = "0.0.1"

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

afterEvaluate {
    publishing {
        publications {
            create<MavenPublication>("release") {
                run {
                    groupId = getLibGroupId()
                    artifactId = getLibArtifactId()
                    version = getLibVersion()
                    artifact("$rootDir/shared/build/outputs/aar/shared-release.aar")
                }
            }
        }

        repositories {
            maven {
                name = "GitHubPackages"
                url = uri("<https://maven.pkg.github.com/Demo/Core-Network>")
                credentials {
                    username = githubProperties.getProperty("gprUser") ?: System.getenv("GPR_USER")
                    password =
                        githubProperties.getProperty("gprKey") ?: System.getenv("GPR_API_KEY")
                }
            }
        }
    }
}
These are all the klibs & jars
m

mkrussel

03/21/2023, 2:15 PM
You configured the code to only publish the aar, so you are not publishing a multiplatform library.
p

psh

03/21/2023, 2:15 PM
The kotlin multiplatform plugin will automatically register artifacts with the maven publishing plugin for the configured platforms. Try commenting out the “artifact” line, and see if more turns up in your
.m2/repository
folder.
If your library is called “foo” you’ll see a single published artifact for each platform (“foo-<platform>“) and a “foo” that contains the gradle metadata publication
a

Ashu Tyagi

03/21/2023, 2:23 PM
I tried commenting it out. But it's still the same. Also, I don't see any .m2/repository folder. Am I doing something wrong?
m

mkrussel

03/21/2023, 2:31 PM
Did you look in your home directory for the .m2 directory? Also I think your how publications section is wrong and can probably be removed. https://kotlinlang.org/docs/multiplatform-publish-lib.html You will need to add
Copy code
kotlin {
    android {
        publishLibraryVariants("release", "debug")
    }
}
p

Pablichjenkov

03/21/2023, 2:38 PM
Yeah, remove the artifact line.
Can you show your publish tasks, in the gradle right tab
a

Ashu Tyagi

03/21/2023, 2:54 PM
@Pablichjenkov here
p

Pablichjenkov

03/21/2023, 3:02 PM
Looks good
Just hit the last one publishtomavenlocal and check ~/.m2/repository
Your Library should be there
a

Ashu Tyagi

03/21/2023, 3:19 PM
If I'm removing
artifact
from the build script then only .pom file is being published, nothing else.
m

mkrussel

03/21/2023, 3:20 PM
Try removing the whole publication section and then add the android specific thing in the Kotlin android block.
a

Ashu Tyagi

03/21/2023, 3:20 PM
okay
p

Pablichjenkov

03/21/2023, 3:21 PM
Also, did you apply the maven publish plugin to your shared project?
The presence of apply false in other plugins make me think you posted your gradle file from the parent project
m

mkrussel

03/21/2023, 3:24 PM
You might want to walk through Jetbrains's tutorial I posted above to see how you differ from them.
a

Ashu Tyagi

03/21/2023, 3:28 PM
Also, did you apply the maven publish plugin to your shared project?
This was the missing piece. Now everything is working as expected.
Thanks a lot for the help. Really appreciated🙏 @Pablichjenkov @mkrussel @psh
89 Views