Gradle templates configure how to build an Android application using Gradle. Each Gradle template represents a single Gradle project. Gradle projects can include, and depend on, other Gradle projects.
Gradle 模板包含以下文件:
File | 所在地 | 包含 |
---|---|---|
baseProjectTemplate.gradle |
在导出的项目中,root/build.gradle 文件夹 |
Configuration information that affects all modules in the final Gradle project. It specifies which Android Gradle Plugin version to use and locations of java plugins. The locations are a combination of online repositories and java plugins inside of this project. |
launcherTemplate.gradle_ |
In the exported project, root/launcher/build.gradle folder |
Instructions on how to build the Android application. This includes bundling, signing, and whether to split the apk. It depends on the unityLibrary project and outputs either an .apk file or an app bundle. |
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 |
因情况而异 | If an Android Library Project plug-in doesn’t include a build.gradle file, Unity uses the libTemplate.gradle file as a template to generate one. After Unity generates the build.gradle file, or if one already exists in the plug-in’s directory, Unity copies the plug-in into the Gradle project. |
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.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:
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.mainTemplate.gradle
文件可以包含以下变量:
可变 | 描述 |
---|---|
DEPS | The list of project dependencies. This is the list of libraries that the project uses. |
APIVERSION | The API version to build for. Unity sets the and TARGETSDKVERSION to the same value (Target API Level in the Android Player Settings). |
MINSDKVERSION | The minimum API version that supports the application. |
BUILDTOOLS | The SDK build tools to use. |
TARGETSDKVERSION | The API version to target. Unity sets the and APIVERSION to the same value (Target API Level in the Android Player Settings). |
APPLICATIONID | Android Application ID. For example, com.mycompany.myapp. |
MINIFY_DEBUG | Indicates whether to minify debug builds. |
PROGUARD_DEBUG | Indicates whether to use ProGuard for minification in debug builds. |
MINIFY_RELEASE | Indicates whether to minify release builds. |
PROGUARD_RELEASE | Indicates whether to use ProGuard for minification in release builds. |
USER_PROGUARD | Specifies a custom ProGuard file to use for minification. |
SIGN | Complete the signingConfigs section if this build is signed. |
SIGNCONFIG | Indicates whether the build is signed. If this property is set to signingConfig.release , the build is signed. |
DIR_GRADLEPROJECT | Unity 创建 Gradle 项目的目录。 |
DIR_UNITYPROJECT | The directory of your Unity project. |
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:
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
.settings.gradle
file by replacing the **INCLUDES**
entry. This means you must add **INCLUDES**
as an entry to your settingsTemplate.gradle
.To modify the Gradle project after Unity assembles it, create a class that inherits from IPostGenerateGradleAndroidProject and override the OnPostGenerateGradleAndroidProject function. This function receives the path to the unityLibrary module as a parameter and you can use it to reach the application’s manifest and resources through C# scripting.
Warning: Unity now uses an incremental build pipeline which means Unity reuses the same Gradle project for consecutive builds. This means that any modifications you do using this API accumulate since Unity no longer creates a new Gradle project for every build. For example, if you use this API to add an additional file to the Gradle project, the first build works as expected, but during the second build the file already exists. A second example is if you use this API to add a permission to a particular file. Each successive build adds another entry for the permission. It is crucial to make sure that modifications you want to make aren’t already present in the build.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.