ATProcessBuildGradleAndroid.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #if UNITY_ANDROID && UNITY_2018_2_OR_NEWER
  2. using AnyThink.Scripts.IntegrationManager.Editor;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Xml.Linq;
  8. using UnityEditor;
  9. using UnityEditor.Android;
  10. using System.Text.RegularExpressions;
  11. using System.Diagnostics;
  12. using UnityEngine;
  13. using System.Text;
  14. namespace AnyThink.Scripts.Editor
  15. {
  16. public class ATProcessBuildGradleAndroid
  17. {
  18. // public void OnPostGenerateGradleAndroidProject(string path)
  19. // {
  20. // }
  21. public static void processBuildGradle(string path)
  22. {
  23. #if UNITY_2019_3_OR_NEWER
  24. var buildGradlePath = Path.Combine(path, "../build.gradle");
  25. #else
  26. var buildGradlePath = Path.Combine(path, "build.gradle");
  27. #endif
  28. #if UNITY_2022_1_OR_NEWER
  29. ATLog.log("processBuildGradle() >>> called");
  30. #else
  31. replaceBuildPluginVersion(buildGradlePath);
  32. // replaceAppBuildPluginVersion(path);
  33. #endif
  34. // replaceAppBuildPluginVersion(path);
  35. handleNetworksConfit(path);
  36. // handleNetworkResMerge(path);
  37. // callGradleTask(path);
  38. }
  39. //修改项目的根目录下的build.gradle文件的插件版本号
  40. private static void replaceBuildPluginVersion(string buildGradlePath)
  41. {
  42. if (!File.Exists(buildGradlePath))
  43. {
  44. return;
  45. }
  46. string gradleFileContent = "";
  47. using (StreamReader reader = new StreamReader(buildGradlePath))
  48. {
  49. gradleFileContent = reader.ReadToEnd();
  50. }
  51. if (string.IsNullOrEmpty(gradleFileContent))
  52. {
  53. return;
  54. }
  55. string buildGradleVersion = "";
  56. string buildGradlePattern = "";
  57. string buildGradleVersion3 = "3.3.3"; // 新gradle插件版本号
  58. string buildGradlePattern3 = @"(?<=gradle:)3\.3\.\d+";
  59. string buildGradleVersion4 = "3.4.3";
  60. string buildGradlePattern4 = @"(?<=gradle:)3\.4\.\d+";
  61. string buildGradleVersion5 = "3.5.4";
  62. string buildGradlePattern5 = @"(?<=gradle:)3\.5\.\d+";
  63. string buildGradleVersion6 = "3.6.4";
  64. string buildGradlePattern6 = @"(?<=gradle:)3\.6\.\d+";
  65. if (isMatchGradleVersion(gradleFileContent, buildGradleVersion3))
  66. {
  67. buildGradleVersion = buildGradleVersion3;
  68. buildGradlePattern = buildGradlePattern3;
  69. }
  70. else if(isMatchGradleVersion(gradleFileContent, buildGradleVersion4))
  71. {
  72. buildGradleVersion = buildGradleVersion4;
  73. buildGradlePattern = buildGradlePattern4;
  74. }
  75. else if(isMatchGradleVersion(gradleFileContent, buildGradleVersion5))
  76. {
  77. buildGradleVersion = buildGradleVersion5;
  78. buildGradlePattern = buildGradlePattern5;
  79. }
  80. else if(isMatchGradleVersion(gradleFileContent, buildGradleVersion6))
  81. {
  82. buildGradleVersion = buildGradleVersion6;
  83. buildGradlePattern = buildGradlePattern6;
  84. }
  85. if (!string.IsNullOrEmpty(buildGradlePattern) && !string.IsNullOrEmpty(buildGradleVersion))
  86. {
  87. replaceContent(buildGradlePath, buildGradlePattern, buildGradleVersion);
  88. }
  89. }
  90. private static void replaceContent(string filePath, string pattern, string content)
  91. {
  92. if (!File.Exists(filePath))
  93. {
  94. return;
  95. }
  96. string buildGradle = "";
  97. using (StreamReader reader = new StreamReader(filePath))
  98. {
  99. buildGradle = reader.ReadToEnd();
  100. }
  101. // Regex regex = new Regex(pattern);
  102. buildGradle = Regex.Replace(buildGradle, pattern, content);
  103. // 修改gradle-wrapper版本号
  104. // string oldWrapperVersion = "distributionUrl=https\\://services.gradle.org/d
  105. using (StreamWriter writer = new StreamWriter(filePath))
  106. {
  107. writer.Write(buildGradle);
  108. }
  109. }
  110. private static bool isMatchGradleVersion(string gradleFileContent, string version)
  111. {
  112. string matchStr = String.Format("gradle:{0}", version.Substring(0, 3));
  113. return gradleFileContent.Contains(matchStr);
  114. }
  115. //修改app module下的build.gradle
  116. private static void replaceAppBuildPluginVersion(string path)
  117. {
  118. #if UNITY_2019_3_OR_NEWER
  119. var buildGradlePath = Path.Combine(path, "../launcher/build.gradle");
  120. #else
  121. var buildGradlePath = Path.Combine(path, "launcher/build.gradle");
  122. #endif
  123. if (!File.Exists(buildGradlePath))
  124. {
  125. return;
  126. }
  127. string buildGradleVersion = "30";
  128. string compileSdkVersionPattern = "compileSdkVersion";
  129. string targetSdkVersionPattern = "targetSdkVersion";
  130. List<string> lines = new List<string>();
  131. using (StreamReader reader = new StreamReader(buildGradlePath))
  132. {
  133. string line;
  134. while ((line = reader.ReadLine()) != null)
  135. {
  136. lines.Add(line);
  137. }
  138. }
  139. int indexToReplace = -1;
  140. int indexToReplace1 = -1;
  141. int removeIndex = -1;
  142. int addIndex = -1;
  143. for (int i = 0; i < lines.Count; i++)
  144. {
  145. if (lines[i].Contains(compileSdkVersionPattern))
  146. {
  147. indexToReplace = i;
  148. }
  149. else if (lines[i].Contains(targetSdkVersionPattern))
  150. {
  151. indexToReplace1 = i;
  152. }
  153. else if (lines[i].Contains("buildToolsVersion"))
  154. {
  155. removeIndex = i;
  156. }
  157. else if (lines[i].Contains("defaultConfig"))
  158. {
  159. addIndex = i;
  160. }
  161. }
  162. if (indexToReplace != -1)
  163. {
  164. lines[indexToReplace] = " " + compileSdkVersionPattern + " " + buildGradleVersion;
  165. }
  166. if (indexToReplace1 != -1)
  167. {
  168. lines[indexToReplace1] = " " + targetSdkVersionPattern + " " + buildGradleVersion;
  169. }
  170. if (removeIndex != -1)
  171. {
  172. lines.RemoveAt(removeIndex);
  173. }
  174. if (addIndex != -1)
  175. {
  176. lines.Insert(addIndex + 1, " multiDexEnabled true");
  177. }
  178. using (StreamWriter writer = new StreamWriter(buildGradlePath))
  179. {
  180. foreach (string line in lines)
  181. {
  182. writer.WriteLine(line);
  183. }
  184. }
  185. }
  186. private static void handleNetworksConfit(string path)
  187. {
  188. if (ATConfig.isSelectedChina())
  189. {
  190. return;
  191. }
  192. #if UNITY_2019_3_OR_NEWER
  193. var buildGradlePath = Path.Combine(path, "../launcher/build.gradle");
  194. #else
  195. var buildGradlePath = Path.Combine(path, "launcher/build.gradle");
  196. #endif
  197. if (!File.Exists(buildGradlePath))
  198. {
  199. return;
  200. }
  201. List<string> lines = new List<string>();
  202. using (StreamReader reader = new StreamReader(buildGradlePath))
  203. {
  204. string line;
  205. while ((line = reader.ReadLine()) != null)
  206. {
  207. lines.Add(line);
  208. }
  209. }
  210. var androidStartIndex = 0;
  211. var isConfigAll = false;
  212. var isExcludeModule = false;
  213. for (int i = 0; i < lines.Count; i++)
  214. {
  215. if (lines[i].Contains("android {"))
  216. {
  217. androidStartIndex = i;
  218. }
  219. else if (lines[i].Contains("configurations.all"))
  220. {
  221. isConfigAll = true;
  222. }
  223. else if (lines[i].Contains("META-INF/*.kotlin_module"))
  224. {
  225. isExcludeModule = true;
  226. }
  227. }
  228. if (androidStartIndex > 0)
  229. {
  230. if (!isExcludeModule)
  231. {
  232. lines.Insert(androidStartIndex + 1, " packagingOptions {\n merge 'META-INF/com.android.tools/proguard/coroutines.pro'\n exclude 'META-INF/*.kotlin_module'\n }");
  233. }
  234. // if (!isConfigAll)
  235. // {
  236. // lines.Insert(androidStartIndex -1, "configurations.all {\n resolutionStrategy {\n force 'androidx.core:core:1.6.0'\n force 'androidx.recyclerview:recyclerview:1.1.0' \n }\n}");
  237. // }
  238. }
  239. // configurations.all {
  240. // resolutionStrategy {
  241. // force 'androidx.core:core:1.6.0'
  242. // force 'androidx.recyclerview:recyclerview:1.1.0'
  243. // }
  244. // }
  245. // packagingOptions {
  246. // merge "META-INF/com.android.tools/proguard/coroutines.pro"
  247. // exclude "META-INF/*.kotlin_module"
  248. // }
  249. using (StreamWriter writer = new StreamWriter(buildGradlePath))
  250. {
  251. foreach (string line in lines)
  252. {
  253. writer.WriteLine(line);
  254. }
  255. }
  256. }
  257. private static void handleNetworkResMerge(string path) {
  258. ATLog.log("handleNetworkResMerge() >>> path: " + path);
  259. #if UNITY_2019_3_OR_NEWER
  260. var buildGradlePath = Path.Combine(path, "../launcher/build.gradle");
  261. #else
  262. var buildGradlePath = Path.Combine(path, "launcher/build.gradle");
  263. #endif
  264. List<string> lines = new List<string>();
  265. bool isAdded = false;
  266. using (StreamReader reader = new StreamReader(buildGradlePath))
  267. {
  268. string line;
  269. while ((line = reader.ReadLine()) != null)
  270. {
  271. if (line.Contains("task handleNetworkResMerge")) {
  272. isAdded = true;
  273. }
  274. lines.Add(line);
  275. }
  276. }
  277. if (isAdded) {
  278. return;
  279. }
  280. using (StreamReader reader = new StreamReader("Assets/AnyThinkPlugin/Script/Editor/network_res_handle.gradle"))
  281. {
  282. string line;
  283. while ((line = reader.ReadLine()) != null)
  284. {
  285. lines.Add(line);
  286. }
  287. }
  288. using (StreamWriter writer = new StreamWriter(buildGradlePath))
  289. {
  290. foreach (string line in lines)
  291. {
  292. writer.WriteLine(line);
  293. }
  294. }
  295. }
  296. private static void callGradleTask(string path) {
  297. // 设置你想要启动的Gradle任务
  298. string gradleTask = "handleNetworkResMerge"; // 例如: assembleDebug or assembleRelease
  299. // 开始一个新的进程来执行Gradle任务
  300. ProcessStartInfo psi = new ProcessStartInfo();
  301. psi.FileName = Application.platform == RuntimePlatform.WindowsEditor ? "cmd" : "bash";
  302. psi.Arguments = Application.platform == RuntimePlatform.WindowsEditor ?
  303. $"/c gradlew {gradleTask}" : // Windows cmd命令
  304. $"-c './gradlew {gradleTask}'"; // UNIX bash命令
  305. psi.UseShellExecute = false;
  306. psi.StandardOutputEncoding = Encoding.UTF8;
  307. psi.StandardErrorEncoding = Encoding.UTF8;
  308. psi.RedirectStandardOutput = true;
  309. psi.RedirectStandardError = true;
  310. psi.CreateNoWindow = true;
  311. psi.WorkingDirectory = "/Users/quinx/Desktop/workspace_topon/sdk_source/a_unity_demo/TestAnyThinkUnityPlugin/Library/Bee/Android/Prj/Mono2x/Gradle"; // 这里应该是你的Android项目路径
  312. ATLog.log("callGradleTask() >>> path: " + path);
  313. using (var process = Process.Start(psi))
  314. {
  315. // 读取输出信息
  316. while (!process.StandardOutput.EndOfStream)
  317. {
  318. var line = process.StandardOutput.ReadLine();
  319. UnityEngine.Debug.Log(line);
  320. }
  321. // 读取错误信息
  322. while (!process.StandardError.EndOfStream)
  323. {
  324. var line = process.StandardError.ReadLine();
  325. UnityEngine.Debug.LogError(line);
  326. }
  327. }
  328. }
  329. }
  330. }
  331. #endif