maven 使用JFrog Artifactory中的Kotlin多平台库

mmvthczy  于 2023-03-01  发布在  Maven
关注(0)|答案(1)|浏览(203)

我从Kotlin多平台上传了一个共享模块到JFrogs Artifactory,目标是在另一个android应用程序项目中使用这个共享模块,并在项目之间共享业务逻辑。
我尝试只上传共享模块的Android变体-出于当前目的,iOS变体不需要上传到artifactory。
我在Kotlin多平台项目共享模块的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出版物的所有步骤。
使用./gradlew artifactoryPublish成功上传至工件:

我尝试在Android应用中使用它,为此,我在Android应用build.gradle文件中添加了与artifactory和此repo的连接,就像artifactory中建议的那样,在使用“设置我”按钮时。最后,我还在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多平台库?
也许是因为运行./gradlew artifactoryPublish上传的jar文件?artifactory中应该有aar文件上传?
也许这里的这行代码已经不起作用了?我读到的实现这篇文章写于2020年5月,从那时起出现了一些主要的Kotlin多平台版本:

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

我想我需要在那里放一些其他的属性,而不是androidDebugandroidRelease
我真的很感谢这方面的每一个帮助-我正在寻找几个小时的解决方案。

ykejflvf

ykejflvf1#

对于android应用程序来说,aar库是必要的--所以我需要将aar文件上传到artifactory。
为了上传aar文件,我在KMP项目中的共享模块build.gradle.kts中添加了一个publishing块:

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

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

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

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

./gradlew clean
./gradlew :shared:build

当所有资产构建成功(并且aar文件在KMP项目的输出文件夹中),我用./gradlew artifactoryPublish成功上传aar,然后我可以简单地在另一个android应用程序中使用它们。
Vadim对问题Convert to gradle-kotlin-dsl Jfrog.Artifactory config的回答帮助我编写了将aar上传到artifactory的代码。

相关问题