TuyooSDKPackage/Packages/com.bywaystudios.tuyoosdk/Editor/Android/BuildGradleProcessor.cs
2025-12-06 10:50:57 +08:00

78 lines
2.7 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
class BuildGradleProcessor
{
private string launcherBuildGradle;
private string unityLibraryBuildGradle;
private string rootBuildGradle;
private string rootProjectPath;
private string settings;
public BuildGradleProcessor(string rootProjectPath)
{
this.rootProjectPath = rootProjectPath;
this.launcherBuildGradle = Path.Combine(rootProjectPath, "launcher/build.gradle");
this.unityLibraryBuildGradle = Path.Combine(rootProjectPath, "unityLibrary/build.gradle");
this.rootBuildGradle = Path.Combine(rootProjectPath, "build.gradle");
this.settings = Path.Combine(rootProjectPath, "settings.gradle");
Debug.Log("BuildGradleProcessor, rootProjectPath:" + rootProjectPath + "\n,launcherBuildGradle:"
+ launcherBuildGradle + "\n,unityLibraryBuildGradle:" + unityLibraryBuildGradle + "\n,settings:" + settings);
}
public static string GetAGPVersion(string rootBuildGradle)
{
if (!File.Exists(rootBuildGradle))
{
Debug.LogError("build.gradle 文件不存在: " + rootBuildGradle);
return null;
}
string buildGradleContent = File.ReadAllText(rootBuildGradle);
string agpVersion = null;
// 尝试匹配 id 'com.android.application' version 'x.y.z'
Match idVersionMatch = Regex.Match(buildGradleContent, @"id\s+['""]com\.android\.application['""]\s+version\s+['""](\d+\.\d+\.\d+)['""]");
if (idVersionMatch.Success)
{
agpVersion = idVersionMatch.Groups[1].Value;
Debug.Log("AGP Version from id version: " + agpVersion);
}
else
{
// 如果 id version 未匹配,尝试匹配 classpath 'com.android.tools.build:gradle:x.y.z'
Match classpathMatch = Regex.Match(buildGradleContent, @"classpath\s+['""]com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)['""]");
if (classpathMatch.Success)
{
agpVersion = classpathMatch.Groups[1].Value;
Debug.Log("AGP Version from classpath: " + agpVersion);
}
}
if (agpVersion == null)
{
Debug.LogError("AGP Version not found in build.gradle");
}
return agpVersion;
}
public void process()
{
string agpVersion = GetAGPVersion(rootBuildGradle);
Debug.Log("process, agpVersion:" + agpVersion);
if (agpVersion.StartsWith("7.") || agpVersion.StartsWith("8."))
{
(new BuildGradleProcessor7x(rootProjectPath)).process();
}
else
{
(new BuildGradleProcessor4x(rootProjectPath)).process();
}
}
}