You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Gradle Plugin 8移除自动组件创建后如何适配Maven发布?

问题

我们有一个使用Android Gradle Plugin 7.X的Android项目,依赖多个构建变体将.apk和.aab先发布到Maven仓库,再按需发布到Play Store。构建变体由productFlavor名称和buildConfig选项组合而成,例如brand1-release、brand2-debug。

在AGP7中,我们通过以下代码将变体关联的组件发布到Maven:

afterEvaluate {
    publishing {
        publications {
            brand1Apk(MavenPublication) {
                groupId = 'apk.release.' + brand1
                artifactId = 'brand1'
                from components.brand1Release_apk 
            }
            // 其他变体的发布配置...
        }
    }
}

但升级到AGP8后,发布失败,因为brand1Release_apk这类自动创建的软件组件被移除了。此前在AGP7中已收到警告:

WARNING: Software Components will not be created automatically for Maven publishing from Android Gradle Plugin 8.0. To opt-in to the future behavior, set the Gradle property android.disableAutomaticComponentCreation=true in the gradle.properties file or use the new publishing DSL.

查阅相关文档后发现,singleVariant()、multipleVariants()等API多针对com.android.library插件,而我们的主模块使用的是com.android.application插件,不清楚如何在AGP8下为应用模块的构建变体配置Maven发布。

解决方案

针对com.android.application模块,AGP8提供了专门的发布配置方式,无需依赖自动创建的组件,直接通过android块下的publishing DSL绑定变体与Maven发布任务,具体操作如下:

1. 适配AGP8行为(推荐)

在项目根目录的gradle.properties中添加:

android.disableAutomaticComponentCreation=true

该配置会禁用旧版自动组件创建逻辑,完全使用新发布DSL,避免兼容问题。

2. 配置变体与Maven出版物关联

无需再用afterEvaluate包裹,直接在android块内添加publishing配置,指定要发布的变体及产物类型(APK/AAB),并关联到对应的Maven出版物:

android {
    // 原有flavor、buildType配置示例
    productFlavors {
        brand1 {}
        brand2 {}
    }
    buildTypes {
        release {}
        debug {}
    }

    publishing {
        // 发布brand1Release变体的APK到brand1Apk出版物
        singleVariant("brand1Release") {
            publishApk()
            publicationName = "brand1Apk"
        }
        // 发布brand2Debug变体的AAB到brand2Bundle出版物
        singleVariant("brand2Debug") {
            publishBundle()
            publicationName = "brand2Bundle"
        }
        // 其他变体依次配置...
    }
}

// 配置Maven出版物的groupId、artifactId等信息
publishing {
    publications {
        brand1Apk(MavenPublication) {
            groupId = 'apk.release.brand1'
            artifactId = 'brand1'
            // AGP会自动关联变体产物,无需手动指定from components.xxx
        }
        brand2Bundle(MavenPublication) {
            groupId = 'aab.debug.brand2'
            artifactId = 'brand2'
        }
        // 对应变体的出版物配置...
    }

    // 配置Maven仓库地址与认证信息
    repositories {
        maven {
            url = uri("你的Maven仓库地址")
            // 如需要认证,添加credentials配置
        }
    }
}

3. 批量处理多变体(高效方案)

若变体数量较多,可通过遍历applicationVariants批量生成发布配置,避免重复代码:

android {
    // ... 原有配置

    publishing {
        applicationVariants.all { variant ->
            def variantName = variant.name
            def apkPublicationName = "${variantName}Apk"
            // 批量配置APK发布
            singleVariant(variantName) {
                publishApk()
                publicationName = apkPublicationName
            }
            // 如需同时发布AAB,可添加以下配置
            def bundlePublicationName = "${variantName}Bundle"
            singleVariant(variantName) {
                publishBundle()
                publicationName = bundlePublicationName
            }
        }
    }
}

publishing {
    publications {
        android.applicationVariants.all { variant ->
            def variantName = variant.name
            // 配置APK出版物
            "${variantName}Apk"(MavenPublication) {
                groupId = "apk.${variant.buildType.name}.${variant.flavorName}"
                artifactId = variant.flavorName
            }
            // 配置AAB出版物
            "${variantName}Bundle"(MavenPublication) {
                groupId = "aab.${variant.buildType.name}.${variant.flavorName}"
                artifactId = variant.flavorName
            }
        }
    }

    // 仓库配置...
}

核心要点

  • 应用模块使用singleVariant()API绑定变体,通过publishApk()或publishBundle()指定发布产物类型。
  • 无需再手动引用components.xxx组件,AGP会自动完成变体产物与Maven出版物的关联。
  • 同一变体可同时发布APK和AAB,只需分别创建两个出版物并关联即可。

内容的提问来源于stack exchange,提问作者Daniel Kareski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 16:52:45