使用 maven 插件和 kotlin dsl 发布 aar

publishing aar using maven plugin and kotlin dsl

我在尝试使用来自 AS 的 maven-publish 插件时遇到问题。

我在一个项目中尝试了这个例子,它没有问题。但是一旦我转到 kotlin dsl,我就会遇到这个问题:

SoftwareComponentInternal with name 'release' not found.

这是我第一次处理 kotlin dsl。首先,我不知道你是否可以同时拥有 kotlin dsl 和 groovy,但我第一次尝试只是将 kotlin dsl 添加到根和 app:build.gradle。我有这个错误,所以我决定也将库迁移到 kotlin dsl:mylib:build.gradle。我结束了这段代码:

plugins {
    id(BuildPlugins.androidLibrary)
    id(BuildPlugins.kotlinAndroid)
    id(BuildPlugins.kotlinAndroidExtensions)
    id(BuildPlugins.mavenPublish)
}

afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            create<MavenPublication>("release") {
                // Applies the component for the release build variant.
                from(components["release"])
                // You can then customize attributes of the publication as shown below.
                groupId = "com.mylib"
                artifactId = "alpha"
                version = "0.1"
            }
        }
    }
}

对此有什么想法以及如何解决吗?

请确保您使用的是 AGP 3.6.0+,因为此配置是从 3.6.0 开始添加的。文档: https://developer.android.com/studio/build/maven-publish-plugin

到目前为止,我在使用 kotlin dsl 时可以使用这个解决方案:

请记住,在这种情况下,发布的解决方案在 groovy 中。但是你可以在 kotlin dsl 中毫无问题地做到这一点。

为什么在 groovy 中?好吧,当我在应用程序模块中包含 kotlin dsl 时,我已经开始出现问题,即使我在子模块中有 groovy gradle 文件(我也想将其发布为库)。

在这个例子中,我发布了一个调试版本和一个发布版本。

afterEvaluate {
    publishing {
        publications {
            def groupIdPublication = 'com.mypackage'
            def artifactIdPublication = "util"
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                //from components.release - this is not working

                // You can then customize attributes of the publication as shown below.
                groupId = groupIdPublication
                artifactId = artifactIdPublication
                version = '0.1'
                artifact("$buildDir/outputs/aar/${project.name}-release.aar") // this is the solution I came up with
                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    applyDependenciesToPOM(dependenciesNode, configurations.api.allDependencies)
                    applyDependenciesToPOM(dependenciesNode, configurations.implementation.allDependencies)
                }
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                //from components.debug - this is not working

                groupId = groupIdPublication
                artifactId = artifactIdPublication
                version = 'debug-0.1'
                artifact("$buildDir/outputs/aar/${project.name}-debug.aar") // this is the solution I came up with
                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                applyDependenciesToPOM(dependenciesNode, configurations.api.allDependencies)
                applyDependenciesToPOM(dependenciesNode, configurations.implementation.allDependencies)
                }
            }
        }
    }
}

static def applyDependenciesToPOM(Object dependenciesNode, DependencySet allDependencies) {
    allDependencies.each {
        if (it.group != null && (it.name != null && !it.name.equals("unspecified")) &&
                (it.version != null && !it.version.equals("unspecified"))) {
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', it.group)
            dependencyNode.appendNode('artifactId', it.name)
            dependencyNode.appendNode('version', it.version)
        }
    }
}