使用maven-publish发布带有构建变体的Android apk和aarandroid gradle插件4.0

问题描述

嗨,我正在尝试遵循https://developer.android.com/studio/build/maven-publish-plugin来将库(aar)模块和应用程序(apk)模块发布到maven存储库(maven-publish插件)。我使用的是Kotlin Gradle kts而不是grovy

示例发表的内容古怪(通过上面的链接

publishing {
    publications {
        paidRelease(MavenPublication) {
          // The following applies a component to this publication
          // which results in publishing an app bundle.
          from components.paidRelease_aab

          groupId = 'com.example.MyApp'
          artifactId = 'paid-release-aab'
          version = '1.0'
        }
        paidDebug(MavenPublication) {
          // The following applies a component to this publication
          // which results in publishing APKs in a ZIP file.
          from components.paidDebug_apk

          groupId = 'com.example.MyApp'
          artifactId = 'paid-debug-apks'
          version = '1.0'
        }
    }
}

我的Kotlin dls发布应用模块代码

publications {
    create<MavenPublication>("mastercardMavenApk") {
        groupId = ProjectInfo.groupId
        artifactId = ProjectInfo.artifactId
        version = ProjectInfo.nexusversion + if (useReleaseRepo) "" else "-SNAPSHOT"

        from(components["mastercardDebug_apk"])
    }
}

其中mastercardDebug是我的Active Build Variant之一

我总是有下面的错误

SoftwareComponentInternal with name 'mastercardDebug_apk' not found

maven-publish插件用于android项目(同时支持aar和apk模块)应该是什么正确方法

解决方法

只需在 print(components.names) 前使用 from(components["mastercardDebug_apk"]) 验证组件是否存在。也许只是打字错误