GradleProcessor.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.IO;
  3. using UnityEditor.Android;
  4. namespace GoogleMobileAds.Editor {
  5. public class GradleProcessor : IPostGenerateGradleAndroidProject {
  6. public int callbackOrder {
  7. get { return 0; }
  8. }
  9. private const string ApplyFrom = "apply from: ";
  10. private const string GmaPluginPath = "GoogleMobileAdsPlugin.androidlib/";
  11. private const string LauncherPath = "../unityLibrary/" + GmaPluginPath;
  12. private const string PackagingOptionsGradle = "packaging_options.gradle";
  13. private const string NextGenExclusionsGradle = "next_gen_exclusions.gradle";
  14. // Excludes the Next Gen GMA SDK (ads-mobile-sdk) so that it is not bundled alongside the
  15. // Legacy GMA SDK (play-services-ads). Applied when the Standard (Legacy) SDK is selected.
  16. private const string LegacyGmaExclusionsGradle = "legacy_gma_exclusions.gradle";
  17. private const string NextGenResolutionStrategyGradle = "next_gen_resolution_strategy.gradle";
  18. private const string GmaValidateDependenciesGradle = "validate_dependencies.gradle";
  19. private const string GmaPackagingOptionsLauncher =
  20. ApplyFrom + "'" + LauncherPath + PackagingOptionsGradle + "'";
  21. private const string GmaPackagingOptions =
  22. ApplyFrom + "'" + GmaPluginPath + PackagingOptionsGradle + "'";
  23. private const string GmaNextGenExclusions =
  24. ApplyFrom + "'" + GmaPluginPath + NextGenExclusionsGradle + "'";
  25. private const string GmaLegacyGmaExclusions =
  26. ApplyFrom + "'" + GmaPluginPath + LegacyGmaExclusionsGradle + "'";
  27. private const string GmaNextGenResolutionStrategy =
  28. ApplyFrom + "'" + GmaPluginPath + NextGenResolutionStrategyGradle + "'";
  29. private const string GmaNextGenResolutionStrategyLauncher =
  30. ApplyFrom + "'" + LauncherPath + NextGenResolutionStrategyGradle + "'";
  31. // The newer versions of webkit (1.15.0+) require AGP 8.1.0+. Thus we are downgrading the
  32. // webkit and error_prone_annotations dependencies in Apps using older AGP versions.
  33. private static readonly Version gmaAgpDowngradeVersion = new Version("8.1.0");
  34. public void OnPostGenerateGradleAndroidProject(string path) {
  35. var settings = GoogleMobileAdsSettings.LoadInstance();
  36. ProcessGradleProject(path, settings.EnableKotlinXCoroutinesPackagingOption,
  37. settings.EffectiveGmaAndroidSdk,
  38. GoogleMobileAds.Editor.Utils.AndroidGradlePluginVersion);
  39. }
  40. // Updates the unityLibrary and launcher build.gradle files in the exported Gradle project
  41. // at `path` to apply the GMA plugin's Gradle scripts that match the given settings.
  42. //
  43. // The settings are passed in as parameters, rather than read from GoogleMobileAdsSettings,
  44. // so that this method can be unit tested without the Unity Editor (loading the settings
  45. // asset requires native Editor APIs such as AssetDatabase).
  46. internal void ProcessGradleProject(string path, bool enableKotlinXCoroutinesPackagingOption,
  47. GoogleMobileAdsSettings.GmaAndroidSdk gmaAndroidSdk,
  48. Version agpVersion) {
  49. var rootDirinfo = new DirectoryInfo(path);
  50. var rootPath = rootDirinfo.Parent.FullName;
  51. var gradleList = Directory.GetFiles(rootPath, "build.gradle", SearchOption.AllDirectories);
  52. var packagingOptionsLauncher = GmaPackagingOptionsLauncher;
  53. var packagingOptionsUnityLibrary = GmaPackagingOptions;
  54. var nextGenExclusionsUnityLibrary = GmaNextGenExclusions;
  55. var legacyGmaExclusionsUnityLibrary = GmaLegacyGmaExclusions;
  56. var nextGenResolutionStrategyLauncher = GmaNextGenResolutionStrategyLauncher;
  57. var nextGenResolutionStrategyUnityLibrary = GmaNextGenResolutionStrategy;
  58. // Windows path requires '\\'
  59. #if UNITY_EDITOR_WIN
  60. packagingOptionsLauncher = packagingOptionsLauncher.Replace("/", "\\\\");
  61. packagingOptionsUnityLibrary = packagingOptionsUnityLibrary.Replace("/", "\\\\");
  62. nextGenExclusionsUnityLibrary = nextGenExclusionsUnityLibrary.Replace("/", "\\\\");
  63. legacyGmaExclusionsUnityLibrary = legacyGmaExclusionsUnityLibrary.Replace("/", "\\\\");
  64. nextGenResolutionStrategyLauncher = nextGenResolutionStrategyLauncher.Replace("/", "\\\\");
  65. nextGenResolutionStrategyUnityLibrary =
  66. nextGenResolutionStrategyUnityLibrary.Replace("/", "\\\\");
  67. #endif
  68. foreach (var gradlepath in gradleList) {
  69. if (!gradlepath.Contains("unityLibrary/build.gradle") &&
  70. !gradlepath.Contains("launcher/build.gradle") &&
  71. !gradlepath.Contains("unityLibrary\\build.gradle") &&
  72. !gradlepath.Contains("launcher\\build.gradle")) {
  73. continue;
  74. }
  75. var isUnityLibrary = gradlepath.Contains("unityLibrary/build.gradle") ||
  76. gradlepath.Contains("unityLibrary\\build.gradle");
  77. var contents = File.ReadAllText(gradlepath);
  78. bool modified = false;
  79. // 1. Clear any previously injected gradle script references (to start with a clean
  80. // slate).
  81. if (contents.Contains(PackagingOptionsGradle)) {
  82. contents = DeleteLineContainingSubstring(contents, PackagingOptionsGradle);
  83. modified = true;
  84. }
  85. if (contents.Contains(NextGenExclusionsGradle)) {
  86. contents = DeleteLineContainingSubstring(contents, NextGenExclusionsGradle);
  87. modified = true;
  88. }
  89. if (contents.Contains(LegacyGmaExclusionsGradle)) {
  90. contents = DeleteLineContainingSubstring(contents, LegacyGmaExclusionsGradle);
  91. modified = true;
  92. }
  93. if (contents.Contains(NextGenResolutionStrategyGradle)) {
  94. contents = DeleteLineContainingSubstring(contents, NextGenResolutionStrategyGradle);
  95. modified = true;
  96. }
  97. // 2. Remove validate_dependencies.gradle script reference if present. This gradle file
  98. // is no longer needed since the offending AndroidManifest.xml tags have been removed.
  99. // This check will also be removed in the next major plugin release (v12.0.0).
  100. if (contents.Contains(GmaValidateDependenciesGradle)) {
  101. contents = DeleteLineContainingSubstring(contents, GmaValidateDependenciesGradle);
  102. modified = true;
  103. }
  104. // 3. Apply packaging options if enabled.
  105. if (enableKotlinXCoroutinesPackagingOption) {
  106. if (isUnityLibrary) {
  107. contents += Environment.NewLine + packagingOptionsUnityLibrary;
  108. } else if (gradlepath.Contains("launcher/build.gradle") ||
  109. gradlepath.Contains("launcher\\build.gradle")) {
  110. contents += Environment.NewLine + packagingOptionsLauncher;
  111. }
  112. modified = true;
  113. }
  114. // 4. Apply Next-Gen resolution strategy if target is NextGen SDK and AGP version is
  115. // less than 8.1.0.
  116. if (gmaAndroidSdk == GoogleMobileAdsSettings.GmaAndroidSdk.NextGen) {
  117. if (isUnityLibrary) {
  118. contents += Environment.NewLine + nextGenExclusionsUnityLibrary;
  119. modified = true;
  120. }
  121. if (agpVersion < gmaAgpDowngradeVersion) {
  122. if (isUnityLibrary) {
  123. contents += Environment.NewLine + nextGenResolutionStrategyUnityLibrary;
  124. modified = true;
  125. } else if (gradlepath.Contains("launcher/build.gradle") ||
  126. gradlepath.Contains("launcher\\build.gradle")) {
  127. contents += Environment.NewLine + nextGenResolutionStrategyLauncher;
  128. modified = true;
  129. }
  130. }
  131. } else {
  132. // 5. Apply Legacy GMA exclusions to unityLibrary if target is the Standard (Legacy)
  133. // SDK, so that the Next Gen GMA SDK is not bundled alongside the Legacy GMA SDK. This
  134. // mirrors next_gen_exclusions.gradle, which excludes the Legacy GMA SDK for NextGen
  135. // targets.
  136. if (isUnityLibrary) {
  137. contents += Environment.NewLine + legacyGmaExclusionsUnityLibrary;
  138. modified = true;
  139. }
  140. }
  141. if (modified) {
  142. File.WriteAllText(gradlepath, contents);
  143. }
  144. }
  145. }
  146. private string DeleteLineContainingSubstring(string file, string substring) {
  147. string escapedSubstring = System.Text.RegularExpressions.Regex.Escape(substring);
  148. string pattern = @"^.*" + escapedSubstring + @".*(?:\r\n|\r|\n|$)";
  149. return System.Text.RegularExpressions.Regex.Replace(
  150. file, pattern, "", System.Text.RegularExpressions.RegexOptions.Multiline);
  151. }
  152. }
  153. }