使用 Maven Publish 插件

Android Gradle 插件 3.6.0 及更高版本支持 Maven Publish Gradle 插件,可让您将构建工件发布到 Apache Maven 代码库。Android Gradle 插件会为应用或库模块中的每个构建变体工件创建一个组件,您可以使用它来自定义要发布到 Maven 代码库的发布内容

Android 插件所创建的组件取决于模块是否使用应用或库插件,如下表所述。

Android Gradle 插件 发布内容工件 组件名称
com.android.library AAR components.variant
com.android.application APK 和可用的 ProGuard 或 R8 映射文件的 ZIP components.variant_apk
com.android.application Android App Bundle (AAB) components.variant_aab

以下代码示例为 AAR 库的发布和调试构建变体创建了发布内容。每项发布内容都会应用匹配的组件,并自定义所生成 POM 的属性,如 Maven 坐标。

Groovy


// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                // You can then customize attributes of the publication as shown below.
                groupId = 'com.example.MyLibrary'
                artifactId = 'final'
                version = '1.0'
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                from components.debug

                groupId = 'com.example.MyLibrary'
                artifactId = 'final-debug'
                version = '1.0'
            }
        }
    }

Kotlin


// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                // You can then customize attributes of the publication as shown below.
                groupId = 'com.example.MyLibrary'
                artifactId = 'final'
                version = '1.0'
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                from components.debug

                groupId = 'com.example.MyLibrary'
                artifactId = 'final-debug'
                version = '1.0'
            }
        }
    }

如需创建发布内容来以 APK 的 ZIP 文件或 Android App Bundle (AAB) 的形式发布您的应用,只需使用适当的组件即可,如下所示。

Groovy


afterEvaluate {
    publishing {
        publications {
            paidRelease(MavenPublication) {
              // The following applies a component to this publication
              // which results in publishing an app bundle.
              from components.paidRelease_aab

              groupId = 'com.example.MyApp'
              artifactId = 'paid-release-aab'
              version = '1.0'
            }
            paidDebug(MavenPublication) {
              // The following applies a component to this publication
              // which results in publishing APKs in a ZIP file.
              from components.paidDebug_apk

              groupId = 'com.example.MyApp'
              artifactId = 'paid-debug-apks'
              version = '1.0'
            }
        }
    }

Kotlin


afterEvaluate {
    publishing {
        publications {
            paidRelease(MavenPublication) {
              // The following applies a component to this publication
              // which results in publishing an app bundle.
              from components.paidRelease_aab

              groupId = 'com.example.MyApp'
              artifactId = 'paid-release-aab'
              version = '1.0'
            }
            paidDebug(MavenPublication) {
              // The following applies a component to this publication
              // which results in publishing APKs in a ZIP file.
              from components.paidDebug_apk

              groupId = 'com.example.MyApp'
              artifactId = 'paid-debug-apks'
              version = '1.0'
            }
        }
    }

在您创建发布内容后,Maven Publish 插件会创建发布任务,您可以通过这些任务将工件发布到您指定的代码库