Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Utils.cs 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. internal static string GradleTemplatePath =
  26. Path.Combine(AndroidPluginsDir, "baseProjectTemplate.gradle");
  27. // Android library plugins directory that contains custom gradle templates.
  28. internal const string AndroidPluginsDir = "Assets/Plugins/Android";
  29. // Extracts an Android Gradle Plugin version number from the contents of a *.gradle file.
  30. // This should work for Unity 2022.1 and below.
  31. // Ex.
  32. // classpath 'com.android.tools.build:gradle:4.0.1'
  33. private static Regex androidGradlePluginVersionExtract_legacy =
  34. new Regex(@"^\s*classpath\s+['""]com\.android\.tools\.build:gradle:([^'""]+)['""]$");
  35. // Extracts an Android Gradle Plugin version number from the contents of a *.gradle file for
  36. // Unity 2022.2+ or 2023.1+.
  37. // Ex.
  38. // id 'com.android.application' version '7.1.2' apply false
  39. private static Regex androidGradlePluginVersionExtract =
  40. new Regex(@"^\s*id\s+['""]com\.android\.application['""] version ['""]([^'""]+)['""]");
  41. /// <summary>
  42. /// Get the Android Gradle Plugin version used by the Unity project.
  43. /// </summary>
  44. public static string AndroidGradlePluginVersion
  45. {
  46. private set {}
  47. get
  48. {
  49. if (!Directory.Exists(AndroidPluginsDir) || !File.Exists(GradleTemplatePath))
  50. {
  51. return DefaultAndroidGradlePlugin();
  52. }
  53. var gradleTemplates = Directory.GetFiles(AndroidPluginsDir, "*.gradle",
  54. SearchOption.TopDirectoryOnly);
  55. foreach (var path in gradleTemplates)
  56. {
  57. foreach (var line in File.ReadAllLines(path))
  58. {
  59. var match = androidGradlePluginVersionExtract_legacy.Match(line);
  60. if (match != null && match.Success)
  61. {
  62. return match.Result("$1");
  63. }
  64. match = androidGradlePluginVersionExtract.Match(line);
  65. if (match != null && match.Success)
  66. {
  67. return match.Result("$1");
  68. }
  69. }
  70. }
  71. return DefaultAndroidGradlePlugin();
  72. }
  73. }
  74. // TODO(@vkini): read from default Unity baseProjectTemplate.gradle file
  75. private static string DefaultAndroidGradlePlugin()
  76. {
  77. #if UNITY_2022_3_OR_NEWER
  78. return "7.1.2";
  79. #else
  80. return "4.0.1";
  81. #endif
  82. }
  83. }
  84. }