Gradle templates configure how to build an Android application using GradleAn Android build system that automates several build processes. This automation means that many common build errors are less likely to occur. More info
See in Glossary. Each Gradle template represents a single Gradle project. Gradle projects can include, and depend on, other Gradle projects.
A Gradle template consists of the following files:
File | Location | Contains |
---|---|---|
baseProjectTemplate.gradle |
In the exported project, root/build.gradle folder |
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 apkThe Android Package format output by Unity. An APK is automatically deployed to your device when you select File > Build & Run. More info See in Glossary. It depends on the unityLibrary project and outputs either an .apk file or an app bundle. |
mainTemplate.gradle |
In the exported project, root/unityLibrary/build.gradle folder |
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. Refer to the Providing a custom Gradle build template section on this page for more details. |
libTemplate.gradle |
Varies | If an Android Library Project plug-inA set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info See in Glossary 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. |
settingsTemplate.gradle |
In the exported project, root/settings.gradle file |
Specifies the names of modules that the Gradle build system should include when it builds the project. You can override the Unity template with a custom template in the Unity Editor. Refer to the Providing a custom Gradle build template section on this page for more details. |
gradleTemplate.properties |
In the exported project, root/gradle.properties file |
Configures the Gradle build system and specifies properties such as the size of the Java virtual machine (JVM) heap |
To have more control over the Gradle project files that Unity produces, you can override Unity’s default Gradle template files. To do this:
You can use the following variables in custom Gradle template files:
Variable | Description |
---|---|
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 | The directory where Unity creates the Gradle project. |
DIR_UNITYPROJECT | The directory of your Unity project. |
PLAY_ASSET_PACKS | Specifies the asset packs to include in your application. |
Custom settingsTemplate.gradle
files can also contain the following variables:
Variable | Description |
---|---|
INCLUDES | The list of Android Library plug-ins to include in the Gradle project. |
Custom gradleTemplate.properties
files can also contain the following variables:
Variable | Description |
---|---|
JVM_HEAP_SIZE | The maximum size of the Java virtual machine (JVM) heap. |
STREAMING_ASSETS | The list of files in the Steaming Assets folder that Gradle shouldn’t compress. |
ADDITIONAL_PROPERTIES | Contains additional properties for the application. This includes: • The Gradle template version. • The path to the Unity project. • If the application uses the Android App Bundle publishing format, a flag that indicates to keep native libraries compressed. |
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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.