From 17ea49120aecef9d78bb88313ab705474a0d9d1a Mon Sep 17 00:00:00 2001 From: huyufei Date: Sun, 31 Mar 2024 11:31:50 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E5=AE=8C=E5=96=84=20AndroidManifest?= =?UTF-8?q?=20=E6=96=87=E6=A1=A3=E7=AE=A1=E7=90=86=E5=92=8C=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...inManifestDoc.cs => AndroidManifestDoc.cs} | 183 ++++++++++++------ ...Doc.cs.meta => AndroidManifestDoc.cs.meta} | 0 .../GuruManager/Helper/AndroidManifestMod.cs | 148 +++----------- .../Helper/AndroidManifestMod.cs.old | 178 +++++++++++++++++ .../Helper/AndroidManifestMod.cs.old.meta | 7 + .../Helper/AndroidPushIconHelper.cs | 2 +- Editor/GuruManager/Manager/GuruSDKManager.cs | 3 +- 7 files changed, 344 insertions(+), 177 deletions(-) rename Editor/GuruManager/Helper/{MainManifestDoc.cs => AndroidManifestDoc.cs} (55%) rename Editor/GuruManager/Helper/{MainManifestDoc.cs.meta => AndroidManifestDoc.cs.meta} (100%) create mode 100644 Editor/GuruManager/Helper/AndroidManifestMod.cs.old create mode 100644 Editor/GuruManager/Helper/AndroidManifestMod.cs.old.meta diff --git a/Editor/GuruManager/Helper/MainManifestDoc.cs b/Editor/GuruManager/Helper/AndroidManifestDoc.cs similarity index 55% rename from Editor/GuruManager/Helper/MainManifestDoc.cs rename to Editor/GuruManager/Helper/AndroidManifestDoc.cs index f4b79f3..6a5f28e 100644 --- a/Editor/GuruManager/Helper/MainManifestDoc.cs +++ b/Editor/GuruManager/Helper/AndroidManifestDoc.cs @@ -12,7 +12,7 @@ namespace Guru.Editor /// /// Android 配置修改器 /// - public class MainManifestDoc + public class AndroidManifestDoc { private const string TargetPath = "Plugins/Android/AndroidManifest.xml"; private const string XmlnsAndroid = "xmlns:android"; @@ -20,8 +20,10 @@ namespace Guru.Editor private const string XmlnsTools= "xmlns:tools"; private const string NamespaceTools = "http://schemas.android.com/tools"; - - + private const string UserPermission = "uses-permission"; + private const string MetaData = "meta-data"; + private const string KName = "name"; + private XmlDocument _doc; public XmlDocument Doc => _doc; @@ -30,8 +32,6 @@ namespace Guru.Editor private XmlElement _manifestNode; private XmlElement _applicationNode; - private XmlNodeList _metadataList; - private XmlNodeList _permissionList; #region Initiallize @@ -41,7 +41,7 @@ namespace Guru.Editor /// /// /// - public static MainManifestDoc Load(string docPath = "") + public static AndroidManifestDoc Load(string docPath = "") { if (string.IsNullOrEmpty(docPath)) { @@ -54,14 +54,14 @@ namespace Guru.Editor return null; } - var mod = new MainManifestDoc(); + var mod = new AndroidManifestDoc(); mod.ReadFromPath(docPath); return mod; } - public static MainManifestDoc Read(string xmlStr, string docPath = "") + public static AndroidManifestDoc Read(string xmlStr, string docPath = "") { - var mod = new MainManifestDoc(); + var mod = new AndroidManifestDoc(); mod.ReadFromXml(xmlStr, docPath); return mod; } @@ -103,11 +103,7 @@ namespace Guru.Editor // --- Root Nodes --- _manifestNode = _doc.SelectSingleNode("manifest") as XmlElement; _applicationNode = _doc.SelectSingleNode("manifest/application") as XmlElement; - // --- Metadatas --- - _metadataList = _applicationNode.SelectNodes("meta-data"); - // --- Permissions --- - _permissionList = _applicationNode.SelectNodes("uses-permission"); - + AddXmlnsAndroid(); AddXmlnsTools(); _isReady = true; @@ -158,7 +154,6 @@ namespace Guru.Editor #endregion - #region API public bool AddXmlnsAndroid() @@ -175,14 +170,14 @@ namespace Guru.Editor /// Add Replace Item /// /// - public void AddReplaceItem(string item) + public void AddApplicationReplaceItem(string item) { - if (_manifestNode != null) + if (_applicationNode != null) { List items = new List(5); - if (_manifestNode.HasAttribute("replace", NamespaceTools)) + if (_applicationNode.HasAttribute("replace", NamespaceTools)) { - var arr = _manifestNode.GetAttribute("replace",NamespaceTools).Split(','); + var arr = _applicationNode.GetAttribute("replace",NamespaceTools).Split(','); if(arr != null && arr.Length > 0) { items.AddRange(arr); @@ -191,11 +186,18 @@ namespace Guru.Editor if (!items.Contains(item)) items.Add(item); - _manifestNode.SetAttribute("replace", NamespaceTools, string.Join(",", items)); + _applicationNode.SetAttribute("replace", NamespaceTools, string.Join(",", items)); } } - - + + public void SetApplicationAttribute(string key, string value) + { + if (_applicationNode != null) + { + _applicationNode.SetAttribute(key, NamespaceAndroid, value); + } + } + /// /// Set metadata /// @@ -203,47 +205,122 @@ namespace Guru.Editor /// /// /// - public void SetMetadata(string key, string value, string valueName = "value", string keyName = "name") + public void SetMetadata(string key, string value, string valueName = "value", string keyName = KName) { - _metadataList = _applicationNode?.SelectNodes("meta-data") ?? null; + if (_doc == null || !_isReady) return; - XmlElement item = null; - if (_metadataList != null) + XmlElement node = null; + if (!TryGetMetadata(key, out node, keyName)) { - foreach (XmlElement e in _metadataList) + node = _doc.CreateElement(MetaData); + _applicationNode?.AppendChild(node); + } + + node.SetAttribute(keyName, NamespaceAndroid, key); + node.SetAttribute(valueName, NamespaceAndroid, value); + } + + + /// + /// 添加权限 + /// + /// + /// + public void AddPermission(string key, string keyName = KName) + { + if (_doc == null || !_isReady) return; + + XmlElement node = null; + if(!TryGetPermission(key, out node, keyName)) + { + node = _doc.CreateElement(UserPermission); + _manifestNode?.AppendChild(node); + } + node.SetAttribute(keyName, NamespaceAndroid, key); + } + + /// + /// 删除 Permission + /// + /// + /// + /// + /// + public void RemovePermission(string key, string value = "remove", string keyName = "name", string valueName = "node") + { + if (_doc == null || !_isReady) return; + + XmlElement node = null; + if(!TryGetPermission(key, out node, keyName)) + { + node = _doc.CreateElement(UserPermission); + _manifestNode?.AppendChild(node); + } + node.SetAttribute(keyName, NamespaceAndroid, key); + node.SetAttribute(valueName, NamespaceTools, value); + } + + public bool SetPackageName(string packageName) + { + if (_manifestNode != null) + { + _manifestNode.Attributes["package"].Value = packageName; + return true; + } + return false; + } + + #endregion + + #region Data Opration + + public bool TryGetMetadata(string name, out XmlElement node, string keyName = KName) + { + node = null; + + if(_applicationNode != null) + { + var list = _applicationNode.SelectNodes(MetaData); + if (list != null) { - // var nm = e.GetAttribute("name", NamespaceAndroid); - if (e.GetAttribute(keyName, NamespaceAndroid) is string k && k == key) + foreach (XmlElement e in list) { - item = e; - break; + if (e.GetAttribute(keyName, NamespaceAndroid) == name) + { + node = e; + return true; + } } } } - - if (item == null) - { - item = _doc.CreateElement("meta-data"); - _applicationNode?.AppendChild(item); - } - - item.SetAttribute(keyName, NamespaceAndroid, key); - item.SetAttribute(valueName, NamespaceAndroid, value); + return false; } - - public void SetPermission(string key, string value, string valueName = "value", string keyName = "name") + public bool TryGetPermission(string name, out XmlElement node, string keyName = KName) { - _metadataList = _applicationNode?.SelectNodes("meta-data") ?? null; - - + node = null; + + if(_manifestNode != null) + { + var list = _manifestNode.SelectNodes(UserPermission); + if (list != null) + { + foreach (XmlElement e in list) + { + if (e.GetAttribute(keyName, NamespaceAndroid) == name) + { + node = e; + return true; + } + } + } + } + return false; } - + #endregion - - - + #region Output @@ -256,13 +333,5 @@ namespace Guru.Editor #endregion - - - - - - - - } } \ No newline at end of file diff --git a/Editor/GuruManager/Helper/MainManifestDoc.cs.meta b/Editor/GuruManager/Helper/AndroidManifestDoc.cs.meta similarity index 100% rename from Editor/GuruManager/Helper/MainManifestDoc.cs.meta rename to Editor/GuruManager/Helper/AndroidManifestDoc.cs.meta diff --git a/Editor/GuruManager/Helper/AndroidManifestMod.cs b/Editor/GuruManager/Helper/AndroidManifestMod.cs index 1686955..6013807 100644 --- a/Editor/GuruManager/Helper/AndroidManifestMod.cs +++ b/Editor/GuruManager/Helper/AndroidManifestMod.cs @@ -15,10 +15,12 @@ namespace Guru.Editor private const string TargetPath = "Plugins/Android/AndroidManifest.xml"; private const string ValOptimizeInitialization = "com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"; private const string ValOptimizeAdLoading = "com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"; - private const string NamespaceAndroid = "http://schemas.android.com/apk/res/android"; - private const string NamespaceTools = "http://schemas.android.com/tools"; + private const string PermissionReadPostNotifications = "android.permission.POST_NOTIFICATIONS"; private const string PermissionReadPhoneState = "android.permission.READ_PHONE_STATE"; + private const string NetworkSecurityConfig = "networkSecurityConfig"; + private const string NetworkSecurityConfigValue = "@xml/network_security_config"; + private static string TargetFullPath = Path.Combine(Application.dataPath, TargetPath); @@ -31,121 +33,36 @@ namespace Guru.Editor CopyManifest(); } - var doc = new XmlDocument(); - doc.Load(TargetFullPath); - - SetApplicationMod(doc); - SetPermissionMod(doc); - - doc.Save(TargetFullPath); + FixAndroidManifest(); } + + /// + /// Fix Android Manifest + /// + private static void FixAndroidManifest() + { + var doc = AndroidManifestDoc.Load(TargetFullPath); + + // --- network_security_config --- + doc.SetApplicationAttribute(NetworkSecurityConfig, NetworkSecurityConfigValue); + doc.AddApplicationReplaceItem($"android:{NetworkSecurityConfig}"); + // ---- Metadata --- + doc.SetMetadata(ValOptimizeInitialization, "true"); + doc.SetMetadata(ValOptimizeAdLoading, "true"); + // ---- Permission --- + doc.AddPermission(PermissionReadPostNotifications); + doc.RemovePermission(PermissionReadPhoneState); + // --- Bundle Id --- + doc.SetPackageName(PlayerSettings.applicationIdentifier); + + doc.Save(); + } + + /// - /// Fix all Elements in + /// 拷贝 AndroidManifest /// - /// - private static void SetApplicationMod(XmlDocument doc) - { - string rootName = "manifest/application"; - var rootNode = doc.SelectSingleNode(rootName); - int item1 = 0; - int item2 = 0; - - if (rootNode == null) - { - Debug.LogError($"Can't find root with name {rootName} ..."); - return; - } - - - XmlNodeList metadatas = rootNode.SelectNodes("meta-data"); - if (metadatas != null && metadatas.Count > 0) - { - bool isDirty = false; - - foreach (XmlElement e in metadatas) - { - if (e != null) - { - if (e.HasAttribute("name", NamespaceAndroid)) - { - if (e.Attributes["android:name"].Value == ValOptimizeInitialization) item1 = 1; - if (e.Attributes["android:name"].Value == ValOptimizeAdLoading) item2 = 1; - } - } - } - } - - if (item1 == 0) - { - var e = doc.CreateElement("meta-data"); - e.SetAttribute("name",NamespaceAndroid, ValOptimizeInitialization); - e.SetAttribute("value",NamespaceAndroid, "true"); - rootNode.AppendChild(e); - } - - if (item2 == 0) - { - var e = doc.CreateElement("meta-data"); - e.SetAttribute("name",NamespaceAndroid,ValOptimizeAdLoading); - e.SetAttribute("value",NamespaceAndroid, "true"); - rootNode.AppendChild(e); - } - - var rootE = doc.SelectSingleNode("manifest") as XmlElement; - if (rootE != null) - { - rootE.Attributes["package"].Value = PlayerSettings.applicationIdentifier; // 写入包名 - } - } - - /// - /// Fix all permissions - /// - /// - private static void SetPermissionMod(XmlDocument doc) - { - string attName = "uses-permission"; - string rootName = "manifest"; - bool isBuild = false; - var rootNode = doc.SelectSingleNode(rootName); - - XmlElement item1 = null; - if (rootNode == null) - { - Debug.LogError($"Can't find root with name {rootName} ..."); - return; - } - - XmlNodeList permissions = rootNode.SelectNodes(attName); - if (permissions != null && permissions.Count > 0) - { - foreach (XmlElement e in permissions) - { - if (e != null) - { - if (e.HasAttribute("android:name")) - { - if (e.Attributes["android:name"].Value == PermissionReadPhoneState) item1 = e; - } - } - } - } - - isBuild = false; - if (item1 == null) - { - isBuild = true; - item1 = doc.CreateElement(attName); - } - item1.SetAttribute("name",NamespaceAndroid, PermissionReadPhoneState); - item1.SetAttribute("node",NamespaceTools, "remove"); - if (isBuild) rootNode.AppendChild(item1); - - } - - - private static void CopyManifest() { if (File.Exists(TargetFullPath)) return; @@ -160,10 +77,7 @@ namespace Guru.Editor } } } - - - - + #region Testing [Test] diff --git a/Editor/GuruManager/Helper/AndroidManifestMod.cs.old b/Editor/GuruManager/Helper/AndroidManifestMod.cs.old new file mode 100644 index 0000000..1686955 --- /dev/null +++ b/Editor/GuruManager/Helper/AndroidManifestMod.cs.old @@ -0,0 +1,178 @@ +using System.Collections; +using Unity.EditorCoroutines.Editor; + +namespace Guru.Editor +{ + using NUnit.Framework; + using UnityEditor; + using UnityEngine; + using System; + using System.IO; + using System.Xml; + + public static class AndroidManifestMod + { + private const string TargetPath = "Plugins/Android/AndroidManifest.xml"; + private const string ValOptimizeInitialization = "com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"; + private const string ValOptimizeAdLoading = "com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"; + private const string NamespaceAndroid = "http://schemas.android.com/apk/res/android"; + private const string NamespaceTools = "http://schemas.android.com/tools"; + + private const string PermissionReadPhoneState = "android.permission.READ_PHONE_STATE"; + + private static string TargetFullPath = Path.Combine(Application.dataPath, TargetPath); + + public static bool IsManifestExist() => File.Exists(TargetFullPath); + + public static void Apply() + { + if (!IsManifestExist()) + { + CopyManifest(); + } + + var doc = new XmlDocument(); + doc.Load(TargetFullPath); + + SetApplicationMod(doc); + SetPermissionMod(doc); + + doc.Save(TargetFullPath); + } + + /// + /// Fix all Elements in + /// + /// + private static void SetApplicationMod(XmlDocument doc) + { + string rootName = "manifest/application"; + var rootNode = doc.SelectSingleNode(rootName); + int item1 = 0; + int item2 = 0; + + if (rootNode == null) + { + Debug.LogError($"Can't find root with name {rootName} ..."); + return; + } + + + XmlNodeList metadatas = rootNode.SelectNodes("meta-data"); + if (metadatas != null && metadatas.Count > 0) + { + bool isDirty = false; + + foreach (XmlElement e in metadatas) + { + if (e != null) + { + if (e.HasAttribute("name", NamespaceAndroid)) + { + if (e.Attributes["android:name"].Value == ValOptimizeInitialization) item1 = 1; + if (e.Attributes["android:name"].Value == ValOptimizeAdLoading) item2 = 1; + } + } + } + } + + if (item1 == 0) + { + var e = doc.CreateElement("meta-data"); + e.SetAttribute("name",NamespaceAndroid, ValOptimizeInitialization); + e.SetAttribute("value",NamespaceAndroid, "true"); + rootNode.AppendChild(e); + } + + if (item2 == 0) + { + var e = doc.CreateElement("meta-data"); + e.SetAttribute("name",NamespaceAndroid,ValOptimizeAdLoading); + e.SetAttribute("value",NamespaceAndroid, "true"); + rootNode.AppendChild(e); + } + + var rootE = doc.SelectSingleNode("manifest") as XmlElement; + if (rootE != null) + { + rootE.Attributes["package"].Value = PlayerSettings.applicationIdentifier; // 写入包名 + } + } + + /// + /// Fix all permissions + /// + /// + private static void SetPermissionMod(XmlDocument doc) + { + string attName = "uses-permission"; + string rootName = "manifest"; + bool isBuild = false; + var rootNode = doc.SelectSingleNode(rootName); + + XmlElement item1 = null; + if (rootNode == null) + { + Debug.LogError($"Can't find root with name {rootName} ..."); + return; + } + + XmlNodeList permissions = rootNode.SelectNodes(attName); + if (permissions != null && permissions.Count > 0) + { + foreach (XmlElement e in permissions) + { + if (e != null) + { + if (e.HasAttribute("android:name")) + { + if (e.Attributes["android:name"].Value == PermissionReadPhoneState) item1 = e; + } + } + } + } + + isBuild = false; + if (item1 == null) + { + isBuild = true; + item1 = doc.CreateElement(attName); + } + item1.SetAttribute("name",NamespaceAndroid, PermissionReadPhoneState); + item1.SetAttribute("node",NamespaceTools, "remove"); + if (isBuild) rootNode.AppendChild(item1); + + } + + + + private static void CopyManifest() + { + if (File.Exists(TargetFullPath)) return; + + var path = GuruEditorHelper.GetFilePath($"{nameof(AndroidManifestMod)} t:Script"); + if (!string.IsNullOrEmpty(path)) + { + var from = Path.GetFullPath($"{path}/../../Files/AndroidManifest.txt"); + if (File.Exists(from)) + { + File.Copy(from, TargetFullPath); + } + } + } + + + + + #region Testing + + [Test] + public static void Test_Injection() + { + Apply(); + } + + #endregion + + } +} \ No newline at end of file diff --git a/Editor/GuruManager/Helper/AndroidManifestMod.cs.old.meta b/Editor/GuruManager/Helper/AndroidManifestMod.cs.old.meta new file mode 100644 index 0000000..c8938b1 --- /dev/null +++ b/Editor/GuruManager/Helper/AndroidManifestMod.cs.old.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: f4547abfcddf84bc6b61e884ebfb30e2 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/GuruManager/Helper/AndroidPushIconHelper.cs b/Editor/GuruManager/Helper/AndroidPushIconHelper.cs index 955d655..bf4126e 100644 --- a/Editor/GuruManager/Helper/AndroidPushIconHelper.cs +++ b/Editor/GuruManager/Helper/AndroidPushIconHelper.cs @@ -137,7 +137,7 @@ namespace Guru.Editor File.WriteAllText(path, content); // ----- Inject AndroidManifest.xml ------ - var doc = MainManifestDoc.Load(); + var doc = AndroidManifestDoc.Load(); if (doc != null) { doc.SetMetadata("com.google.firebase.messaging.default_notification_icon", "@drawable/ic_notification", valueName:"resource"); diff --git a/Editor/GuruManager/Manager/GuruSDKManager.cs b/Editor/GuruManager/Manager/GuruSDKManager.cs index d980ade..d7eb1b8 100644 --- a/Editor/GuruManager/Manager/GuruSDKManager.cs +++ b/Editor/GuruManager/Manager/GuruSDKManager.cs @@ -1005,8 +1005,7 @@ namespace Guru.Editor } #endregion - - + #region Push Icon Maker private bool _showSegmentPush = false;