using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using System.IO;
using System.Xml;
using UnityEditor.Callbacks;
public class AndroidBuildProcessor
{
// build 开始时
[PostProcessBuildAttribute(1)]
public static void OnPostProcessBuild(BuildTarget target, string path)
{
//throw new System.NotImplementedException();
Debug.Log("OnPostProcessBuild");
if (target == BuildTarget.Android)
{
OnPreprocessBuild_android(target, path);
//读取源文件路径
//string sourceParh = Application.dataPath + "/PottingMobileSDK/google-services.json";
////获取拷贝路径 = 打包输出路径 + 包名(打包文件夹名) + 下级路径
//string targetPath = path + "/" + PlayerSettings.productName;
////是否已存在文件夹
//if (!Directory.Exists(targetPath))
//{
// Directory.CreateDirectory(targetPath);
//}
////拷贝文件(源路径及文件名, 拷贝路径及文件名, 若该文件名已存在,是否替换)
//File.Copy(sourceParh, targetPath + "/google-services.json", true);
}
}
///
/// 在生成android apk前,将一些配置写入AndroidManifest.xml
///
///
///
public static void OnPreprocessBuild_android(BuildTarget buildTarget, string path)
{
Debug.Log("path: " + path);
if (buildTarget != BuildTarget.Android)
{
return;
}
// 读取xml
string xmlPath = Application.dataPath + "/Plugins/Android/AndroidManifest.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
// 包名
XmlNode node = xmlDoc.SelectSingleNode("/manifest");
// API_KEY
node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.facebook.sdk.ApplicationId");
node.Attributes["android:value"].Value = "fb" + AdManagerSettings.Instance.AndroidFacebookID;
node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.facebook.sdk.ClientToken");
node.Attributes["android:value"].Value = AdManagerSettings.Instance.AndroidFacebookClientToken;
node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "applovin.sdk.key");
//node.Attributes["android:value"].Value = jd["android"]["android_manifest"]["yunwa_app_id"].ToString();
node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.google.android.gms.ads.APPLICATION_ID");
node.Attributes["android:value"].Value = AdManagerSettings.Instance.AndroidAdmobId;
node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.google.android.gms.games.APP_ID");
node.Attributes["android:value"].Value = "\\u003"+AdManagerSettings.Instance.GoogleGamesAppID;
//node = FindNode(xmlDoc, "/manifest/application/meta-data", "android:name", "com.google.android.gms.games.APP_ID");
//node.Attributes["android:value"].Value = AdManagerSettings.Instance.GoogleGamesAppID;
// scheme
XmlNodeList nodes = xmlDoc.SelectNodes("/manifest/application/activity/intent-filter/data");
node = nodes.Item(0);
node.Attributes["android:scheme"].Value = "fb"+ AdManagerSettings.Instance.AndroidFacebookID;
//com.facebook.app.FacebookContentProvider{APP_ID}
node = FindNode(xmlDoc, "/manifest/application/provider", "android:name", "com.facebook.FacebookContentProvider");
node.Attributes["android:authorities"].Value = "com.facebook.app.FacebookContentProvider"+ AdManagerSettings.Instance.AndroidFacebookID;
// 保存
xmlDoc.Save(xmlPath);
AssetDatabase.Refresh();
}
static XmlNode FindNode(XmlDocument xmlDoc, string xpath, string attributeName, string attributeValue)
{
XmlNodeList nodes = xmlDoc.SelectNodes(xpath);
//Debug.Log(nodes.Count);
for (int i = 0; i < nodes.Count; i++)
{
XmlNode node = nodes.Item(i);
string _attributeValue = node.Attributes[attributeName].Value;
if (_attributeValue == attributeValue)
{
return node;
}
}
return null;
}
}