Hello folks! Hope you are all doing great! I am f...
# gradle
h
Hello folks! Hope you are all doing great! I am facing some issues with Publishing Android library setup. Any help/pointers would be greatly appreciated. I have a KMP project with multi-module setup. Module-1:
CoreModule
Module-2:
FeatureModule
FeatureModule
references
CoreModule
internally using
api(project(path = ":CoreModule))
. The final artifact
FeatureModule
after publishing to maven/maven local is supposed to be integrated in main Android app. So, now my expectation is to have the code within
CoreModule
to be accessible inside the main Android app since
FeatureModule
includes the
CoreModule
. But, I can’t access the code/classes within
CoreModule
inside the main Android app. This is a KMP project which I am not so familiar with. So, not sure if it is a KMP thing or something else. Any idea what could be missing in this setup? I am attaching reference code in the thread.
Copy code
======build.gradle.kts=========
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework

plugins {
  alias(libs.plugins.kotlinMultiplatform)
  alias(libs.plugins.androidLibrary)

  id("maven-publish")
  id("com.example.kmpproject.library")
}

kmpProjectLibrary {
  publishedArtifactId.set("FeatureModule")
}

kotlin {
  androidTarget {
    publishLibraryVariants("release")

    compilations.all {
      kotlinOptions {
        jvmTarget = "1.8"
      }
    }
  }

  val xcf = XCFramework()
  listOf(iosArm64(), iosSimulatorArm64()).forEach {
    it.binaries.framework {
      export(project(":CoreModule"))


      xcf.add(this)
      baseName = "FeatureModule"
      isStatic = true
      binaryOption("bundleId", "com.example.kmmproject.FeatureModule")
    }
  }

  sourceSets {
    commonMain.dependencies {
      implementation(libs.ktor.client.core)

      // Including CoreModule       
      api(project(path = ":CoreModule"))


    }


    androidMain.dependencies {
     // Including CoreModule       
      api(project(path = ":CoreModule"))
    }

    
    iosMain.dependencies {
      implementation(libs.ktor.client.darwin)
    }
  }
}

android {
   
}
The Publishing artifact is handled using ConventionPlugin as below
Copy code
class LibraryConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            val extension =
                extensions.create<LibraryConventionPluginExtension>("kmmLibrary")
            configureArtifactoryPublishing {
                try {
                    extension.publishedArtifactId.get()
                } catch (exception: MissingValueException) {
                    null
                }
            }
        }
    }

    private fun Project.configureArtifactoryPublishing(artifactIdProvider: () -> String?) {
        configure<LibraryExtension> {
            configure<PublishingExtension> {
                publishing {
                    repositories {
                        maven {
                            url = "sample url"
                            version = "1.0"
                            group = "com.example.kmm"

                            credentials {
                                username = properties["USERNAME"] as? String ?: ""
                                password = properties["PASSWORD"] as? String ?: ""
                            }
                        }
                    }
                }
            }
        }
    }
}
t
@Anton Lakotka [JB] maybe you could help here?
a
Maybe you can provide some reproducer? Or a bit more details. Like
.*module
(you can find them in maven repository if you publish it locally) files of published libraries (CoreModule and FeatureModule). Or your androidApp depends on FeatureModule as project dependency? Because indeed transitive dependencies should work correctly by default. And it is not clear what went wrong
h
@Anton Lakotka [JB] Thanks for responding! Yes, the idea is that main Android app depends on
FeatureModule
as project dependency. The KMP project (which has
CoreModule
and
FeatureModule
is a separate project. When I try to publish artifact (maven & local maven) only for
FeatureModule
that internally locally referencing
CoreModule
, I am able to generate artifact only for
FeatureModule
which is want to achieve. But I also want to access the classes residing in
CoreModule
, when I call them from main Android app after adding
FeatureModule
artifact as a dependency. In general, all the functionality (code depending on
CoreModule
) works as expected as it is locally referenced from the
FeatureModule
which is finally integrated in main Android app. The only issue is that, from main app, I can’t call/access the classes residing in
CoreModule
although I am using
api
instead of
implementation
while referencing
CoreModule
inside
FeatureModule
. As per my understanding using
api
should make the classes in
CoreModule
available to the end consumer as well, not just to
FeatureModule
. But this is not happening. I hope you got the issue.
a
Hi! Sorry for long delays in responses.
I hope you got the issue.
Kinda. I'm still not quite sure what you're doing.
1. the idea is that main Android app depends on
FeatureModule
as project dependency.
If you mean this. then Android app,
FeatureModule
and
CoreModule
must be in the same Gradle Build
2. The KMP project (which has
CoreModule
and
FeatureModule
is a separate project
What do you mean separate project? One Gradle Build with two Sub-Projects? Then this contradicts with the Statement 1. As Android App can't have such project dependency.
When I try to publish artifact (maven & local maven) only for
FeatureModule
that internally locally referencing
CoreModule
, I am able to generate artifact only for
FeatureModule
which is want to achieve
If you publish
FeatureModule
that depends on
CoreModule
in runtime or compiletime doesn't matter. The
CoreModule
must be published as well. Unless you "embed" classes from CoreModule to FeatureModule. So I would prefer a reproducer here as I don't entirely sure where is the problem. from what I see rn, most likely your build is doing something wrong. And KMP is just making it way more harder. May I propose you to approach this problem from different perspective: 1. create 3 small projects: Android, Feature and Core. Make them all to be very basic ones. And Feature and Core not KMP but just simple JAVA/Kotlin JVM. 2. try to see if you can achieve your needs in this small setup. 3. then do next step and convert Feature and Core into KMP. 4. If, after converting to KMP, your setup started to fail. Then please come back and I'll try to help you. Perhaps there is indeed some migration issues in KMP world.