Plugin Android Gradle 3.6.0 dan yang lebih tinggi menyertakan dukungan untuk plugin Maven Publish Gradle, yang memungkinkan Anda memublikasikan artefak build ke repositori Apache Maven. Plugin Android Gradle membuat component untuk setiap artefak varian build di aplikasi atau modul library Anda yang dapat Anda gunakan untuk menyesuaikan publication ke repositori Maven.
Komponen yang dibuat plugin Android bergantung pada apakah modul menggunakan plugin aplikasi atau library, seperti yang dijelaskan pada tabel di bawah ini.
Plugin Gradle Android | Artefak publikasi | Nama komponen |
---|---|---|
com.android.library
|
AAR | components.variant
|
com.android.application
|
ZIP APK dan file pemetaan ProGuard atau R8 yang tersedia | components.variant_apk
|
com.android.application
|
Android App Bundle (AAB) | components.variant_aab
|
Contoh kode berikut membuat publikasi untuk rilis dan varian build debug library AAR. Setiap publikasi menerapkan komponen yang cocok dan menyesuaikan atribut POM yang dihasilkan, seperti koordinat 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' } } }
Untuk membuat publikasi yang memublikasikan aplikasi Anda sebagai file ZIP APK atau Android App Bundle (AAB), cukup gunakan komponen yang sesuai, seperti yang ditampilkan di bawah ini.
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' } } }
Setelah Anda membuat publikasi, plugin Maven Publish akan membuat tugas pemublikasian yang dapat Anda gunakan untuk memublikasikan artefak Anda ke repositori yang Anda tentukan.