Dùng trình bổ trợ Maven Publish

Trình bổ trợ Android cho Gradle 3.6.0 trở lên bao gồm hỗ trợ cho Trình bổ trợ Maven Publish cho Gradle, cho phép bạn phát hành các cấu phần mềm bản dựng lên kho lưu trữ Apache Maven. Trình bổ trợ Android cho Gradle sẽ tạo một thành phần cho mỗi cấu phần mềm biến thể xây dựng trong ứng dụng hoặc mô-đun thư viện mà bạn có thể sử dụng để tùy chỉnh ấn bản vào kho lưu trữ Maven.

Các thành phần mà trình bổ trợ Android tạo ra phụ thuộc vào việc mô-đun sử dụng trình bổ trợ của ứng dụng hay thư viện, như mô tả trong bảng bên dưới.

Trình bổ trợ Android cho Gradle Cấu phần mềm của ấn bản Tên thành phần
com.android.library AAR components.variant
com.android.application Tệp ZIP của (các) tệp APK và các tệp ánh xạ ProGuard hoặc R8 hiện có components.variant_apk
com.android.application Android App Bundle (AAB) components.variant_aab

Mã mẫu sau đây sẽ tạo một ấn bản cho bản phát hành và gỡ lỗi các biến thể xây dựng của một thư viện AAR. Mỗi ấn bản áp dụng thành phần trùng khớp và tùy chỉnh các thuộc tính của POM được tạo, chẳng hạn như tọa độ của 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'
            }
        }
    }

Để tạo một ấn bản phát hành ứng dụng của bạn dưới dạng tệp ZIP của các tệp APK hoặc Android App Bundle (AAB), bạn chỉ cần sử dụng thành phần thích hợp, như hiển thị dưới đây.

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'
            }
        }
    }

Sau khi bạn tạo ấn bản, trình bổ trợ Maven Publish sẽ tạo tác vụ phát hành mà bạn có thể sử dụng để phát hành cấu phần mềm lên kho lưu trữ mà bạn chỉ định.