using System.Collections.Generic; using System.IO; using EFSDK; using Unity.Plastic.Newtonsoft.Json.Linq; using UnityEditor.Android; using UnityEngine; public class DynamicApplicationClass : IPostGenerateGradleAndroidProject { public int callbackOrder { get { return int.MaxValue; } } private string GetRootPath(string path) { return Path.Combine(path, "..", ""); } public void OnPostGenerateGradleAndroidProject(string path) { AndroidResAarBuilder.BuildAAR(); var androidManifest = new SDKTool.AndroidManifest(SDKTool.GetManifestPath(path)); androidManifest.SetStartingActivityAttribute("hardwareAccelerated", "true"); androidManifest.Save(); SetGradleConstraints(path); FixedAddressValueTypeAttribute(path); // 仅测试使用 // ParseConfigFile(path); } private static void SetGradleConstraints(string path) { string gradlePath = Path.Combine(path, "../unityLibrary", "build.gradle"); if (!File.Exists(gradlePath)) { Debug.LogError("未找到unityLibrary模块的build.gradle文件: " + gradlePath); } // var content = " implementation \"androidx.datastore:datastore:1.0.0\"\n constraints {\n implementation(\"androidx.datastore:datastore\") {\n version {\n strictly \"1.0.0\"\n }\n because \"1.0.0版本,避免高版本兼容性问题\"\n }\n }"; var buildGradleOutLines = new List(); foreach (var line in File.ReadLines(gradlePath)) { if (line.Trim().Contains("com.earn.money:sdk")) { Debug.Log("找到com.earn.money:sdk"); buildGradleOutLines.Add($" implementation ('com.earn.money:{Application.identifier}:{SDKTool.GetSDKVersion()}')"); } else { buildGradleOutLines.Add(line); } } File.WriteAllText(gradlePath, string.Join("\n", buildGradleOutLines.ToArray()) + "\n"); } private static void FixedAddressValueTypeAttribute(string path) { string launcherBuildGradlePath = Path.Combine(path, "../launcher", "build.gradle"); string content = " configurations.all {\n resolutionStrategy {\n force \"androidx.appcompat:appcompat:1.6.1\"\n force \"androidx.core:core:1.12.0\"\n }\n }"; var launcherBuildGradleOutLines = new List(); foreach (var line in File.ReadLines(launcherBuildGradlePath)) { launcherBuildGradleOutLines.Add(line); if (line.Trim().StartsWith("defaultConfig")) { launcherBuildGradleOutLines.Add(content); } } File.WriteAllText(launcherBuildGradlePath, string.Join("\n", launcherBuildGradleOutLines.ToArray()) + "\n"); } private void ParseConfigFile(string path) { Dictionary jsonDict = ParseToSimpleDictionary(); if (jsonDict == null || jsonDict.Count == 0) { throw new System.Exception("配置文件中未解析到google-services.json"); } // 获取launcher模块的build.gradle路径 string gradlePath = Path.Combine(path, "../launcher", "build.gradle"); if (!File.Exists(gradlePath)) { Debug.LogError("未找到launcher模块的build.gradle文件: " + gradlePath); } // 读取文件内容 string newResValue = $"\n resValue \"string\", \"game_services_project_id\", \"{jsonDict["project_id"]}\"" + $"\n resValue \"string\", \"project_id\", \"{jsonDict["project_id"]}\"" + $"\n resValue \"string\", \"google_project_id\", \"{jsonDict["project_id"]}\"" + $"\n resValue \"string\", \"google_package_name\", \"{jsonDict["package_name"]}\"" + $"\n resValue \"string\", \"gcm_defaultSenderId\", \"{jsonDict["project_number"]}\"" + $"\n resValue \"string\", \"google_api_key\", \"{jsonDict["current_key"]}\"" + $"\n resValue \"string\", \"google_app_id\", \"{jsonDict["mobilesdk_app_id"]}\"" + $"\n resValue \"string\", \"google_crash_reporting_api_key\", \"{jsonDict["current_key"]}\"" + $"\n resValue \"string\", \"google_storage_bucket\", \"{jsonDict["storage_bucket"]}\""; Debug.Log($"DSSdk newResValue: {newResValue}"); string launcherBuildGradlePath = Path.Combine(path, "../launcher", "build.gradle"); var launcherBuildGradleOutLines = new List(); foreach (var line in File.ReadLines(launcherBuildGradlePath)) { launcherBuildGradleOutLines.Add(line); if (line.Trim().StartsWith("defaultConfig")) { launcherBuildGradleOutLines.Add(newResValue); } } File.WriteAllText(launcherBuildGradlePath, string.Join("\n", launcherBuildGradleOutLines.ToArray()) + "\n"); } public Dictionary ParseToSimpleDictionary() { Dictionary simpleDict = new Dictionary(); try { string filePath = Path.Combine(Application.streamingAssetsPath, "google-services.json"); if (!File.Exists(filePath)) { Debug.LogError("文件不存在: " + filePath); return simpleDict; } string jsonContent = File.ReadAllText(filePath); JObject jsonObject = JObject.Parse(jsonContent); // 递归解析所有字段 ParseJToken(jsonObject, simpleDict); return simpleDict; } catch (System.Exception e) { Debug.LogError("解析错误: " + e.Message); return simpleDict; } } /// /// 递归解析JToken并提取字段名和值 /// private void ParseJToken(JToken token, Dictionary dict) { if (token is JObject jObject) { foreach (var property in jObject.Properties()) { // 如果是对象类型,继续递归解析 if (property.Value is JObject || property.Value is JArray) { ParseJToken(property.Value, dict); } else { // 基本类型直接添加,相同key会覆盖 if (dict.ContainsKey(property.Name)) { Debug.LogWarning($"字段名重复,已覆盖: {property.Name}"); } dict[property.Name] = property.Value.ToString(); } } } else if (token is JArray jArray) { // 解析数组中的每个元素 foreach (var item in jArray) { ParseJToken(item, dict); } } } }