Hey folks, So I created a pure kotlin KMM library...
# multiplatform
a
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
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
The library is pure kotlin (no java/android specific imports)
m
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
What happens if you use mavenLocal?
m
mavenLocal should behave the same, but I don't have a lot of experience publishing to either.
a
It is the same behaviour with mavenLocal.
m
Also does the library you publish have the same platforms as the one trying to consume it?
p
Just run the publishtomavenlocal task
In your home dir check .m2 directory
a
let me give it a try & update.
p
Make sure you see all the platforms you care of with respective klibs and jars
a
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
You configured the code to only publish the aar, so you are not publishing a multiplatform library.
p
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
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
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
Yeah, remove the artifact line.
Can you show your publish tasks, in the gradle right tab
a
@Pablichjenkov here
p
Looks good
Just hit the last one publishtomavenlocal and check ~/.m2/repository
Your Library should be there
a
If I'm removing
artifact
from the build script then only .pom file is being published, nothing else.
m
Try removing the whole publication section and then add the android specific thing in the Kotlin android block.
a
okay
p
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
You might want to walk through Jetbrains's tutorial I posted above to see how you differ from them.
a
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
126 Views