Skip to content

Commit 6d06c69

Browse files
authored
Fix Android Resolver unable to resolve mainTemplate.gradle in Unity 2022.2+ or 2023.1+ (#547)
Later version of Unity (2022.2+ or 2023.1+) changed the way to specify Android Gradle plugin version. As a result, `AndroidGradlePluginVersion` getter would fail to find the version and Android Resolver would fail when `mainTemplate.gradle` is in use. This fix searches Android Gradle plugin version using two regex to ensure Android Resolver can find the version in any Unity version. Also fixed the reflection exception because Unity added a new version of SetApplicationIdentifier() in 2021
1 parent ad55ee6 commit 6d06c69

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

Diff for: source/AndroidResolver/src/PlayServicesResolver.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,17 @@ public static string GradleVersion {
571571
// Gradle plugin version.
572572
private static DateTime mainTemplateLastWriteTime = default(DateTime);
573573
// Extracts an Android Gradle Plugin version number from the contents of a *.gradle file.
574+
// This should work for Unity 2022.1 and below.
575+
// Ex.
576+
// classpath 'com.android.tools.build:gradle:4.0.1'
577+
private static Regex androidGradlePluginVersionExtract_legacy = new Regex(
578+
@"['""]com\.android\.tools\.build:gradle:([^'""]+)['""]$");
579+
// Extracts an Android Gradle Plugin version number from the contents of a *.gradle file for
580+
// Unity 2022.2+ or 2023.1+.
581+
// Ex.
582+
// id 'com.android.application' version '7.1.2' apply false
574583
private static Regex androidGradlePluginVersionExtract = new Regex(
575-
@"['""]com\.android\.tools\.build:gradle:([^']+)['""]$");
584+
@"['""]com\.android\.application['""] version ['""]([^'""]+)['""]");
576585

577586
/// <summary>
578587
/// Get the Android Gradle Plugin version used by Unity.
@@ -609,7 +618,12 @@ public static string AndroidGradlePluginVersion {
609618
SearchOption.TopDirectoryOnly));
610619
foreach (var path in gradleTemplates) {
611620
foreach (var line in File.ReadAllLines(path)) {
612-
var match = androidGradlePluginVersionExtract.Match(line);
621+
var match = androidGradlePluginVersionExtract_legacy.Match(line);
622+
if (match != null && match.Success) {
623+
androidGradlePluginVersion = match.Result("$1");
624+
break;
625+
}
626+
match = androidGradlePluginVersionExtract.Match(line);
613627
if (match != null && match.Success) {
614628
androidGradlePluginVersion = match.Result("$1");
615629
break;

Diff for: source/AndroidResolver/src/UnityCompat.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ private static int VersionFromAndroidSDKVersionsEnum(object enumValue, string fa
6464
//Fall back on auto if the enum value is not parsable
6565
return -1;
6666
}
67-
68-
// If the enumName is empty then enumValue was not represented in the enum,
67+
68+
// If the enumName is empty then enumValue was not represented in the enum,
6969
// most likely because Unity has not yet added the new version,
7070
// fall back on the raw enumValue
7171
if (String.IsNullOrEmpty(enumName)) return (int)enumValue;
@@ -462,7 +462,9 @@ private static string GetUnity56AndAboveApplicationIdentifier(BuildTarget buildT
462462
private static bool SetUnity56AndAboveApplicationIdentifier(BuildTarget buildTarget,
463463
string applicationIdentifier) {
464464
var setApplicationIdentifierMethod =
465-
typeof(UnityEditor.PlayerSettings).GetMethod("SetApplicationIdentifier");
465+
typeof(UnityEditor.PlayerSettings).GetMethod(
466+
"SetApplicationIdentifier",
467+
new[]{typeof(BuildTargetGroup), typeof(string)});
466468
if (setApplicationIdentifierMethod == null) return false;
467469
var buildTargetGroup = ConvertBuildTargetToBuildTargetGroup(buildTarget);
468470
if (buildTargetGroup == BuildTargetGroup.Unknown) return false;

Diff for: source/AndroidResolver/test/src/AndroidResolverIntegrationTests.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,24 @@ private static void ValidateAndroidTargetSelected(
484484
});
485485
IntegrationTester.Runner.LogSummaryAndExit();
486486
}
487+
488+
// Verify if PlayServicesResolver properties are working properly.
489+
var testCaseResult = new IntegrationTester.TestCaseResult(testCase);
490+
491+
if (String.IsNullOrEmpty(PlayServicesResolver.AndroidGradlePluginVersion)) {
492+
testCaseResult.ErrorMessages.Add(String.Format(
493+
"PlayServicesResolver.AndroidGradlePluginVersion is empty or null"));
494+
}
495+
496+
if (String.IsNullOrEmpty(PlayServicesResolver.GradleVersion)) {
497+
testCaseResult.ErrorMessages.Add(String.Format(
498+
"PlayServicesResolver.GradleVersion is empty or null"));
499+
}
500+
487501
// Also, set the internal Gradle version to a deterministic version number. This controls
488502
// how gradle template snippets are generated by GradleTemplateResolver.
489503
PlayServicesResolver.GradleVersion = "2.14";
490-
testCaseComplete(new IntegrationTester.TestCaseResult(testCase));
504+
testCaseComplete(testCaseResult);
491505
}
492506

493507
/// <summary>

0 commit comments

Comments
 (0)