Gradle is an Android build system that automates a number of build processes and prevents many common build errors. In Unity, Gradle reduces the method reference count in DEX (Dalvik Executable format) files, which means you are less likely to come across DEX limit problems.
Unity uses Gradle for all Android builds. You can either build the output package (.apk, .aab) in Unity, or export a Gradle project from Unity, and then build it with an external tool such as Android Studio.
Unity version | Gradle version |
---|---|
2020.3 starting from 2020.3.15f1 | 6.1.1 |
2020.1, 2020.2, 2020.3 up to and including 2020.3.14f1 | 5.6.4 |
2019.4 | 5.1.1 |
To learn more about:
To build a Gradle project, follow these steps:
Select the:
To export your Unity Project as a Gradle project, enable the Export Project setting, and then select the Export button. When you do this, Unity generates a Gradle project in the folder you specify, and doesn’t build an .apk file. Import this project into Android Studio or another external tool to make further changes, or to build it from there.
Unity 2019.3 and newer versions create a Gradle project with two modules:
Gradle templates describe and configure how to build your Android app with Gradle. Each Gradle template represents a singleGradle 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 |
Contains configuration that is shared between all other templates/Gradle projects (repositories and the dependency on the Android Gradle plug-in). |
launcherTemplate.gradle_ |
In the exported project, root/launcher/build.gradle folder
|
Contains instructions on how to build the Android application (bundling, signing, APK splitting). 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 |
Contains instructions on how to build Unity as a Library. 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 |
Varies | Used if your Unity Project contains Android Library Projects as plug-insA 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. |
You can provide a custom Gradle build template and select minification options in the Publishing Settings section of the Player SettingsSettings that let you set various player-specific options for the final game built by Unity. More info
See in Glossary window.
You can use a custom build.gradle
file for the unityLibrary module when you build the APK from Unity. This file contains specific build instructions specified in template variables. For a list of template variables, see the next section.
To use your own build.gradle
file for the unityLibrary module, follow these steps:
Unity then generates a default mainTemplate.gradle
file in your Project’s Assets/Plugins/Android/
folder. The path to the new file also appears under the Custom Main Gradle Template option in the Player Settings. Double-click the mainTemplate.gradle
file in the Project view to open it in an external text editor.
By default, Unity uses the settingsTemplate.gradle
file from the Unity install directory to create the settings.gradle
file for your build. The settings.gradle
file contains project components which are involved in the build process. Unity creates the following components by default, which should always be included in the settings.gradle
file:
Additionally, if you add Android library plugins to your project, Unity automatically includes them in the settings file by replacing the **INCLUDES** entry.
If you want to add additional components to the settings.gradle
file that Unity doesn’t include by default, create a settingsTemplate.gradle
file in your project’s Assets/Plugins/Android/
folder. This overrides the default template.
If you use your own settingsTemplate.gradle
file, it must contain the following lines:
include ':launcher', ':unityLibrary'
**INCLUDES**
IPostGenerateGradleAndroidProject returns the path to the unityLibrary module. This keeps everything similar to versions earlier than Unity 2019.3 and doesn’t require any further changes, which means Unity can reach the app’s manifest and resources in a consistent way across versions.
The mainTemplate.gradle
file can contain the following variables:
Variable: | Description: |
---|---|
DEPS | List of project dependencies; that is, the libraries that the Project uses. |
APIVERSION | API version to build for (for example, 25). |
MINSDKVERSION | Minimum API version (for example, 25). |
BUILDTOOLS | SDK Build tools used (for example, 25.0.1). |
TARGETSDKVERSION | API version to target (for example, 25). |
APPLICATIONID | Android Application ID (for example, com.mycompany.myapp). |
MINIFY_DEBUG | Enable minify for debug builds (true or false). |
PROGUARD_DEBUG | Use proguard for minification in debug builds (true or false.) |
MINIFY_RELEASE | Enable minify for release builds (true or false). |
PROGUARD_RELEASE | Use proguard for minification in release builds (true or false). |
USER_PROGUARD | Custom user proguard file (for example, proguard-user.txt ). |
SIGN | Complete the signingConfigs section if this build is signed. |
SIGNCONFIG | Set to signingConfig signingConfig.release if this build is signed. |
DIR_GRADLEPROJECT | The directory where Unity creates the Gradle project. |
DIR_UNITYPROJECT | The directory of your Unity Project. |
You can use Proguard minification to shrink and optimize your app. To activate this option, follow these steps:
Note: Proguard might strip out important code that your app needs, so use these options carefully.
To generate a custom proguard.txt
file, enable the User Proguard File setting in the Publishing Settings section of the Player Settings. This immediately generates the proguard.txt
file in your Project’s Assets/Plugins/Android/
folder.
To learn more about ProGuard, see the ProGuard manual.
If an error occurs when you use Gradle to build your app for Android, Unity displays an error dialog box. Select the Troubleshoot button to open the Gradle troubleshooting Unity documentation in your system’s browser.