作为 Gradle 项目集成到 Android 应用程序中时,KMM 项目上缺少公共类

发布于 2025-01-12 16:56:46 字数 1122 浏览 2 评论 0原文

我有一个现有的 Android 应用程序,我喜欢将 KMM 库项目集成为 git 子模块。我们希望在本地构建上集成 git 子模块,并在 CI 构建上使用 Maven 存储库依赖项。

为了实现这一目标,我通过 includeBuild 在我的 settings.gradle 中添加了子模块的目录,并替换了特定模块的所有依赖项:

includeBuild('my-kmm-library-project') {
    dependencySubstitution {
        substitute module('com.example.any-kmm-artifact:any') using project(':any')
    }
}

添加

implementation 'com.example.any-kmm-artifact:any:1.0.0'

到我的应用程序 build。 gradle 文件允许访问 android 源集中子模块 /my-kmm-library-project/any/src/androidMain 中的所有类和包。但我无法选择从 commonMain 源集中导入“类”。我已经尝试让 androidMain 依赖于 commonMain。

kmm/build.gradle.kts:

val androidMain by getting {
    dependsOn(commonMain)
    ...

}

这不会导致任何成功。

我还尝试在 KMM 项目上添加 jvm() 目标并让 jvmMain 依赖于 androidMain,但这会产生编译错误,导致 Android无法解析 SDK 类。

我认为主要问题是,这些类只是编译到可以组装的aar中。但该模块被集成为 gradle 项目实现。

是否有 KMM 选项来编译“公共类”并通过 gradle 模块访问它们?

I have an existing Android app where I like to integrate a KMM library project as a git submodule. We want to integrate a git submodule on local builds and using a maven repository dependency on CI builds.

To achieve this, I added the directory of the submodule in my settings.gradle via includeBuild and substitute all dependencies of a specific module:

includeBuild('my-kmm-library-project') {
    dependencySubstitution {
        substitute module('com.example.any-kmm-artifact:any') using project(':any')
    }
}

Adding

implementation 'com.example.any-kmm-artifact:any:1.0.0'

to my application build.gradle file allows access to all classes and packages in the android sourceset int the submodule /my-kmm-library-project/any/src/androidMain. But I have no option to import "classes" from the commonMain sourceset. I already tried to let androidMain depend on commonMain.

kmm/build.gradle.kts:

val androidMain by getting {
    dependsOn(commonMain)
    ...

}

This doesn't lead to any success.

I also tried to add a jvm() target on KMM project and let jvmMain depend on androidMain, but this produces compile error, cause the Android SDK classes cannot be resolved.

I think the main problem is that, the classes are just compiled into the aar which can be assembled. But the module is integrated as a gradle project implementation.

Is there a KMM option to compile "common classes" and access them through a gradle module?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久夏青 2025-01-19 16:56:46

“但我无法选择从 commonMain 源集中导入“类””

在 Android 项目中,在将 KMM 库包含为 *.aar 后。

您只能从 KMM 库的 /androidMain(而不是 /commonMain)正确导入*使用“类”

因此,请使用这种方式expect -actual方法进行继承

  • /commonMain:expect方法

  • /androidMain:实际方法

"But I have no option to import "classes" from the commonMain sourceset"

At Android project, after included KMM Library as *.aar.

You only import * use "classes" correctly from /androidMain of KMM Library (not /commonMain)

Therefore, use this way expect - actual methods for inheritance

  • /commonMain: expect method

  • /androidMain: actual method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文