Skip to content

Commit 81f3202

Browse files
committed
Lazy initialize gradleResolver as long as build target is Android.
Fixed a regression that gradleResolver can be null until Initialize() is called. This may race with some build process which tries to call PlayServicesResolver.Resolve() in the next editor frame. Now it will be lazy initialized as long as current build target is Android. Change-Id: I25ad9a4d22d2eaf11568be6b9ba0dbd2263acad5
1 parent a1e6d2f commit 81f3202

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

source/AndroidResolver/src/PlayServicesResolver.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,22 @@ public void Poll(Func<T> getCurrentValue, Changed changed) {
383383

384384
/// <summary>
385385
/// Resolver that uses Gradle to download libraries and embed them within a Unity project.
386+
/// Lazy initialize it only when current build target is Android.
386387
/// </summary>
387-
private static GradleResolver gradleResolver;
388+
private static GradleResolver GradleResolverInstance {
389+
get {
390+
if (gradleResolverInstance == null &&
391+
EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) {
392+
gradleResolverInstance = new GradleResolver();
393+
}
394+
return gradleResolverInstance;
395+
}
396+
}
397+
398+
/// <summary>
399+
/// Instance of GradleResolver.
400+
/// </summary>
401+
private static GradleResolver gradleResolverInstance = null;
388402

389403
/// <summary>
390404
/// Resoluton job.
@@ -954,8 +968,6 @@ private static bool Initialize() {
954968
"please report to the developer.");
955969
}
956970

957-
// Create the resolver.
958-
gradleResolver = new GradleResolver();
959971
// Monitor Android dependency XML files to perform auto-resolution.
960972
AddAutoResolutionFilePatterns(xmlDependencies.fileRegularExpressions);
961973

@@ -1091,7 +1103,7 @@ private static void OnPostprocessAllAssets(string[] importedAssets,
10911103
string[] deletedAssets,
10921104
string[] movedAssets,
10931105
string[] movedFromAssetPaths) {
1094-
if (gradleResolver != null) {
1106+
if (GradleResolverInstance != null) {
10951107
// If the manifest changed, try patching it.
10961108
var manifestPath = FileUtils.NormalizePathSeparators(
10971109
SettingsDialogObj.AndroidManifestPath);
@@ -1150,7 +1162,7 @@ private static void OnPostProcessScene() {
11501162
if (UnityEngine.Application.isPlaying) return;
11511163
// If the Android resolver isn't enabled or automatic resolution is disabled,
11521164
// do nothing.
1153-
if (gradleResolver == null || !SettingsDialogObj.AutoResolveOnBuild) {
1165+
if (GradleResolverInstance == null || !SettingsDialogObj.AutoResolveOnBuild) {
11541166
return;
11551167
}
11561168
// If post-processing has already been executed since this module was loaded, don't
@@ -1231,7 +1243,7 @@ private static void AutoResolve(Action resolutionComplete) {
12311243
/// Auto-resolve if any packages need to be resolved.
12321244
/// </summary>
12331245
private static void Reresolve() {
1234-
if (AutomaticResolutionEnabled && gradleResolver != null) {
1246+
if (AutomaticResolutionEnabled && GradleResolverInstance != null) {
12351247
ScheduleAutoResolve();
12361248
}
12371249
}
@@ -1940,15 +1952,23 @@ private static void ResolveUnsafe(Action<bool> resolutionComplete,
19401952
});
19411953
} else {
19421954
lastError = "";
1943-
gradleResolver.DoResolution(
1944-
SettingsDialogObj.PackageDir,
1945-
closeWindowOnCompletion,
1946-
() => {
1947-
RunOnMainThread.Run(() => {
1948-
finishResolution(String.IsNullOrEmpty(lastError) &&
1949-
patchGradleProperties(), lastError);
1950-
});
1955+
if (GradleResolverInstance != null) {
1956+
GradleResolverInstance.DoResolution(
1957+
SettingsDialogObj.PackageDir,
1958+
closeWindowOnCompletion,
1959+
() => {
1960+
RunOnMainThread.Run(() => {
1961+
finishResolution(String.IsNullOrEmpty(lastError) &&
1962+
patchGradleProperties(), lastError);
1963+
});
1964+
});
1965+
} else {
1966+
// Fail the resolution if gradleResolver is not initialized.
1967+
RunOnMainThread.Run(() => {
1968+
finishResolution(false, "GradleResolver is not created. Is your " +
1969+
"current build target set to Android?");
19511970
});
1971+
}
19521972
}
19531973
}
19541974

@@ -1986,7 +2006,7 @@ public static void SettingsDialog()
19862006
/// Interactive resolution of dependencies.
19872007
/// </summary>
19882008
private static void ExecuteMenuResolve(bool forceResolution) {
1989-
if (gradleResolver == null) {
2009+
if (GradleResolverInstance == null) {
19902010
NotAvailableDialog();
19912011
return;
19922012
}
@@ -2428,7 +2448,7 @@ internal static void DeleteLabeledAssets() {
24282448
internal static void OnSettingsChanged() {
24292449
PlayServicesSupport.verboseLogging = SettingsDialogObj.VerboseLogging;
24302450
logger.Verbose = SettingsDialogObj.VerboseLogging;
2431-
if (gradleResolver != null) {
2451+
if (GradleResolverInstance != null) {
24322452
PatchAndroidManifest(GetAndroidApplicationId(), null);
24332453
Reresolve();
24342454
}

0 commit comments

Comments
 (0)