使用 JFrog Artifactory 的 Kotlin 多平台库

Use Kotlin Multiplatform Library from JFrog Artifactory

我从 Kotlin Multiplatform 上传了一个共享模块到 JFrogs Artifactory。目标是在另一个 android 应用程序项目中使用此共享模块并跨项目共享业务逻辑。

我尝试只上传共享模块的 android 变体 - 对于当前目的,不需要将 iOS 变体上传到 artifactory。

我在 Kotlin Multiplatform 项目的共享模块 build.gradle.kts 中写了这段代码:

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("kotlin-android-extensions")
    kotlin("plugin.serialization")

    // plugins required for uploading to artifactory
    id("maven-publish")
    id("com.jfrog.artifactory") version "4.13.0"
}

.
.
.

artifactory {
    setContextUrl("https://rbartifactory.jfrog.io/artifactory/")
    publish(delegateClosureOf<PublisherConfig> {
        repository(delegateClosureOf<DoubleDelegateWrapper> {
            setProperty("repoKey", "App-Shared-Test-KMP")
            setProperty("username", "<MY-USERNAME>")
            setProperty("password", "<MY-USER-API-KEY>")
            setProperty("maven", true)
        })
        defaults(delegateClosureOf<groovy.lang.GroovyObject> {
            invokeMethod("publications", arrayOf(
                    "androidDebug", "androidRelease", "kotlinMultiplatform", "metadata"
            ))
        })
    })
}

在settings.gradle我还加了enableFeaturePreview("GRADLE_METADATA").

为了实现 gradle 我在这里阅读了这篇文章:https://medium.com/vmware-end-user-computing/publishing-kotlin-multiplatform-artifacts-to-artifactory-maven-a283ae5912d6

因为我想在其他 Android 应用程序项目中使用此共享模块,所以我跳过了有关 iOS 部分的 Maven 发布的所有步骤。

上传到 artifactory 成功 ./gradlew artifactoryPublish:

我尝试在 android 应用程序中使用它,为此我在 android 应用程序 build.gradle 文件中添加了与 artifactory 和此 repo 的连接,就像在 artifactory 中建议的那样使用“设置我”按钮。我也 link 最后 Android 应用程序的 build.gradle 的依赖块中的框架:

dependencies {
    .
    .
    .

    implementation(group: 'com.rb.kmp_test_shared_lib', name: 'shared', version: '1.0.4', ext: 'jar')
}

在 android 应用程序中我 运行 在此处出现此错误:

Could not determine the dependencies of task ':app:lintVitalRelease'.
> Could not resolve all artifacts for configuration ':app:debugCompileClasspath'.
   > Could not resolve com.rb.kmp_test_shared_lib:shared:1.0.4.
     Required by:
         project :app
      > No matching variant of com.rb.kmp_test_shared_lib:shared:1.0.4 was found. The consumer was configured to find an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
          - Variant 'commonMainMetadataElements-published' capability com.rb.kmp_test_shared_lib:shared:1.0.4:
              - Incompatible because this component declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed an API of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'
              - Other compatible attribute:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug')
          - Variant 'iosArm64ApiElements-published' capability com.rb.kmp_test_shared_lib:shared:1.0.4:
              - Incompatible because this component declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native' and the consumer needed an API of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'
              - Other compatible attribute:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug')
          - Variant 'iosArm64MetadataElements-published' capability com.rb.kmp_test_shared_lib:shared:1.0.4:
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native' and the consumer needed an API of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'
              - Other compatible attribute:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug')
          - Variant 'iosX64ApiElements-published' capability com.rb.kmp_test_shared_lib:shared:1.0.4:
              - Incompatible because this component declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native' and the consumer needed an API of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'
              - Other compatible attribute:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug')
          - Variant 'iosX64MetadataElements-published' capability com.rb.kmp_test_shared_lib:shared:1.0.4:
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native' and the consumer needed an API of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'
              - Other compatible attribute:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug')
          - Variant 'metadataApiElements-published' capability com.rb.kmp_test_shared_lib:shared:1.0.4:
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed an API of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'
              - Other compatible attribute:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug')
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

为什么我在尝试使用该库时出现此错误?我如何才能在其他 android 应用程序中准确地使用来自 jfrogs artifactory 的 Kotlin Multiplatform 库?

可能是因为 运行ning ./gradlew artifactoryPublish 上传的 jar? artifactory里面应该有上传的aar文件吧?

也许这里的这条线不再起作用了?我为实现而阅读的文章写于 2020 年 5 月,此后发布了一些主要的 Kotlin Multiplatform 版本:

defaults(delegateClosureOf<groovy.lang.GroovyObject> {
            invokeMethod("publications", arrayOf(
                    "androidDebug", "androidRelease", "kotlinMultiplatform", "metadata"
            ))
        })

我想我需要添加一些其他属性,而不是 androidDebugandroidRelease

我真的很感谢大家对此的帮助 - 我正在寻找解决方案的时间。

对于 android 应用程序 aar 库是必需的 - 所以我需要将 air 文件上传到 artifactory。

要上传 aar 文件,我将 publishing 块添加到我在 KMP 项目中共享模块的 build.gradle.kts

publishing {
    publications {
        create<MavenPublication>("aar") {
            artifact("$buildDir/outputs/aar/${project.name}-release.aar")
        }
    }
}

我还更改了工件块并将 defaults 部分替换为:

defaults(delegateClosureOf<groovy.lang.GroovyObject> {
    invokeMethod("publications", "aar")
    setProperty("publishArtifacts", true)
})

进行此更改后,我清理项目并构建所有工件:

./gradlew clean
./gradlew :shared:build

当所有资产构建成功(并且 aar 文件位于 KMP 项目的输出文件夹中)时,我使用 ./gradlew artifactoryPublish 成功上传 aar,然后我可以简单地在另一个 android应用

Vadim 对问题 的回答帮助我编写了将 aar 上传到 artifactory 的代码。