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.
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-ins. |
You can provide a custom Gradle build template and select minification options in the Publishing Settings section of the Player Settings 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 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.
You can also create your own settings.gradle file. To do this, you need to manually create a settingsTemplate.gradle file in your Project’s Assets/Plugins/Android/ folder. Unity uses this file to include your library projects. Unless you want to override this process, your file needs to contain this line:
INCLUDES
Unity replaces this line with include directives to all of the Project’s libraries.
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.