Skip to content

Commit

Permalink
Add logic to automatically enable gradle templates in new projects (#670
Browse files Browse the repository at this point in the history
)

* Add logic to automatically enable gradle templates

* Update PlayServicesResolver.cs
  • Loading branch information
a-maurice authored Feb 12, 2024
1 parent ecae23d commit 47cd40e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
69 changes: 68 additions & 1 deletion source/AndroidResolver/src/PlayServicesResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace GooglePlayServices {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
Expand Down Expand Up @@ -1747,7 +1748,7 @@ private static void ScheduleResolve(bool forceResolution, bool closeWindowOnComp
new ResolutionJob(
isAutoResolveJob,
() => {
ResolveUnsafeAfterJetifierCheck(
ResolveUnsafeAfterMainTemplateCheck(
(success) => {
SignalResolveJobComplete(() => {
if (resolutionCompleteWithResult != null) {
Expand All @@ -1763,6 +1764,72 @@ private static void ScheduleResolve(bool forceResolution, bool closeWindowOnComp
if (firstJob) ExecuteNextResolveJob();
}

/// <summary>
/// Ensures that the mainTemplate.gradle and gradle.properties files are present in the project,
/// creating them via the Unity Editor's template files if needed.
/// </summary>
/// <returns>True if both files are present.</returns>
private static bool EnableGradleTemplates() {
return GradleTemplateResolver.EnsureGradleTemplateEnabled(GradleTemplateResolver.GradleTemplateFilename) &&
GradleTemplateResolver.EnsureGradleTemplateEnabled(GradleTemplateResolver.GradlePropertiesTemplateFilename);
}

/// <summary>
/// Resolve dependencies after checking if mainTemplate.gradle is enabled (or previously disabled).
/// </summary>
/// <param name="resolutionComplete">Delegate called when resolution is complete
/// with a parameter that indicates whether it succeeded or failed.</param>
/// <param name="forceResolution">Whether resolution should be executed when no dependencies
/// have changed. This is useful if a dependency specifies a wildcard in the version
/// expression.</param>
/// <param name="isAutoResolveJob">Whether this is an auto-resolution job.</param>
/// <param name="closeWindowOnCompletion">Whether to unconditionally close the resolution
/// window when complete.</param>
private static void ResolveUnsafeAfterMainTemplateCheck(Action<bool> resolutionComplete,
bool forceResolution,
bool isAutoResolveJob,
bool closeWindowOnCompletion) {
// If mainTemplate.gradle is already enabled, or if the user has rejected the switch,
// move to the next step.
if (GradleTemplateEnabled ||
SettingsDialogObj.UserRejectedGradleUpgrade) {
ResolveUnsafeAfterJetifierCheck(resolutionComplete, forceResolution, isAutoResolveJob, closeWindowOnCompletion);
return;
}

// Else, if there are no resolved files tracked by this (aka, it hasn't been run before),
// turn on mainTemplate, and log a message to the user.
// Or, if using Batch mode, we want to enable the templates as well, since that is now the
// desired default behavior. If Users want to preserve the old method, they can save their
// SettingsObject with the UserRejectedGradleUpgrade option enabled.
if (ExecutionEnvironment.InBatchMode || !PlayServicesResolver.FindLabeledAssets().Any()) {
EnableGradleTemplates();
ResolveUnsafeAfterJetifierCheck(resolutionComplete, forceResolution, isAutoResolveJob, closeWindowOnCompletion);
return;
}

// Else, prompt the user to turn it on for them.
DialogWindow.Display(
"Enable Android Gradle templates?",
"Android Resolver recommends using Gradle templates " +
"for managing Android dependencies. The old method of downloading " +
"the dependencies into Plugins/Android is no longer recommended.",
DialogWindow.Option.Selected0, "Enable", "Disable",
complete: (selectedOption) => {
switch (selectedOption) {
case DialogWindow.Option.Selected0: // Enable
EnableGradleTemplates();
break;
case DialogWindow.Option.Selected1: // Disable
SettingsDialogObj.UserRejectedGradleUpgrade = true;
break;
}

// Either way, proceed with the resolution.
ResolveUnsafeAfterJetifierCheck(resolutionComplete, forceResolution, isAutoResolveJob, closeWindowOnCompletion);
});
}

/// <summary>
/// Resolve dependencies after checking the configuration is compatible with the Jetifier
/// settings.
Expand Down
22 changes: 20 additions & 2 deletions source/AndroidResolver/src/SettingsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private class Settings {
internal bool autoResolutionDisabledWarning;
internal bool promptBeforeAutoResolution;
internal bool useProjectSettings;
internal bool userRejectedGradleUpgrade;
internal EditorMeasurement.Settings analyticsSettings;

/// <summary>
Expand All @@ -72,6 +73,7 @@ internal Settings() {
autoResolutionDisabledWarning = SettingsDialog.AutoResolutionDisabledWarning;
promptBeforeAutoResolution = SettingsDialog.PromptBeforeAutoResolution;
useProjectSettings = SettingsDialog.UseProjectSettings;
userRejectedGradleUpgrade = SettingsDialog.UserRejectedGradleUpgrade;
analyticsSettings = new EditorMeasurement.Settings(PlayServicesResolver.analytics);
}

Expand All @@ -97,6 +99,7 @@ internal void Save() {
SettingsDialog.AutoResolutionDisabledWarning = autoResolutionDisabledWarning;
SettingsDialog.PromptBeforeAutoResolution = promptBeforeAutoResolution;
SettingsDialog.UseProjectSettings = useProjectSettings;
SettingsDialog.UserRejectedGradleUpgrade = userRejectedGradleUpgrade;
analyticsSettings.Save();
}
}
Expand All @@ -121,6 +124,7 @@ internal void Save() {
private const string PromptBeforeAutoResolutionKey =
Namespace + "PromptBeforeAutoResolution";
private const string UseGradleDaemonKey = Namespace + "UseGradleDaemon";
private const string UserRejectedGradleUpgradeKey = Namespace + "UserRejectedGradleUpgrade";

// List of preference keys, used to restore default settings.
private static string[] PreferenceKeys = new[] {
Expand All @@ -140,7 +144,8 @@ internal void Save() {
VerboseLoggingKey,
AutoResolutionDisabledWarningKey,
PromptBeforeAutoResolutionKey,
UseGradleDaemonKey
UseGradleDaemonKey,
UserRejectedGradleUpgradeKey
};

internal const string AndroidPluginsDir = "Assets/Plugins/Android";
Expand Down Expand Up @@ -293,6 +298,11 @@ internal static bool VerboseLogging {
get { return projectSettings.GetBool(VerboseLoggingKey, false); }
}

internal static bool UserRejectedGradleUpgrade {
set { projectSettings.SetBool(UserRejectedGradleUpgradeKey, value); }
get { return projectSettings.GetBool(UserRejectedGradleUpgradeKey, false); }
}

internal static string ValidatePackageDir(string directory) {
// Make sure the package directory starts with the same name.
// This is case insensitive to handle cases where developers rename Unity
Expand Down Expand Up @@ -507,6 +517,12 @@ public void OnGUI() {
"builds when mixing legacy Android support libraries and Jetpack libraries.");
}

GUILayout.BeginHorizontal();
GUILayout.Label("Disable MainTemplate Gradle prompt", EditorStyles.boldLabel);
settings.userRejectedGradleUpgrade =
EditorGUILayout.Toggle(settings.userRejectedGradleUpgrade);
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("Patch mainTemplate.gradle", EditorStyles.boldLabel);
settings.patchMainTemplateGradle =
Expand Down Expand Up @@ -697,7 +713,9 @@ public void OnGUI() {
new KeyValuePair<string, string>(
"patchSettingsTemplateGradle",
SettingsDialog.PatchSettingsTemplateGradle.ToString()),

new KeyValuePair<string, string>(
"userRejectedGradleUpgrade",
SettingsDialog.UserRejectedGradleUpgrade.ToString()),
},
"Settings Save");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ private static void ClearAllDependencies() {
GooglePlayServices.SettingsDialog.PatchPropertiesTemplateGradle = false;
GooglePlayServices.SettingsDialog.PatchSettingsTemplateGradle = false;

GooglePlayServices.SettingsDialog.UserRejectedGradleUpgrade = true;

PlayServicesSupport.ResetDependencies();
UpdateAdditionalDependenciesFile(false, ADDITIONAL_DEPENDENCIES_FILENAME);
UpdateAdditionalDependenciesFile(false, ADDITIONAL_DUPLICATE_DEPENDENCIES_FILENAME);
Expand Down

0 comments on commit 47cd40e

Please sign in to comment.