Skip to content

Fix Android Resolver unable to resolve mainTemplate.gradle in Unity 2022.2+ or 2023.1+ #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions source/AndroidResolver/src/PlayServicesResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,17 @@ public static string GradleVersion {
// Gradle plugin version.
private static DateTime mainTemplateLastWriteTime = default(DateTime);
// Extracts an Android Gradle Plugin version number from the contents of a *.gradle file.
// This should work for Unity 2022.1 and below.
// Ex.
// classpath 'com.android.tools.build:gradle:4.0.1'
private static Regex androidGradlePluginVersionExtract_legacy = new Regex(
@"['""]com\.android\.tools\.build:gradle:([^'""]+)['""]$");
// Extracts an Android Gradle Plugin version number from the contents of a *.gradle file for
// Unity 2022.2+ or 2023.1+.
// Ex.
// id 'com.android.application' version '7.1.2' apply false
private static Regex androidGradlePluginVersionExtract = new Regex(
@"['""]com\.android\.tools\.build:gradle:([^']+)['""]$");
@"['""]com\.android\.application['""] version ['""]([^'""]+)['""]");

/// <summary>
/// Get the Android Gradle Plugin version used by Unity.
Expand Down Expand Up @@ -609,7 +618,12 @@ public static string AndroidGradlePluginVersion {
SearchOption.TopDirectoryOnly));
foreach (var path in gradleTemplates) {
foreach (var line in File.ReadAllLines(path)) {
var match = androidGradlePluginVersionExtract.Match(line);
var match = androidGradlePluginVersionExtract_legacy.Match(line);
if (match != null && match.Success) {
androidGradlePluginVersion = match.Result("$1");
break;
}
match = androidGradlePluginVersionExtract.Match(line);
if (match != null && match.Success) {
androidGradlePluginVersion = match.Result("$1");
break;
Expand Down
8 changes: 5 additions & 3 deletions source/AndroidResolver/src/UnityCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private static int VersionFromAndroidSDKVersionsEnum(object enumValue, string fa
//Fall back on auto if the enum value is not parsable
return -1;
}
// If the enumName is empty then enumValue was not represented in the enum,

// If the enumName is empty then enumValue was not represented in the enum,
// most likely because Unity has not yet added the new version,
// fall back on the raw enumValue
if (String.IsNullOrEmpty(enumName)) return (int)enumValue;
Expand Down Expand Up @@ -462,7 +462,9 @@ private static string GetUnity56AndAboveApplicationIdentifier(BuildTarget buildT
private static bool SetUnity56AndAboveApplicationIdentifier(BuildTarget buildTarget,
string applicationIdentifier) {
var setApplicationIdentifierMethod =
typeof(UnityEditor.PlayerSettings).GetMethod("SetApplicationIdentifier");
typeof(UnityEditor.PlayerSettings).GetMethod(
"SetApplicationIdentifier",
new[]{typeof(BuildTargetGroup), typeof(string)});
if (setApplicationIdentifierMethod == null) return false;
var buildTargetGroup = ConvertBuildTargetToBuildTargetGroup(buildTarget);
if (buildTargetGroup == BuildTargetGroup.Unknown) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,24 @@ private static void ValidateAndroidTargetSelected(
});
IntegrationTester.Runner.LogSummaryAndExit();
}

// Verify if PlayServicesResolver properties are working properly.
var testCaseResult = new IntegrationTester.TestCaseResult(testCase);

if (String.IsNullOrEmpty(PlayServicesResolver.AndroidGradlePluginVersion)) {
testCaseResult.ErrorMessages.Add(String.Format(
"PlayServicesResolver.AndroidGradlePluginVersion is empty or null"));
}

if (String.IsNullOrEmpty(PlayServicesResolver.GradleVersion)) {
testCaseResult.ErrorMessages.Add(String.Format(
"PlayServicesResolver.GradleVersion is empty or null"));
}

// Also, set the internal Gradle version to a deterministic version number. This controls
// how gradle template snippets are generated by GradleTemplateResolver.
PlayServicesResolver.GradleVersion = "2.14";
testCaseComplete(new IntegrationTester.TestCaseResult(testCase));
testCaseComplete(testCaseResult);
}

/// <summary>
Expand Down