AndroidBuildProcessor.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEditor.Build;
  6. using System.IO;
  7. using System.Xml;
  8. using UnityEditor.Callbacks;
  9. public class AndroidBuildProcessor
  10. {
  11. // build 开始时
  12. [PostProcessBuildAttribute(1)]
  13. public static void OnPostProcessBuild(BuildTarget target, string path)
  14. {
  15. //throw new System.NotImplementedException();
  16. Debug.Log("OnPostProcessBuild");
  17. if (target == BuildTarget.Android)
  18. {
  19. OnPreprocessBuild_android(target, path);
  20. //读取源文件路径
  21. //string sourceParh = Application.dataPath + "/PottingMobileSDK/google-services.json";
  22. ////获取拷贝路径 = 打包输出路径 + 包名(打包文件夹名) + 下级路径
  23. //string targetPath = path + "/" + PlayerSettings.productName;
  24. ////是否已存在文件夹
  25. //if (!Directory.Exists(targetPath))
  26. //{
  27. // Directory.CreateDirectory(targetPath);
  28. //}
  29. ////拷贝文件(源路径及文件名, 拷贝路径及文件名, 若该文件名已存在,是否替换)
  30. //File.Copy(sourceParh, targetPath + "/google-services.json", true);
  31. }
  32. }
  33. /// <summary>
  34. /// 在生成android apk前,将一些配置写入AndroidManifest.xml
  35. /// </summary>
  36. /// <param name="buildTarget"></param>
  37. /// <param name="path"></param>
  38. public static void OnPreprocessBuild_android(BuildTarget buildTarget, string path)
  39. {
  40. Debug.Log("path: " + path);
  41. if (buildTarget != BuildTarget.Android)
  42. {
  43. return;
  44. }
  45. // 读取xml
  46. string xmlPath = Application.dataPath + "/Plugins/Android/AndroidManifest.xml";
  47. XmlDocument xmlDoc = new XmlDocument();
  48. xmlDoc.Load(xmlPath);
  49. // 包名
  50. XmlNode node = xmlDoc.SelectSingleNode("/manifest");
  51. // API_KEY
  52. node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.facebook.sdk.ApplicationId");
  53. node.Attributes["android:value"].Value = "fb" + AdManagerSettings.Instance.AndroidFacebookID;
  54. node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.facebook.sdk.ClientToken");
  55. node.Attributes["android:value"].Value = AdManagerSettings.Instance.AndroidFacebookClientToken;
  56. node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "applovin.sdk.key");
  57. //node.Attributes["android:value"].Value = jd["android"]["android_manifest"]["yunwa_app_id"].ToString();
  58. node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.google.android.gms.ads.APPLICATION_ID");
  59. node.Attributes["android:value"].Value = AdManagerSettings.Instance.AndroidAdmobId;
  60. node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.google.android.gms.games.APP_ID");
  61. node.Attributes["android:value"].Value = "\\u003"+AdManagerSettings.Instance.GoogleGamesAppID;
  62. //node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.google.android.gms.games.APP_ID");
  63. //node.Attributes["android:value"].Value = AdManagerSettings.Instance.GoogleGamesAppID;
  64. // scheme
  65. XmlNodeList nodes = xmlDoc.SelectNodes("/manifest/application/activity/intent-filter/data");
  66. node = nodes.Item(0);
  67. node.Attributes["android:scheme"].Value = "fb"+ AdManagerSettings.Instance.AndroidFacebookID;
  68. //com.facebook.app.FacebookContentProvider{APP_ID}
  69. node = FindNode(xmlDoc, "/manifest/application/provider", "android:name", "com.facebook.FacebookContentProvider");
  70. node.Attributes["android:authorities"].Value = "com.facebook.app.FacebookContentProvider"+ AdManagerSettings.Instance.AndroidFacebookID;
  71. // 保存
  72. xmlDoc.Save(xmlPath);
  73. AssetDatabase.Refresh();
  74. }
  75. static XmlNode FindNode(XmlDocument xmlDoc, string xpath, string attributeName, string attributeValue)
  76. {
  77. XmlNodeList nodes = xmlDoc.SelectNodes(xpath);
  78. //Debug.Log(nodes.Count);
  79. for (int i = 0; i < nodes.Count; i++)
  80. {
  81. XmlNode node = nodes.Item(i);
  82. string _attributeValue = node.Attributes[attributeName].Value;
  83. if (_attributeValue == attributeValue)
  84. {
  85. return node;
  86. }
  87. }
  88. return null;
  89. }
  90. }