Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用maven插件和kotlin dsl发布aar_Android_Kotlin_Gradle_Gradle Kotlin Dsl - Fatal编程技术网

Android 使用maven插件和kotlin dsl发布aar

Android 使用maven插件和kotlin dsl发布aar,android,kotlin,gradle,gradle-kotlin-dsl,Android,Kotlin,Gradle,Gradle Kotlin Dsl,我在尝试使用AS的插件时遇到问题 我在一个项目中尝试了这个例子,它毫无问题。但一旦我转到kotlin dsl,我就遇到了这个问题: SoftwareComponentInternal with name 'release' not found. 这是我第一次使用kotlin dsl。首先,我不知道你是否可以同时拥有kotlin dsl和groovy,但我第一次尝试了,只是将kotlin dsl添加到根目录和应用程序中:build.gradle。我有这个错误,所以我决定将库也迁移到kotlin

我在尝试使用AS的插件时遇到问题

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

SoftwareComponentInternal with name 'release' not found.
这是我第一次使用kotlin dsl。首先,我不知道你是否可以同时拥有kotlin dsl和groovy,但我第一次尝试了,只是将kotlin dsl添加到根目录和应用程序中: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"
            }
        }
    }
}
插件{
id(BuildPlugins.androidLibrary)
id(BuildPlugins.kotlinAndroid)
id(BuildPlugins.kotlinAndroidExtensions)
id(BuildPlugins.mavenPublish)
}
后评价{
出版{
出版物{
//创建名为“release”的Maven发布。
创建(“发布”){
//为发布版本变体应用组件。
来自(组件[“发布”])
//然后可以自定义出版物的属性,如下所示。
groupId=“com.mylib”
artifactId=“alpha”
version=“0.1”
}
}
}
}

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

请确保您正在使用AGP 3.6.0+,因为此配置是从3.6.0开始添加的。文档:

到目前为止,我可以在使用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)
        }
    }
}

请尝试从(组件[“java”])中执行
。@Nicolas为什么您认为这样可以解决问题?在本例中,“release”来自android默认构建变体。很抱歉,我错过了android标签。我使用的是3.6.0+。请记住,当我转到kotlin dsl时,这个问题正在发生