android - 是否可以创建引用公共模块的正确目标的 kotlin 多平台项目?
问题描述
如果我们想用 Kotlin 构建一个多平台项目,并且我们有这样的结构:
common
android
android1
android2
backend
api1
api2
在common
模块中,我们有 3 个目标/预设:
jvm
(所有基于 jvm 的项目的通用代码)jvmAndroid
(与所有 jvm android 项目的通用代码,取决于 jvm)jvmApi
(所有 jvm api 项目的通用代码,取决于 jvm)
我们如何正确配置我们build.gradle
的 s 文件以仅依赖于正确的预设/目标?
例如,如果我们想在我们的其他项目中使用 common 模块作为依赖项,我们需要使用类似的东西:
dependencies {
implementation project(':common')
}
但是,是否可以只使用公共模块的正确部分?像这样的东西(对于android 1和2)?
dependencies {
implementation project(':common:jvmAndroid')
}
否则,当我们使用implementation project(':common')
它时,将获取所有 jvm 预设/目标,但某些代码仅在正确的平台上有意义或工作(在本例中为 android 或 api)。
解决方案
我们可以使用一种称为消除歧义的策略来实现这一目标。
https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#disambiguating-targets
我们需要做类似的事情:
val commonAttribute = Attribute.of("com.example", String::class.java)
jvm {
attributes.attribute(commonAttribute, "nameOfYourTarget")
}
在“客户端”和“服务器”端。以同样的方式。
推荐阅读
- python - 如何从不同文件夹中的文件中检索数据
- c++ - 如何在 gFor 中解决不完整的 ArrayFire
- c# - 使用偏移量复制流
- gitlab - Gitlab多项目管道,如何在最新标签上触发下游管道?
- c - 在 C 中将秒转换为 hh:mm:ss
- vbscript - 如何将汉字分配给VBSCRIPT中的字符串变量
- javascript - Javascript:不要对不可见的元素求和
- excel - Excel平板电脑中的宏
- python - 是否可以使用 streamlink 和 ffmpeg 从 Python 的 twitch 中跳过广告
- asp.net - 用于 ASP.Net 身份帐户确认的 ASP.Net URL 页面帮助程序不会创建正确的 URL