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 の ZIP、利用可能な ProGuard または R8 マッピング ファイル components.variant_apk
com.android.application Android App Bundle(AAB) components.variant_aab

次のコード例は、AAR ライブラリについて、リリースおよびデバッグ用のビルド バリアントのパブリケーションを作成します。各パブリケーションは対応するコンポーネントを適用し、Maven 座標など、生成された POM の属性をカスタマイズします。

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

アプリを ZIP 形式の APK または 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 プラグインによって公開タスクが作成されます。これを使用して、指定したリポジトリにアーティファクトを公開できます。