update: 增加 AndroidManifestMode 的去 permission 功能

deeplink
胡宇飞 2024-03-12 10:35:30 +08:00
parent 3871841988
commit bb4073aa2f
1 changed files with 78 additions and 11 deletions

View File

@ -15,6 +15,10 @@ 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 PermissionReadPhoneState = "android.permission.READ_PHONE_STATE";
private static string TargetFullPath = Path.Combine(Application.dataPath, TargetPath);
@ -30,10 +34,30 @@ namespace Guru.Editor
var doc = new XmlDocument();
doc.Load(TargetFullPath);
var rootNode = doc.SelectSingleNode("manifest/application");
SetApplicationMod(doc);
SetPermissionMod(doc);
doc.Save(TargetFullPath);
}
/// <summary>
/// Fix all Elements in <Applicaiton>
/// </summary>
/// <param name="doc"></param>
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)
{
@ -52,21 +76,19 @@ namespace Guru.Editor
}
}
string androidSP = "http://schemas.android.com/apk/res/android";
if (item1 == 0)
{
var e = doc.CreateElement("meta-data");
e.SetAttribute("name",androidSP, ValOptimizeInitialization);
e.SetAttribute("value",androidSP, "true");
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",androidSP,ValOptimizeAdLoading);
e.SetAttribute("value",androidSP, "true");
e.SetAttribute("name",NamespaceAndroid,ValOptimizeAdLoading);
e.SetAttribute("value",NamespaceAndroid, "true");
rootNode.AppendChild(e);
}
@ -75,10 +97,55 @@ namespace Guru.Editor
{
rootE.Attributes["package"].Value = PlayerSettings.applicationIdentifier; // 写入包名
}
doc.Save(TargetFullPath);
}
/// <summary>
/// Fix all permissions
/// </summary>
/// <param name="doc"></param>
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;