Symptoms
- I want to change the Gradle version to a custom version (not shipped with the Editor version being used).
- My builds on Cloud Build are failing due to differences in the Gradle versions.
Resolution
Usually, It's recommended to use the Gradle version that ships with the Editor version. When necessary, you can still install custom Gradle versions in UBA. If you wish to configure a custom Gradle version for your Android build, please follow the steps below:
- Download the required Gradle version in a pre-build bash script using the sample script below.
cd "$WORKSPACE"
cd ..
curl -LO https://services.gradle.org/distributions/gradle-6.1.1-bin.zip
unameOut="$(uname -s)"
case "${unameOut}" in
Darwin*) unzip "gradle-6.1.1-bin";;
CYGWIN*) 7z x "gradle-6.1.1-bin.zip";;
esac
ls -lh gradle-6.1.1 - Set the custom Gradle path in the Editor settings in a Pre-Export Method using the sample script below.
using System;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public class PreProcessBuild
{
public static void ChangeGradle()
{
EditorPrefs.SetBool("GradleUseEmbedded", false);
string parentWorkspace = Directory.GetParent(Environment.GetEnvironmentVariable("WORKSPACE")).FullName;
string gradlePath = parentWorkspace+"/gradle-6.1.1";
EditorPrefs.SetString("GradlePath", gradlePath);
Debug.Log("PreProcessBuild - changed path: "+EditorPrefs.GetString("GradlePath"));
}
}
Additionally, other users have discovered that if $WORKSPACE does not work on the pre-build script, you can try using $USERPROFILE and removing "cd ..".
Then, the pre-export method should be changed to include this:
EditorPrefs.SetBool("GradleUseEmbedded", false);
var workspacePath = Environment.GetEnvironmentVariable("USERPROFILE");
var gradlePath = Path.Combine(workspacePath, "gradle-6.1.1");
EditorPrefs.SetString("GradlePath", gradlePath);
Debug.Log("PreProcessBuild - changed path: " + EditorPrefs.GetString("GradlePath"));
More Information
For more information about Gradle for Android, please visit our documentation page here.