使用 Maven Publish 外掛程式

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

Android Gradle 外掛程式 3.6.0 以上版本支援 Maven Publishing Gradle 外掛程式,因此您可以將建構構件發布至 Apache Maven 存放區。Android Gradle 外掛程式會為應用程式或程式庫模組中的每個建構作業變化版本構件建立元件,方便您用來自訂 Maven 存放區的出版品

Android 外掛程式建立的元件取決於模組使用的應用程式或程式庫外掛程式,如下表所述。

Android Gradle 外掛程式 出版品成果 ComponentName
com.android.library 自動套用的建議 components.variant
com.android.application APK 的 ZIP 檔案,以及可用的 ProGuard 或 R8 對應檔 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 或 Android App Bundle (AAB) 的 ZIP 檔案,只需使用適當的元件即可,如下所示。

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 發布外掛程式會建立發布工作,方便您將構件發布至該存放區,指定的名稱。