AppLovinPreProcessAndroid.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // AppLovinBuildPreProcessor.cs
  3. // AppLovin MAX Unity Plugin
  4. //
  5. // Created by Santosh Bagadi on 8/27/19.
  6. // Copyright © 2019 AppLovin. All rights reserved.
  7. //
  8. #if UNITY_ANDROID
  9. using System.Xml.Linq;
  10. using UnityEditor.Build;
  11. using UnityEditor.Build.Reporting;
  12. namespace AppLovinMax.Scripts.IntegrationManager.Editor
  13. {
  14. /// <summary>
  15. /// Adds the AppLovin Quality Service plugin to the gradle template file. See <see cref="AppLovinProcessGradleBuildFile"/> for more details.
  16. /// </summary>
  17. public class AppLovinPreProcessAndroid : AppLovinProcessGradleBuildFile, IPreprocessBuildWithReport
  18. {
  19. private const string ElementNameAndroidPackages = "androidPackages";
  20. private const string ElementNameAndroidPackage = "androidPackage";
  21. private const string AttributeNameSpec = "spec";
  22. private const string UmpDependencyPackage = "com.google.android.ump:user-messaging-platform:";
  23. private const string UmpDependencyVersion = "2.1.0";
  24. public void OnPreprocessBuild(BuildReport report)
  25. {
  26. PreprocessAppLovinQualityServicePlugin();
  27. AddGoogleCmpDependencyIfNeeded();
  28. }
  29. private static void PreprocessAppLovinQualityServicePlugin()
  30. {
  31. // We can only process gradle template file here. If it is not available, we will try again in post build on Unity IDEs newer than 2018_2 (see AppLovinPostProcessGradleProject).
  32. if (!AppLovinIntegrationManager.GradleTemplateEnabled) return;
  33. #if UNITY_2019_3_OR_NEWER
  34. // The publisher could be migrating from older Unity versions to 2019_3 or newer.
  35. // If so, we should delete the plugin from the template. The plugin will be added to the project's application module in the post processing script (AppLovinPostProcessGradleProject).
  36. RemoveAppLovinQualityServiceOrSafeDkPlugin(AppLovinIntegrationManager.GradleTemplatePath);
  37. #else
  38. AddAppLovinQualityServicePlugin(AppLovinIntegrationManager.GradleTemplatePath);
  39. #endif
  40. }
  41. private static void AddGoogleCmpDependencyIfNeeded()
  42. {
  43. if (AppLovinInternalSettings.Instance.ConsentFlowEnabled)
  44. {
  45. var umpPackage = new XElement(ElementNameAndroidPackage,
  46. new XAttribute(AttributeNameSpec, UmpDependencyPackage + UmpDependencyVersion));
  47. var success = AddOrUpdateAndroidDependency(UmpDependencyPackage, umpPackage );
  48. if (!success)
  49. {
  50. MaxSdkLogger.UserWarning("Google CMP will not function. Unable to add user-messaging-platform dependency.");
  51. }
  52. }
  53. else
  54. {
  55. RemoveAndroidDependency(UmpDependencyPackage);
  56. }
  57. }
  58. /// <summary>
  59. /// Adds or updates an Android dependency in the AppLovin Dependencies.xml file.
  60. /// </summary>
  61. /// <param name="package">The package that we are trying to update</param>
  62. /// <param name="newDependency">The new dependency to add if it doesn't exist</param>
  63. /// <returns>Returns true if the file was successfully edited</returns>
  64. private static bool AddOrUpdateAndroidDependency(string package, XElement newDependency)
  65. {
  66. var dependenciesFilePath = AppLovinDependenciesFilePath;
  67. var dependenciesDocument = GetAppLovinDependenciesFile(dependenciesFilePath, AppLovinIntegrationManager.IsPluginInPackageManager);
  68. if (dependenciesDocument == null) return false;
  69. AddOrUpdateDependency(dependenciesDocument,
  70. ElementNameAndroidPackages,
  71. ElementNameAndroidPackage,
  72. AttributeNameSpec,
  73. package,
  74. newDependency);
  75. return SaveDependenciesFile(dependenciesDocument, dependenciesFilePath);
  76. }
  77. /// <summary>
  78. /// Removed an android dependency from the AppLovin Dependencies.xml file.
  79. /// </summary>
  80. /// <param name="package">The package to remove</param>
  81. private static void RemoveAndroidDependency(string package)
  82. {
  83. var dependenciesFilePath = AppLovinDependenciesFilePath;
  84. var dependenciesDocument = GetAppLovinDependenciesFile(dependenciesFilePath);
  85. if (dependenciesDocument == null) return;
  86. var removed = RemoveDependency(dependenciesDocument,
  87. ElementNameAndroidPackages,
  88. ElementNameAndroidPackage,
  89. AttributeNameSpec,
  90. package);
  91. if (!removed) return;
  92. SaveDependenciesFile(dependenciesDocument, dependenciesFilePath);
  93. }
  94. public int callbackOrder
  95. {
  96. get { return CallbackOrder; }
  97. }
  98. }
  99. }
  100. #endif