【问题标题】:Software Components will not be created automatically for Maven publishing from Android Gradle Plugin 8.0不会为从 Android Gradle 插件 8.0 发布的 Maven 自动创建软件组件
【发布时间】:2023-01-04 20:12:18
【问题描述】:

使用 Gradle 7.2 和这些插件:

plugins {
    id 'com.android.library' // Android Gradle Plugin 7.1.2
    id 'maven-publish'
}

它仍然有效,但给了我这个弃用警告:

警告:不会为从 Android Gradle 插件 8.0 发布的 Maven 自动创建软件组件。要选择加入未来的行为,请在 gradle.properties 文件中设置 Gradle 属性 android.disableAutomaticComponentCreation=true 或使用新的发布 DSL。

release notes 也提到了它,但这些指的是过时的文档:

从 AGP 8.0 开始,默认情况下将禁用自动组件创建。目前,AGP 7.1 自动为每个构建变体创建一个组件,该组件与构建变体具有相同的名称,以及一个包含所有构建变体的 all 组件。此自动组件创建将被禁用。要转换到新行为,您应该通过将 android.disableAutomaticComponentCreation 设置为 true 来手动禁用自动组件创建。
有关详细信息,请参阅Use the Maven Publish plugin


但是在文件 gradle.properties 中为 AGP 8.0 默认行为启用预览时:

android.disableAutomaticComponentCreation=true

它找不到属性components.release

FAILURE: Build failed with an exception.

* Where:
Script 'publish.gradle' line: 53

* What went wrong:
A problem occurred configuring project ':library'.
> Could not get unknown property 'release' for SoftwareComponentInternal set of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer.

offending line 内容如下:

release(MavenPublication) {
    from components.release
}

变体仍然存在,但它不再创建组件:

androidComponents {
    onVariants(selector().all(), {
        println "$it.name"
    })
}

我怎样才能升级到这个“新的发布 DSL”并创建一个软件component来发布?

【问题讨论】:

    标签: android gradle android-gradle-plugin maven-publish android-gradle-7.0


    【解决方案1】:

    根据PublishingOptions,必须定义一个android.publishing块:

    android {
        publishing {
            singleVariant('release') {
                withSourcesJar()
                withJavadocJar()
            }
            // ...
        }
    }
    

    一次定义多个变体:

    android {
        publishing {
            multipleVariants {
                withSourcesJar()
                withJavadocJar()
                allVariants()
            }
        }
    }
    

    然后例如。 components.getByName('release') 将再次为人所知。

    【讨论】:

      【解决方案2】:

      Android Studio 团队刚刚在https://developer.android.com/studio/publish-library/configure-pub-variants 发布了一些有用的用户文档

      【讨论】:

        【解决方案3】:

        在你的gradle.propertiesandroid 目录中的文件,您可以添加以下行以禁用此警告

        android.disableAutomaticComponentCreation=true
        

        enter image description here

        【讨论】:

        • 您的答案可以通过其他支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以在in the help center找到更多关于如何写出好的答案的信息。
        【解决方案4】:

        为避免此警告,您可以在 gradle.properties 文件中添加

        android.disableAutomaticComponentCreation=true

        【讨论】:

          猜你喜欢
          • 2014-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-14
          • 2023-03-19
          • 1970-01-01
          相关资源
          最近更新 更多