Что такое gradle в unity
Gradle for Android
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 version | Gradle version |
---|---|
2020.1 | 5.6.4 |
2018.4 starting from 2018.4.4f1 2019.1 starting from 2019.1.7f1, 2019.2, 2019.3, and 2019.4 | 5.1.1 |
2018.3, 2018.4 up to and including 2018.4.3f1 2019.1 up to and including 2019.1.6f1 | 4.6 |
2018.2 | 4.2.1 |
2018.1 | 4.0.1 |
2017.4, starting from 2017.4.17 | 4.6 |
2017.4 up to and including 2017.4.16 | 4.0.1 |
2017.1, 2017.2 | 2.14 |
To learn more about:
Building or exporting a Gradle project
To build a Gradle project, follow these steps:
Exported Gradle project structure
Unity 2019.3 and newer versions create a Gradle project with two modules:
build.gradle templates
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:
Customizing your Gradle build
You can provide a custom Gradle build template and select minification options in the Publishing Settings section of the Player Settings window.
Providing a custom Gradle build template
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.
mainTemplate.gradle file in the Project view for the unityLibrary module
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:
Unity replaces this line with include directives to all of the Project’s libraries.
IPostGenerateGradleAndroidProject
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.
Template variables
The mainTemplate.gradle file can contain the following variables:
Variable: | Description: |
---|---|
DEPS | List of project dependencies See in Glossary ; 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. |
Minification
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.
Errors when building with Gradle
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.
Gradle for Android
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 version | Gradle version |
---|---|
2020.3 starting from 2020.3.15f1 2021.1 starting from 2021.1.16f1 | 6.1.1 |
2020.1, 2020.2, 2020.3 up to and including 2020.3.14f1 2021.1 up to and including 2021.1.15f1 | 5.6.4 |
2019.4 | 5.1.1 |
To learn more about:
Building or exporting a Gradle project
To build a Gradle project, follow these steps:
Exported Gradle project structure
Unity 2019.3 and newer versions create a Gradle project with two modules:
build.gradle templates
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:
Customizing your Gradle build
You can provide a custom Gradle build template and select minification options in the Publishing Settings section of the Player Settings Settings that let you set various player-specific options for the final game built by Unity. More info
See in Glossary window.
Providing a custom Gradle build template
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.
mainTemplate.gradle file in the Project view for the unityLibrary module
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:
IPostGenerateGradleAndroidProject
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.
Template variables
The mainTemplate.gradle file can contain the following variables:
Variable: | Description: |
---|---|
DEPS | List of project dependencies In the context of the Package Manager, a dependency is a specific package version (expressed in the form package_name@package_version ) that a project or another package requires in order to work. Projects and packages use the dependencies attribute in their manifests to define the set of packages they require. For projects, these are considered direct dependencies; for packages, these are indirect, or transitive, dependencies. More info See in Glossary ; 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. |
Minification
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.
Errors when building with Gradle
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.
Gradle for Android
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 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:
Building or exporting a Gradle project
To build a Gradle project, follow these steps:
Exported Gradle project structure
Unity 2019.3 and newer versions create a Gradle project with two modules:
build.gradle templates
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:
Customizing your Gradle build
You can provide a custom Gradle build template and select minification options in the Publishing Settings section of the Player Settings Settings that let you set various player-specific options for the final game built by Unity. More info
See in Glossary window.
Providing a custom Gradle build template
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.
mainTemplate.gradle file in the Project view for the unityLibrary module
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:
IPostGenerateGradleAndroidProject
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.
Template variables
The mainTemplate.gradle file can contain the following variables:
Variable: | Description: |
---|---|
DEPS | List of project dependencies See in Glossary ; 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. |
Minification
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.
Errors when building with Gradle
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.
Gradle for Android
Gradle is an Android build system which automates a number of build processes. This automation means that many common build errors are less likely to occur. Most notably in Unity, it reduces the method reference count in DEX (Dalvik Executable format) files, meaning that you are less likely to come across DEX limit problems. However, due to the differences between Gradle and the default Unity Android build system, some existing projects may be hard to convert to Gradle.
You can either build the output package (APK) using the Gradle build system in Unity, or export the Gradle project and build it in an external tool (such as Android Studio).
Building with Gradle for Android
To build your Android build with Gradle in Unity:
Exporting the Gradle project
See Android Studio’s documentation on configuring your build for more information about building an output package (APK).
Providing a custom build.gradle template
which will be replaced by include directives to all your libraries.
Template Variables
These variables can be used in the mainTemplate.gradle file:
Variable: | Description: |
---|---|
DEPS | List of project dependencies; ie the libraries used. |
API VERSION | API version to build for (eg 25). |
BUILDTOOLS | SDK Build tools used (eg 25.0.1). |
TARGETSDKVERSION | API version to taget (eg 25). |
APPLICATIONID | Android Application ID (eg com.mycompany.mygame). |
MINIFY_DEBUG | Minify for debug builds enabled (true or false). |
PROGUARD_DEBUG | Use proguard for minification (true or false.) |
MINIFY_RELEASE | Minify for release builds enabled (true or false). |
PROGUARD_RELEASE | Use proguard for minification (true or false). |
USER_PROGUARD | Custom user proguard file (ie progard-user.txt). |
SIGN | Complete signingConfigs section if build is to be signed. |
SIGN_CONFIG | Set to ‘signingConfig signingConfig.release’ if build is signed. |
DIR_GRADLEPROJECT | Directory where the gradle project is created. |
DIR_UNITYPROJECT | Directory of your Unity project. |
MINSDKVERSION | Minimum API version (for example, 25). |
Minification
You can activate Proguard Minification under the Minify setting on the Player window. Note that proguard can easily strip out code that is actually needed, so often this process needs to be configured carefully. You can generate a custom proguard.txt file by enabling the User Proguard File setting under the same settings as a starting point.
See the ProGuard manual for more information on Proguard.
Errors when building with Gradle
If an error occurs during building for Android using Gradle, Unity displays an error dialog box. Click Troubleshoot to open the Gradle troubleshooting Unity documentation in your system’s browser.
Unity’s Grade build error dialog box
2017–10–02 Page published with limited editorial review
Expanded custom build.gradle template section.
Did you find this page useful? Please give it a rating: