使用 maven 插件和 kotlin dsl 发布 aar

Lea*_*mpo 5 android gradle kotlin gradle-kotlin-dsl

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

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

SoftwareComponentInternal with name 'release' not found.
Run Code Online (Sandbox Code Playgroud)

这是我第一次处理 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"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Lea*_*mpo -1

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

\n

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

\n

为什么这是groovy?好吧,当我在应用程序模块中包含 kotlin dsl 时,我就开始了这个问题,尽管我在子模块中有 groovy gradle 文件(我也想将其发布为库)。

\n

在此示例中,我将发布调试版本和发布版本。

\n
afterEvaluate {\n    publishing {\n        publications {\n            def groupIdPublication = 'com.mypackage'\n            def artifactIdPublication = "util"\n            // Creates a Maven publication called "release".\n            release(MavenPublication) {\n                // Applies the component for the release build variant.\n                //from components.release - this is not working\n\n                // You can then customize attributes of the publication as shown below.\n                groupId = groupIdPublication\n                artifactId = artifactIdPublication\n                version = '0.1'\n                artifact("$buildDir/outputs/aar/${project.name}-release.aar") // this is the solution I came up with\n                pom.withXml {\n                    def dependenciesNode = asNode().appendNode('dependencies')\n                    applyDependenciesToPOM(dependenciesNode, configurations.api.allDependencies)\n                    applyDependenciesToPOM(dependenciesNode, configurations.implementation.allDependencies)\n                }\n            }\n            // Creates a Maven publication called \xe2\x80\x9cdebug\xe2\x80\x9d.\n            debug(MavenPublication) {\n                // Applies the component for the debug build variant.\n                //from components.debug - this is not working\n\n                groupId = groupIdPublication\n                artifactId = artifactIdPublication\n                version = 'debug-0.1'\n                artifact("$buildDir/outputs/aar/${project.name}-debug.aar") // this is the solution I came up with\n                pom.withXml {\n                    def dependenciesNode = asNode().appendNode('dependencies')\n                applyDependenciesToPOM(dependenciesNode, configurations.api.allDependencies)\n                applyDependenciesToPOM(dependenciesNode, configurations.implementation.allDependencies)\n                }\n            }\n        }\n    }\n}\n\nstatic def applyDependenciesToPOM(Object dependenciesNode, DependencySet allDependencies) {\n    allDependencies.each {\n        if (it.group != null && (it.name != null && !it.name.equals("unspecified")) &&\n                (it.version != null && !it.version.equals("unspecified"))) {\n            def dependencyNode = dependenciesNode.appendNode('dependency')\n            dependencyNode.appendNode('groupId', it.group)\n            dependencyNode.appendNode('artifactId', it.name)\n            dependencyNode.appendNode('version', it.version)\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n