Utils.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (C) 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Text.RegularExpressions;
  18. namespace GoogleMobileAds.Editor
  19. {
  20. /*
  21. * Utils class that contains helper methods.
  22. */
  23. public static class Utils
  24. {
  25. // Android library plugins directory that contains custom gradle templates.
  26. internal const string AndroidPluginsDir = "Assets/Plugins/Android";
  27. // Extracts an Android Gradle Plugin version number from the contents of a *.gradle file.
  28. // This should work for Unity 2022.1 and below.
  29. // Ex.
  30. // classpath 'com.android.tools.build:gradle:4.0.1'
  31. private static Regex androidGradlePluginVersionExtract_legacy =
  32. new Regex(@"^\s*classpath\s+['""]com\.android\.tools\.build:gradle:([^'""]+)['""]$");
  33. // Extracts an Android Gradle Plugin version number from the contents of a *.gradle file for
  34. // Unity 2022.2+ or 2023.1+.
  35. // Ex.
  36. // id 'com.android.application' version '7.1.2' apply false
  37. private static Regex androidGradlePluginVersionExtract =
  38. new Regex(@"^\s*id\s+['""]com\.android\.application['""] version ['""]([^'""]+)['""]");
  39. // Extracts major.minor[.patch] version numbers from a string.
  40. private static readonly Regex versionParseRegex =
  41. new Regex(@"^(\d+)\.(\d+)(?:\.(\d+))?", RegexOptions.Compiled);
  42. /// <summary>
  43. /// Get the Android Gradle Plugin version used by the Unity project.
  44. /// </summary>
  45. public static Version AndroidGradlePluginVersion
  46. {
  47. get
  48. {
  49. return ParseVersion(GetAndroidGradlePluginVersionString());
  50. }
  51. }
  52. private static string GetAndroidGradlePluginVersionString()
  53. {
  54. if (!Directory.Exists(AndroidPluginsDir))
  55. {
  56. return DefaultAndroidGradlePlugin();
  57. }
  58. var gradleTemplates = Directory.GetFiles(AndroidPluginsDir, "*.gradle",
  59. SearchOption.TopDirectoryOnly);
  60. foreach (var path in gradleTemplates)
  61. {
  62. foreach (var line in File.ReadLines(path))
  63. {
  64. var match = androidGradlePluginVersionExtract_legacy.Match(line);
  65. if (match != null && match.Success)
  66. {
  67. return match.Result("$1");
  68. }
  69. match = androidGradlePluginVersionExtract.Match(line);
  70. if (match != null && match.Success)
  71. {
  72. return match.Result("$1");
  73. }
  74. }
  75. }
  76. return DefaultAndroidGradlePlugin();
  77. }
  78. private static Version ParseVersion(string versionStr)
  79. {
  80. var match = versionParseRegex.Match(versionStr);
  81. if (match.Success)
  82. {
  83. int major = int.Parse(match.Groups[1].Value);
  84. int minor = int.Parse(match.Groups[2].Value);
  85. int patch = match.Groups[3].Success ? int.Parse(match.Groups[3].Value) : 0;
  86. return new Version(major, minor, patch);
  87. }
  88. return new Version(0, 0, 0);
  89. }
  90. // These values are based on the Unity documentation for the latest version of Unity. See
  91. // https://docs.unity3d.com/2023.2/Documentation/Manual/android-gradle-overview.html
  92. // https://docs.unity3d.com/6000.0/Documentation/Manual/android-gradle-version-compatibility.html
  93. private static string DefaultAndroidGradlePlugin()
  94. {
  95. #if UNITY_6000_0_OR_NEWER
  96. return "8.3.0";
  97. #elif UNITY_2023_2_OR_NEWER
  98. return "7.3.1";
  99. #elif UNITY_2022_2_OR_NEWER
  100. return "7.1.2";
  101. #else
  102. return "4.0.1";
  103. #endif
  104. }
  105. }
  106. }