未找到匹配的Kotlin多平台移动库变体。

9
我有一个使用Kotlin多平台移动 ,并将其发布至Maven中央仓库。我也尝试在非KMM Android应用程序中使用此库。当我在Android应用程序中声明依赖关系时,我收到以下错误提示:
Execution failed for task ':app:checkDebugAarMetadata'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Could not resolve io.github.tyczj:tweedle-android:0.3.0.
     Required by:
         project :app
      > No matching variant of io.github.tyczj:tweedle-android:0.3.0 was found. The consumer was configured to find a runtime 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 io.github.tyczj:tweedle-android:0.3.0:
              - 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 a runtime 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 io.github.tyczj:tweedle-android:0.3.0:
              - 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 a runtime 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 'releaseApiElements-published' capability io.github.tyczj:tweedle-android:0.3.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
              - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug'
          - Variant 'releaseRuntimeElements-published' capability io.github.tyczj:tweedle-android:0.3.0 declares a runtime of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm':
              - Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release' and the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug'

我假设我的配置有问题,但我真的不知道那个错误在告诉我什么,这是我第一次创建 KMM 库。
下面是我的 `build.gradle.kts`:
import org.gradle.api.tasks.bundling.Jar

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("kotlin-android-extensions")
    id("kotlinx-serialization")
    id("maven-publish")
    id("signing")
}

repositories {
    gradlePluginPortal()
    google()
    mavenCentral()
}

val javadocJar by tasks.registering(Jar::class) {
    archiveClassifier.set("javadoc")
}

group = "io.github.tyczj"
version = "0.3.0"

kotlin {
    android{
        publishLibraryVariants("release")
    }
    ios {
        binaries {
            framework {
                baseName = "tweedle"
            }
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-core:1.5.1")
                implementation("io.ktor:ktor-client-json:1.5.1")
                implementation("io.ktor:ktor-client-serialization:1.5.1")
                implementation("io.ktor:ktor-client-logging:1.5.1")
                implementation("ch.qos.logback:logback-classic:1.2.3")
//                implementation("com.squareup.okio:okio:2.10.0")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2-native-mt"){
                    version {
                        strictly("1.4.2-native-mt")
                    }
                }
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("androidx.core:core-ktx:1.3.2")
                implementation("io.ktor:ktor-client-android:1.5.1")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2-native-mt")
                implementation("com.github.scribejava:scribejava-apis:8.3.0")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.12")
            }
        }
        val iosMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-ios:1.5.1")
            }
        }
//        val iosTest by getting
    }
}

android {
    compileSdkVersion(30)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(26)
        targetSdkVersion(30)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }

    packagingOptions {
        excludes.add("META-INF/*.kotlin_module")
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

afterEvaluate {
    publishing {
        repositories {
            maven {
                name = "sonatype"
                url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
                credentials {
                    username = rootProject.ext["ossrhUsername"]?.toString()
                    password = rootProject.ext["ossrhPassword"]?.toString()
                }
            }
        }

        publications.withType<MavenPublication> {

            artifact(javadocJar.get())

            pom{
                name.set("Tweedle")
                description.set("Tweedle is an Android library built around the Twitter v2 API built fully in Kotlin using Kotlin Coroutines")
                url.set("https://github.com/tyczj/Tweedle")
                licenses {
                    license {
                        name.set("MIT")
                        url.set("https://opensource.org/licenses/MIT")
                    }
                }
                developers {
                    developer {
                        id.set("tyczj")
                        name.set("Jeff Tycz")
                        email.set("tyczj359@gmail.com")
                    }
                }
                scm {
                    url.set("https://github.com/tyczj/Tweedle")
                }
            }
        }
    }
}

val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework =
        kotlin.targets.getByName<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget>(
            targetName
        ).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)

ext["signing.keyId"] = rootProject.ext["signing.keyId"]?.toString()
ext["signing.password"] = rootProject.ext["signing.password"]?.toString()
ext["signing.secretKeyRingFile"] = rootProject.ext["signing.secretKeyRingFile"]?.toString()

signing {
    sign(publishing.publications)
}

我想使用android依赖库(io.github.tyczj:tweedle-android)在非KMM的android应用程序中,需要做哪些操作?

更新

我尝试将"debug"添加到android库变体中。

android{
    publishLibraryVariants("release", "debug")
}

除了添加回退方案之外

buildTypes {
    getByName("release") {
        isMinifyEnabled = false
    }
    val staging by creating{
        setMatchingFallbacks(listOf("release", "debug"))
    }
}

但是这并没有起到任何作用。

如果我创建一个新的KMM项目并尝试添加依赖项,我会得到一个略有不同的错误信息:

Failed building KotlinMPPGradleModel
org.gradle.internal.resolve.ArtifactNotFoundException: Could not find tweedle-android-0.3.1-samplessources.jar (io.github.tyczj:tweedle-android:0.3.1).

我曾经遇到过非常类似的错误,一开始我认为这与 kapt 有关,但是我在你这里没有看到你使用它。如果我在项目中改变一些 kapt 的配置,使用 configurations["kapt"].dependencies.add(..) 而不是 kapt.annotationProcessor(..),我就可以避免这个问题,但是项目在 ide 中无法构建,我遇到了一个 jdk11 的问题(javax.annotation 包不存在),虽然在命令行上构建是成功的。我想我能够接受这种情况。希望这能给你一些启示。 - salbury
@tyxzj,我正在尝试将您的库添加到一个新项目中,但它没有被添加。当我运行cradle依赖图时,它显示+--- com.tycz:tweedle-android:0.3.2 FAILED +--- com.tycz:tweedle-android-debug:0.3.2 FAILED。 - Peterstev Uremgba
2个回答

4
当您引入不适用于正确平台的依赖项时,会出现涉及“samplessources”的错误消息。在这个youtrack问题以及另一个问题中有一些讨论,关于在这种情况下发出更好的错误消息。
在您的情况下,这很可能是由于您的通用依赖项中的implementation("ch.qos.logback:logback-classic:1.2.3")触发的。这是一个JVM库,但您有一个iOS目标,因此它在您的通用依赖项中无法工作。请将其移至Android依赖项中。

嗯,有趣。我之前只是在 Ktor 中用它来调试请求 https://ktor.io/docs/logging.html#access_logger,那么这是否意味着我无法在 iOS 中记录请求? - tyczj
你可以在iOS上记录请求,但必须使用适用于iOS的日志记录器来完成。内置了Logger.DEFAULTLogger.SIMPLE,两者都会转发到iOS上的println(),或者您可以创建自己的日志记录器,将其转发到您选择的iOS日志记录API。 - RussHWolf
你的意思是将它移动到你的Android依赖项中,对吗? - IgorGanapolsky
将其移至您的 Android 依赖项,即确保声明此依赖项的块类似于 android { ... dependencies { ...} }。我的问题是我试图在 :build-logic 模块中使用 Android 库,阅读了这个答案后我意识到这当然行不通 :| - Saik Caskey

0

我猜你是将库发布为aar格式。在这种情况下,你应该在gradle依赖中明确添加@aar。

implementation 'io.github.tyczj:tweedle-android:0.3.0@aar'

1
仍然是同样的问题。 - tyczj
1
这个对我有效。1. 注释行//实现项目(':tweedle') 2. 添加以下依赖项。分别为发布和调试debugImplementation 'io.github.tyczj:tweedle-android-debug:0.3.2@aar' releaseImplementation 'io.github.tyczj:tweedle-android:0.3.2@aar' - Maxim Tulupov
1
此外,我猜你想添加库依赖项 debugImplementation('io.github.tyczj:tweedle-android-debug:0.3.2@aar'){ transitive(true) } releaseImplementation('io.github.tyczj:tweedle-android:0.3.2@aar'){ transitive(true) } - Maxim Tulupov
1
抱歉回复晚了。请尝试执行以下操作。在文件Tweedle/app/build.gradle中,只保留一个依赖项implementation ('io.github.tyczj:tweedle-android:0.3.2@aar') {transitive(true)}。并将debug部分添加到buildType中 debug { matchingFallbacks = ['release'] } - Maxim Tulupov
这完全什么也不做。 - IgorGanapolsky
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接