Skip to content

Instantly share code, notes, and snippets.

@tracer8
Created October 24, 2023 04:13
Show Gist options
  • Save tracer8/6ac9ace808beaff9d1c0421c4bbbee9a to your computer and use it in GitHub Desktop.
Save tracer8/6ac9ace808beaff9d1c0421c4bbbee9a to your computer and use it in GitHub Desktop.
Unity 2020 change gradle version
public class AutoChangeGradleVersion : IPostGenerateGradleAndroidProject, IPreprocessBuildWithReport
{
public int callbackOrder
{
get
{
return 1;
}
}
public void OnPreprocessBuild(BuildReport report)
{
SetGradleDistributionUrlTo72();
}
public static void SetGradleDistributionUrlTo72()
{
// Get the path to the gradle-wrapper.properties file.
string gradleWrapperPropertiesPath = Path.Combine(Application.dataPath, "../Temp/gradleOut/gradle/wrapper/gradle-wrapper.properties");
// Check if the file exists.
if (!File.Exists(gradleWrapperPropertiesPath))
{
Debug.LogWarning("The gradle-wrapper.properties file does not exist.");
return;
}
// Read the contents of the file.
string gradleWrapperPropertiesContents = File.ReadAllText(gradleWrapperPropertiesPath);
// Replace the old distribution URL with the new one.
gradleWrapperPropertiesContents = gradleWrapperPropertiesContents.Replace("gradle-6.1.1-all.zip", "gradle-7.2-all.zip");
// Write the updated contents back to the file.
File.WriteAllText(gradleWrapperPropertiesPath, gradleWrapperPropertiesContents);
// Debug log a message to confirm that the operation was successful.
Debug.Log("The Gradle distribution URL has been successfully updated to 7.2.");
}
public void OnPostGenerateGradleAndroidProject(string path)
{
SetGradleDistributionUrlTo72();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment