簡體   English   中英

使用 JFrog Artifactory 的 Kotlin 多平台庫

[英]Use Kotlin Multiplatform Library from JFrog Artifactory

我將一個共享模塊從 Kotlin Multiplatform 上傳到 JFrogs Artifactory。 目標是在另一個 android app 項目中使用這個共享模塊,並跨項目共享業務邏輯。

我嘗試僅上傳共享模塊的 android 變體 - 對於當前目的,不需要將 iOS 變體上傳到工件。

我在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 出版物的所有步驟。

使用./gradlew artifactoryPublish

Artifactory-瀏覽器

我嘗試在 android 應用程序中使用它,為此,我在 android 應用程序 build.gradle 文件中添加了與工件的連接,並在使用“設置 MeAZ 文件時,如使用工件時建議的按鈕”。 最后,我還鏈接了 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 工件的 Kotlin 多平台庫?

這可能是因為通過運行./gradlew artifactoryPublish上傳的 jars ? 神器里應該有上傳的aar文件?

也許這里的這條線不再起作用了? 我閱讀並實施的那篇文章寫於 2020 年 5 月,從那時起有一些主要的 Kotlin 多平台版本:

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

我想我需要在其中放入一些其他屬性,而不是androidDebugandroidRelease

我真的很感謝這方面的每一個幫助——我正在尋找幾個小時的解決方案。

對於 android 應用程序 aar 庫是必需的 - 所以我需要將空氣文件上傳到工件。

要上傳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 對Convert to gradle-kotlin-dsl Jfrog.Artifactory config問題的回答幫助我編寫了將 aar 上傳到 artifactory 的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM