Version: 2022.1
言語: 日本語
Android 用のビルドと提出
Android アプリマニフェストのオーバーライド

Gradle テンプレート

Gradle テンプレートは、Gradle で Android アプリケーションをビルドする方法を設定します。各 Gradle テンプレートは 1 つの Gradle プロジェクトを表します。Gradle プロジェクトは他の Gradle プロジェクトを含むことや、他の Gradle プロジェクトに依存することができます。

Gradle テンプレートファイル

Gradle テンプレートは以下のファイルで構成されています。

ファイル 場所 含まれるもの
baseProjectTemplate.gradle エクスポートしたプロジェクトの root/build.gradle フォルダー 最終的な Gradleプロジェクトのすべてのモジュールに影響する設定情報が含まれています。どの Android Gradle Plugin のバージョンを使用するか、また java プラグインの場所を指定します。場所は、このプロジェクト内のオンラインレポジトリと java プラグインの組み合わせです。
launcherTemplate.gradle_ エクスポートしたプロジェクトの root/launcher/build.gradle folder フォルダー Android アプリケーションのビルド方法 (バンドル、署名、APK 分割) のインストラクションが含まれています。unityLibrary プロジェクトに依存し、.apk ファイルまたはアプリケーションバンドルを出力します。
mainTemplate.gradle エクスポートしたプロジェクトの root/unityLibrary/build.gradle フォルダー Instructions on how to build Unity as a Library. This outputs an .aar file. You can override the Unity template with a custom template in the Unity Editor. See the Providing a custom Gradle build template section on this page for more details.
libTemplate.gradle 任意 Android Library Project プラグインに build.gradle ファイルが含まれていない場合、Unity は libTemplate.gradle ファイルをテンプレートとして使用し、ファイルを生成します。Unity が build.gradle ファイルを生成した後、またはプラグインのディレクトリに既に存在する場合、Unity はプラグインを Gradle プロジェクトへコピーします。

Gradle のカスタムテンプレートの使用

There are two .gradle files that control the Gradle build process for the unityLibrary module:

  • build.gradle: Specifies build instructions.
  • settings.gradle: Contains the names of modules that the Gradle build system should include when it builds the project.project components that the instructions in the build.gradle file use in the build process.

build.gradle

The build.gradle file contains template variables that specify build instructions.

By default, Unity uses the mainTemplate.gradle file from the Unity install directory to create the build.gradle file for the unityLibrary module. To create your own mainTemplate.gradle file:

  1. Open the Project Settings window (menu: Edit > Project Settings).
  2. Select the Player tab then open Android Player Settings:
  3. In the Publishing Settings section, enable Custom Main Gradle Template. This creates a Gradle template file called mainTemplate.gradle and displays the path to the file. Unity now uses this mainTemplate.gradle file to create the build.gradle file. For a list of template variables and a description of what each does, see Template variables.

Template variables

mainTemplate.gradle ファイルには以下の変数を加えることができます。

変数 説明 
DEPS プロジェクト依存関係のリスト。プロジェクトが使用するライブラリのリスト。
APIVERSION ビルドする API バージョン。Unity はこれと TARGETSDKVERSION を同じ値 (Android Player SettingsTarget API Level) に設定します。
MINSDKVERSION アプリケーションをサポートする API の最低バージョン。
BUILDTOOLS 使用する SDK ビルドツール。
TARGETSDKVERSION ターゲットとする API バージョン。Unity はこれと APIVERSION を同じ値 (Android Player SettingsTarget API Level) に設定します。
APPLICATIONID Android アプリケーション ID (例えば com.mycompany.myapp)
MINIFY_DEBUG デバッグビルドを小さくするかどうかを示します。
PROGUARD_DEBUG デバッグビルドの小型化のために ProGuard を使用するかどうかを指定します。
MINIFY_RELEASE リリースビルドを小型化するかどうかを示します。
PROGUARD_RELEASE リリースビルドの小型化のために ProGuard を使用するかどうかを指定します。
USER_PROGUARD 小型化に使用するカスタム ProGuard ファイルを指定します。
SIGN このビルドが署名されている場合は、signingConfigs セクションを行ってください。
SIGNCONFIG ビルドが署名されているかどうかを示します。このプロパティが signingConfig.release に設定されている場合、ビルドは署名されています。
DIR_GRADLEPROJECT Unity が Gradle プロジェクトを作成するディレクトリ。
DIR_UNITYPROJECT Unity プロジェクトのディレクトリ。

settings.gradle

The settings.gradle file contains project components used in the build process.

By default, Unity uses the settingsTemplate.gradle file from the Unity install directory to create the settings.gradle file for your build. To create your own settingsTemplate.gradle file, create a settingsTemplate.gradle file in your project’s Assets/Plugins/Android/ folder. This overrides the default template.

If you create a custom settings.gradle file, be aware of the following:

  • Unity creates the launcher and unityLibrary components by default and you must include them in your settingsTemplate.gradle file. To do this, add include ':launcher', ':unityLibrary' as an entry to your settingsTemplate.gradle.
  • If you use Android Library plug-ins, Unity includes them in the final settings.gradle file by replacing the **INCLUDES** entry. This means you must add **INCLUDES** as an entry to your settingsTemplate.gradle.

エクスポートされた Gradle プロジェクトを C# を使用して修正する

Unity が組み立てた後に Gradle プロジェクトを変更するには、IPostGenerateGradleAndroidProject を継承するクラスを作成し、OnPostGenerateGradleAndroidProject 関数をオーバーライドしてください。この関数は、パラメーターとして unityLibrary モジュールへのパスを受け取り、それを使って C# スクリプトを通してアプリケーションのマニフェストとリソースにアクセスできます。

注意: Unity はインクリメンタルな (増分) ビルドパイプラインを使用するようになり、同じ Gradle プロジェクトを連続したビルドに再利用するようになりました。これは、Unity がビルドごとに新しい Gradle プロジェクトを作成しなくなったため、この API を使用して行った変更が蓄積されることを意味します。例えば、この API を使って Gradle プロジェクトにファイルを追加する場合、最初のビルドは期待通りに動作しますが、2 回目のビルドではそのファイルはすでに存在しています。2 つ目の例は、この API を使用して特定のファイルに許可を追加する場合です。各連続するビルドは、許可のための別のエントリーを追加します。行いたい変更が、まだビルドに存在しないことを確認することが非常に重要です。

Android 用のビルドと提出
Android アプリマニフェストのオーバーライド