90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
|
|
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 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);
|
|
|
|
public static bool IsManifestExist() => File.Exists(TargetFullPath);
|
|
|
|
public static void Apply()
|
|
{
|
|
if (!IsManifestExist())
|
|
{
|
|
CopyManifest();
|
|
}
|
|
|
|
FixAndroidManifest();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Fix Android Manifest
|
|
/// </summary>
|
|
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();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 拷贝 AndroidManifest
|
|
/// </summary>
|
|
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
|
|
|
|
}
|
|
} |