ManifestProcessor.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2020 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. #if UNITY_ANDROID
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Xml.Linq;
  19. using UnityEditor;
  20. using UnityEditor.Build;
  21. #if UNITY_2018_1_OR_NEWER
  22. using UnityEditor.Build.Reporting;
  23. #endif
  24. using UnityEngine;
  25. using GoogleMobileAds.Editor;
  26. #if UNITY_2018_1_OR_NEWER
  27. public class ManifestProcessor : IPreprocessBuildWithReport
  28. #else
  29. public class ManifestProcessor : IPreprocessBuild
  30. #endif
  31. {
  32. private const string MANIFEST_RELATIVE_PATH =
  33. "Plugins/Android/GoogleMobileAdsPlugin.androidlib/AndroidManifest.xml";
  34. private const string METADATA_APPLICATION_ID =
  35. "com.google.android.gms.ads.APPLICATION_ID";
  36. private const string METADATA_DELAY_APP_MEASUREMENT_INIT =
  37. "com.google.android.gms.ads.DELAY_APP_MEASUREMENT_INIT";
  38. private const string METADATA_OPTIMIZE_INITIALIZATION =
  39. "com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION";
  40. private const string METADATA_OPTIMIZE_AD_LOADING =
  41. "com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING";
  42. private XNamespace ns = "http://schemas.android.com/apk/res/android";
  43. public int callbackOrder { get { return 0; } }
  44. #if UNITY_2018_1_OR_NEWER
  45. public void OnPreprocessBuild(BuildReport report)
  46. #else
  47. public void OnPreprocessBuild(BuildTarget target, string path)
  48. #endif
  49. {
  50. string manifestPath = Path.Combine(
  51. Application.dataPath, MANIFEST_RELATIVE_PATH);
  52. if (AssetDatabase.IsValidFolder("Packages/com.google.ads.mobile"))
  53. {
  54. manifestPath = Path.Combine("Packages/com.google.ads.mobile", MANIFEST_RELATIVE_PATH);
  55. }
  56. XDocument manifest = null;
  57. try
  58. {
  59. manifest = XDocument.Load(manifestPath);
  60. }
  61. #pragma warning disable 0168
  62. catch (IOException e)
  63. #pragma warning restore 0168
  64. {
  65. StopBuildWithMessage("AndroidManifest.xml is missing. Try re-importing the plugin.");
  66. }
  67. XElement elemManifest = manifest.Element("manifest");
  68. if (elemManifest == null)
  69. {
  70. StopBuildWithMessage("AndroidManifest.xml is not valid. Try re-importing the plugin.");
  71. }
  72. XElement elemApplication = elemManifest.Element("application");
  73. if (elemApplication == null)
  74. {
  75. StopBuildWithMessage("AndroidManifest.xml is not valid. Try re-importing the plugin.");
  76. }
  77. GoogleMobileAdsSettings instance = GoogleMobileAdsSettings.LoadInstance();
  78. string appId = instance.GoogleMobileAdsAndroidAppId;
  79. if (appId.Length == 0)
  80. {
  81. StopBuildWithMessage(
  82. "Android Google Mobile Ads app ID is empty. Please enter a valid app ID to run ads properly.");
  83. }
  84. IEnumerable<XElement> metas = elemApplication.Descendants()
  85. .Where( elem => elem.Name.LocalName.Equals("meta-data"));
  86. SetMetadataElement(elemApplication,
  87. metas,
  88. METADATA_APPLICATION_ID,
  89. appId);
  90. SetMetadataElement(elemApplication,
  91. metas,
  92. METADATA_DELAY_APP_MEASUREMENT_INIT,
  93. instance.DelayAppMeasurementInit);
  94. SetMetadataElement(elemApplication,
  95. metas,
  96. METADATA_OPTIMIZE_INITIALIZATION,
  97. instance.OptimizeInitialization);
  98. SetMetadataElement(elemApplication,
  99. metas,
  100. METADATA_OPTIMIZE_AD_LOADING,
  101. instance.OptimizeAdLoading);
  102. elemManifest.Save(manifestPath);
  103. }
  104. private XElement CreateMetaElement(string name, object value)
  105. {
  106. return new XElement("meta-data",
  107. new XAttribute(ns + "name", name), new XAttribute(ns + "value", value));
  108. }
  109. private XElement GetMetaElement(IEnumerable<XElement> metas, string metaName)
  110. {
  111. foreach (XElement elem in metas)
  112. {
  113. IEnumerable<XAttribute> attrs = elem.Attributes();
  114. foreach (XAttribute attr in attrs)
  115. {
  116. if (attr.Name.Namespace.Equals(ns)
  117. && attr.Name.LocalName.Equals("name") && attr.Value.Equals(metaName))
  118. {
  119. return elem;
  120. }
  121. }
  122. }
  123. return null;
  124. }
  125. /// <summary>
  126. /// Utility for setting a metadata element
  127. /// </summary>
  128. /// <param name="elemApplication">application element</param>
  129. /// <param name="metas">all metadata elements</param>
  130. /// <param name="metadataName">name of the element to set</param>
  131. /// <param name="metadataValue">value to set</param>
  132. private void SetMetadataElement(XElement elemApplication,
  133. IEnumerable<XElement> metas,
  134. string metadataName,
  135. string metadataValue)
  136. {
  137. XElement element = GetMetaElement(metas, metadataName);
  138. if (element == null)
  139. {
  140. elemApplication.Add(CreateMetaElement(metadataName, metadataValue));
  141. }
  142. else
  143. {
  144. element.SetAttributeValue(ns + "value", metadataValue);
  145. }
  146. }
  147. /// <summary>
  148. /// Utility for setting a metadata element
  149. /// </summary>
  150. /// <param name="elemApplication">application element</param>
  151. /// <param name="metas">all metadata elements</param>
  152. /// <param name="metadataName">name of the element to set</param>
  153. /// <param name="metadataValue">value to set</param>
  154. /// <param name="defaultValue">If metadataValue is default, node will be removed.</param>
  155. private void SetMetadataElement(XElement elemApplication,
  156. IEnumerable<XElement> metas,
  157. string metadataName,
  158. bool metadataValue,
  159. bool defaultValue = false)
  160. {
  161. XElement element = GetMetaElement(metas, metadataName);
  162. if (metadataValue != defaultValue)
  163. {
  164. if (element == null)
  165. {
  166. elemApplication.Add(CreateMetaElement(metadataName, metadataValue));
  167. }
  168. else
  169. {
  170. element.SetAttributeValue(ns + "value", metadataValue);
  171. }
  172. }
  173. else
  174. {
  175. if (element != null)
  176. {
  177. element.Remove();
  178. }
  179. }
  180. }
  181. private void StopBuildWithMessage(string message)
  182. {
  183. string prefix = "[GoogleMobileAds] ";
  184. #if UNITY_2017_1_OR_NEWER
  185. throw new BuildPlayerWindow.BuildMethodException(prefix + message);
  186. #else
  187. throw new OperationCanceledException(prefix + message);
  188. #endif
  189. }
  190. }
  191. #endif