This commit is contained in:
puxuan 2025-08-04 19:14:30 +08:00
parent 4ce72abeb5
commit d351d4549f
5 changed files with 576 additions and 622 deletions

View File

@ -20,61 +20,32 @@ namespace BFEditor.Build
}
}
public static class BuildMode
{
public const string DEV = "dev";
public const string TEST = "test";
public const string PUBLISH = "publish";
}
[Serializable]
public class BuildInfo
{
public string bundleName; // 包名
public string version; // 版本号
public string mode; // 渠道名_debug 或 渠道名_release
public int version_code = 1; // 各自渠道的version_code
public string mode; // 内网测试包,外网测试包,外网正式包
public int versionCode = 1; // 各自渠道的version code
public List<BulidGitInfo> git_info; // 打包的git信息
public bool exportProject = false; // 是否只导出工程
public bool onlyAssetBundle = false; // 是否只打ab包
[NonSerialized]
public bool skipVersion = false; // 是否跳过版本校验
public bool IsGPChannel()
{
return bundleName == "com.combo.heroes.puzzle.rpg";
}
public bool IsGPOfficial()
{
return bundleName == "com.combo.heroes.puzzle.rpg";
}
// dev包使用mono编译不会导出as工程
public bool IsDevChannel()
{
return bundleName == "com.juzu.b6.dev" || bundleName == "com.juzu.b6.dev.android" ||
bundleName == "com.juzu.b6.dev.ios";
}
public bool IsReleaseChannel()
{
return !IsDevChannel();
}
// 是否是内网release
public bool IsLanRelease()
{
return bundleName == "com.juzu.b6.release.android" || bundleName == "com.juzu.b6.release.ios";
}
public bool IsPublish()
{
return !IsDevChannel() && !IsLanRelease();
return mode == BuildMode.PUBLISH;
}
public bool IsIOSPlatform()
{
return bundleName.Contains("ios");
}
public bool IsAndroidPlatform()
{
return !IsIOSPlatform();
}
public string GetBundleName()
{
return bundleName;

View File

@ -48,6 +48,12 @@ namespace BFEditor.Build
return false;
}
// 只打ab包
if (buildInfo.onlyAssetBundle)
{
AssetDatabase.Refresh();
return true;
}
// 删除不进包的多语言
// if (!DeleteOverLanguageAb(buildInfo))
// {
@ -73,19 +79,10 @@ namespace BFEditor.Build
watch.Start();
var result = false;
if (buildInfo.IsAndroidPlatform() && BuildAndroidUtils.BuildAndroidPlayer(buildInfo))
if (BuildAndroidUtils.BuildAndroidPlayer(buildInfo))
{
result = true;
}
#if UNITY_EDITOR_OSX
if (buildInfo.IsIOSPlatform() && BuildIOSUtils.BuildIOSPlayer(buildInfo))
{
result = true;
}
#endif
if (result)
{
watch.Stop();
@ -140,8 +137,7 @@ namespace BFEditor.Build
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
// var buildTarget = buildInfo.IsIOSPlatform() ? BuildTarget.iOS : BuildTarget.Android;
var isRelease = buildInfo.IsReleaseChannel();
var isRelease = true;
Debug.Log("[bfinfo]开始打包资源, bundleName : " + buildInfo.bundleName + " version : " + buildInfo.version + " mode : " + buildInfo.mode);
// // 检查平台
@ -197,7 +193,7 @@ namespace BFEditor.Build
}
// 本地缓存ab
if (buildInfo.IsReleaseChannel())
if (buildInfo.IsPublish())
{
Debug.Log("[bfinfo]正在缓存assetbundles...");
BFEditorUtils.CopyDirWithIgnore(outputPath, Path.Combine(AssetBundleCachePath, buildInfo.version), new List<string> { ".meta" });

View File

@ -6,77 +6,116 @@ namespace BFEditor.Build
public enum BFPlatformOptions
{
AndroidDev = 1,
IOSDev,
AndroidRelease,
AndroidTest,
AndroidGP,
AndroidGPRU
}
public class BuildProjectWindow : EditorWindow
{
private static int versionCode = 18;
private static string versionName = "1.6.5";
private static int VersionCode = 4;
private static string VersionName = "1.2.10";
private static int VersionCodeRU = 12;
private static string VersionNameRU = "0.4.9";
BFPlatformOptions platform = BFPlatformOptions.AndroidDev;
const string ANDROID_DEV_PACKAGE_NAME = "com.juzu.b6.dev.android";
const string ANDROID_RELEASE_PACKAGE_NAME = "com.juzu.b6.release.android";
const string ANDROID_GP_PACKAGE_NAME = "com.combo.heroes.puzzle.rpg";
const string IOS_PACKAGE_NAME = "com.juzu.b6.dev.ios";
const string ANDROID_GP_PACKAGE_NAME = "com.c1.dev.android";
public BuildProjectWindow()
{
titleContent = new GUIContent("打包");
}
{
titleContent = new GUIContent("打包");
}
private void OnEnable() { }
void OnGUI()
{
GUILayout.BeginVertical("box");
EditorGUILayout.LabelField("选择渠道");
platform = (BFPlatformOptions)EditorGUILayout.EnumPopup("", platform);
EditorGUILayout.Space();
EditorGUILayout.LabelField("版本: " + versionName);
EditorGUILayout.Space();
string packageName;
string mode;
string versionName = VersionName;
int versionCode = VersionCode;
bool skipVersion = false;
string appType;
if (platform == BFPlatformOptions.AndroidDev)
{
packageName = ANDROID_DEV_PACKAGE_NAME;
packageName = ANDROID_GP_PACKAGE_NAME;
skipVersion = true;
mode = "dev_debug";
mode = BuildMode.DEV;
versionName = "0.1.0";
versionCode = 1;
appType = "内网测试包";
}
else if(platform == BFPlatformOptions.AndroidRelease)
else if(platform == BFPlatformOptions.AndroidTest)
{
packageName = ANDROID_RELEASE_PACKAGE_NAME;
mode = "release_release";
packageName = ANDROID_GP_PACKAGE_NAME;
mode = BuildMode.TEST;
versionName = "0.1.0";
versionCode = 1;
appType = "外网测试包";
}
else if(platform == BFPlatformOptions.AndroidGP)
{
packageName = ANDROID_GP_PACKAGE_NAME;
mode = "publish_release";
mode = BuildMode.PUBLISH;
appType = "外网正式包";
}
else
{
packageName = IOS_PACKAGE_NAME;
mode = "dev_debug";
packageName = ANDROID_GP_PACKAGE_NAME;
skipVersion = true;
mode = BuildMode.DEV;
versionName = "0.1.0";
versionCode = 1;
appType = "内网测试包";
}
GUILayout.BeginVertical("box");
EditorGUILayout.LabelField("选择");
platform = (BFPlatformOptions)EditorGUILayout.EnumPopup("", platform);
EditorGUILayout.Space();
EditorGUILayout.LabelField("版本: " + versionName);
EditorGUILayout.Space();
EditorGUILayout.LabelField("包名: " + packageName);
EditorGUILayout.LabelField("mode: " + mode);
EditorGUILayout.Space();
EditorGUILayout.LabelField(appType);
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
var buildInfo = new BuildInfo();
buildInfo.version = versionName;
buildInfo.versionCode = versionCode;
buildInfo.mode = mode;
buildInfo.bundleName = packageName;
buildInfo.skipVersion = skipVersion;
if (GUILayout.Button("一键打包"))
if (GUILayout.Button("一键APK"))
{
var buildInfo = new BuildInfo();
buildInfo.version = versionName;
buildInfo.version_code = versionCode;
buildInfo.mode = mode;
buildInfo.bundleName = packageName;
buildInfo.skipVersion = skipVersion;
buildInfo.exportProject = false;
buildInfo.onlyAssetBundle = false;
BuildProjectTools.BuildBFPlayer(buildInfo);
}
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
if (GUILayout.Button("仅导出工程"))
{
buildInfo.exportProject = true;
buildInfo.onlyAssetBundle = false;
BuildProjectTools.BuildBFPlayer(buildInfo);
}
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
if (GUILayout.Button("仅打ab包"))
{
buildInfo.exportProject = true;
buildInfo.onlyAssetBundle = true;
BuildProjectTools.BuildBFPlayer(buildInfo);
}

View File

@ -3,9 +3,9 @@ using UnityEditor;
using System.IO;
using UnityEditor.Build.Reporting;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System;
using System.Threading;
using System.Collections.Generic;
namespace BFEditor.Build
{
@ -24,13 +24,13 @@ namespace BFEditor.Build
static string BuglyPath = Application.dataPath + "/../Bugly";
static string BuglySOPath = Application.dataPath + "/../Bugly/so";
static string GradleExcuteProjectPath = Application.dataPath + "/../BFVersions/android/ub-release";
static string DevAsProjectPath = Application.dataPath + "/../BFVersions/android/dz_dev";
static string LanReleaseAsProjectPath = Application.dataPath + "/../BFVersions/android/dz_release";
static string GoogleAsProjectPath = Application.dataPath + "/../BFVersions/android/dz_google_apk";
static string GoogleCommonProjectPath = Application.dataPath + "/../BFVersions/android/google_common";
static string GPAsProjectPath = Application.dataPath + "/../BFVersions/android/ub-gp"; // gp删档测试渠道
static string GPOfficialAsProjectPath = Application.dataPath + "/../BFVersions/android/ub-google"; // gp正式渠道
static string RuStoreProjectPath = Application.dataPath + "/../BFVersions/android/ru_store";
static string RuProjectPath = Application.dataPath + "/../BFVersions/android/ru";
static string GoogleServicesProjectPath = Application.dataPath + "/../BFVersions/android/google-services";
static string PublishAsProjectPath = Application.dataPath + "/../BFVersions/android/publish_release";
static string KeystoreFilePath = Application.dataPath + "/../BFVersions/android/keystore/dz_keystore.txt";
static string SignShellPath = Application.dataPath + "/../BFFiles/androidkey";
static string GpAlginShellPath = Application.dataPath + "/../BFFiles/androidkey";
static HashSet<string> AABInPackageFileHashSet = new HashSet<string>()
@ -77,10 +77,19 @@ namespace BFEditor.Build
var bpOptions = GetBuildOptions(buildInfo);
var report = BuildPipeline.BuildPlayer(bpOptions);
// 打包成功
// 导出工程成功
if (report.summary.result == BuildResult.Succeeded)
{
return BuildAndroidAPK(buildInfo);
MergeProject(buildInfo, GoogleAsProjectPath);
FixGradleVersion(buildInfo.versionCode, buildInfo.version);
if (buildInfo.exportProject)
{
return true;
}
else
{
return BuildAPK(buildInfo);
}
}
else
{
@ -98,7 +107,7 @@ namespace BFEditor.Build
PlayerSettings.bundleVersion = buildInfo.version;
// 设置VersionCode
PlayerSettings.Android.bundleVersionCode = buildInfo.version_code;
PlayerSettings.Android.bundleVersionCode = buildInfo.versionCode;
// 设置竖屏
PlayerSettings.defaultInterfaceOrientation = UIOrientation.Portrait;
@ -118,41 +127,33 @@ namespace BFEditor.Build
// 设置包名
PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, buildInfo.bundleName);
Debug.Log("[bfinfo]设置包名:" + buildInfo.bundleName);
// 跳过版本控制
var symbols = ANDROID_DEFINE_SYMBOLS;
if (buildInfo.skipVersion)
{
symbols = symbols + ";SKIP_VERSION;";
symbols = symbols + ";SKIP_VERSION";
}
if (buildInfo.mode == BuildMode.DEV)
{
symbols = symbols + ";BF_APP_DEV";
}
else if (buildInfo.mode == BuildMode.TEST)
{
symbols = symbols + ";BF_APP_TEST";
}
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, symbols);
Debug.Log("[bfinfo]设置defineSymbols: " + symbols);
// 是否是dev
var development = buildInfo.IsDevChannel() ? true : false;
EditorUserBuildSettings.development = development;
// 商品名称
// 应用名
if (buildInfo.IsPublish())
{
PlayerSettings.productName = "Knights Combo";
}
else
{
PlayerSettings.productName = development ? "b6-dev" : "b6-release";
}
// BuildType设置dev/release
EditorUserBuildSettings.androidBuildType = development ? AndroidBuildType.Debug : AndroidBuildType.Release;
PlayerSettings.productName = "Pull Pull Pull Heroes";
EditorUserBuildSettings.development = false;
EditorUserBuildSettings.androidBuildType = AndroidBuildType.Release;
// 是否导出as工程
// EditorUserBuildSettings.exportAsGoogleAndroidProject = development ? false : true;
EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
// dev使用Mono release使用IL2CPP
var scriptImp = development ? ScriptingImplementation.Mono2x : ScriptingImplementation.IL2CPP;
var scriptImp = ScriptingImplementation.IL2CPP;
PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, scriptImp);
}
@ -164,29 +165,9 @@ namespace BFEditor.Build
var bpOptions = new BuildPlayerOptions();
bpOptions.scenes = AssetBundleUtils.GetBuildScenes();
bpOptions.target = BuildTarget.Android;
// if (buildInfo.IsDevChannel())
// {
// bpOptions.locationPathName = GetDevApkPathName(buildInfo);
// Debug.Log("[bfinfo]apk path : " + bpOptions.locationPathName);
// }
// else
// {
// bpOptions.locationPathName = GetASProjectPathName();
// Debug.Log("[bfinfo]asProject path : " + bpOptions.locationPathName);
// }
bpOptions.locationPathName = GetASProjectPathName(buildInfo);
BuildOptions options;
if (buildInfo.IsReleaseChannel())
{
options = BuildOptions.None;
}
else
{
options = BuildOptions.Development;
}
BuildOptions options = BuildOptions.None;
bpOptions.options = options;
return bpOptions;
}
@ -200,7 +181,7 @@ namespace BFEditor.Build
static string GetASProjectPathName(BuildInfo buildInfo)
{
var dir = Path.Combine(AS_PROJECT_PATH, buildInfo.mode);
var dir = Path.Combine(AS_PROJECT_PATH, "publish_release");
if (Directory.Exists(dir))
{
Directory.Delete(dir, true);
@ -208,32 +189,53 @@ namespace BFEditor.Build
return dir;
}
static bool BuildAPK(BuildInfo buildInfo)
static bool BuildAPK(BuildInfo buildInfo, bool isAAB = false)
{
if (buildInfo.IsGPChannel())
{
MergeGPToReleaseProject(buildInfo);
FixGradleVersion(buildInfo.version_code, buildInfo.version);
}
// 设置jdk环境变量
string javaHomePath = System.Environment.GetEnvironmentVariable("JAVE_HOME");
if (string.IsNullOrEmpty(javaHomePath))
{
Debug.LogError("[bferror] 找不到环境变量JAVE_HOME");
return false;
}
var gradleFilePath = Path.Combine(PublishAsProjectPath, "gradle.properties");
var gradleFileText = File.ReadAllText(gradleFilePath);
if (!gradleFileText.Contains("org.gradle.java.home"))
{
gradleFileText = gradleFileText + "\norg.gradle.java.home=" + javaHomePath.Replace("\\", "/");
}
File.WriteAllText(gradleFilePath, gradleFileText);
if (buildInfo.IsLanRelease())
{
AddBuglyParamsToReleaseProject();
}
// 设置密钥密码
if (File.Exists(KeystoreFilePath))
{
var password = File.ReadAllText(KeystoreFilePath).Replace("\n", "").Replace("\r", "");
var buildGradlePath = Path.Combine(PublishAsProjectPath, "launcher/build.gradle");
var text = File.ReadAllText(buildGradlePath);
var regex = new Regex("REPLACE_PASSWORD");
text = regex.Replace(text, password);
File.WriteAllText(buildGradlePath, text);
}
Debug.Log("[bfinfo]正在buildApk...");
var success = true;
var args = "";
if (buildInfo.IsReleaseChannel())
{
args += " assembleRelease";
}
else
{
args += " assembleDebug";
}
BFEditorUtils.RunCommond(GRADLE_PATH, args, GradleExcuteProjectPath,
var args = "assembleRelease";
if (isAAB)
{
args = "bundleRelease";
}
string gradleHomePath = System.Environment.GetEnvironmentVariable("GRADLE_HOME");
if (string.IsNullOrEmpty(gradleHomePath))
{
Debug.LogError("[bferror] 找不到环境变量GRADLE_HOME");
return false;
}
#if UNITY_EDITOR_OSX
var gradleCommondPath = Path.Combine(gradleHomePath, "gradle");
#else
var gradleCommondPath = Path.Combine(gradleHomePath, "gradle.bat");
#endif
BFEditorUtils.RunCommond(gradleCommondPath, args, PublishAsProjectPath,
(msg) =>
{
},
@ -245,80 +247,35 @@ namespace BFEditor.Build
Debug.LogError("[bferror] " + errorMsg);
}
});
if (buildInfo.IsGPChannel())
{
// 尝试制作并上传bugly符号表 开新Thread不影响打包流程
if (success)
{
CopySOAndUploadBuglySymbol(buildInfo.bundleName, buildInfo.version);
}
}
return success;
}
static bool BuildAAB(BuildInfo buildInfo)
{
var result = true;
if(buildInfo.IsGPChannel())
{
// MergeGPToReleaseProject(buildInfo);
// FixGradleVersion(buildInfo.version_code, buildInfo.version);
}
return result;
}
static bool BuildAndroidAPK(BuildInfo buildInfo)
{
var result = true;
if(buildInfo.IsDevChannel())
{
MergeProject(buildInfo, DevAsProjectPath);
var dir = Path.Combine(Application.dataPath, "../", AS_PROJECT_PATH, buildInfo.mode);
BFEditorUtils.RunCommond("gradle", " assembleDebug", dir, (msg) => {
}, (errorMsg) => {
Debug.LogError("[bferror] " + errorMsg);
});
}
else if(buildInfo.IsLanRelease())
{
MergeProject(buildInfo, LanReleaseAsProjectPath);
}
else if(buildInfo.IsGPChannel())
{
MergeProject(buildInfo, GoogleAsProjectPath);
FixGradleVersion(buildInfo.version_code, buildInfo.version);
}
return result;
}
/// <summary>
/// 合并dev工程
/// 合并工程
/// </summary>
static void MergeProject(BuildInfo buildInfo, String asProjectPath)
{
Debug.Log("[bfinfo]正在整合dev project...");
Debug.Log("[bfinfo]正在整合 project...");
var dir = Path.Combine(Application.dataPath, "../", AS_PROJECT_PATH, buildInfo.mode);
var dir = Path.Combine(Application.dataPath, "../", AS_PROJECT_PATH, "publish_release");
var javaPath = Path.Combine(dir, "unityLibrary/src/main/java");
var manifestPath = Path.Combine(dir, "unityLibrary/src/main/AndroidManifest.xml");
// 老版本unity需要替换这个2021之后的新版本不需要
// 获取到unity打出的build-id
var reader = new StreamReader(new FileStream(manifestPath, FileMode.Open));
string buildIdLine;
while ((buildIdLine = reader.ReadLine()) != null)
{
if (buildIdLine.Contains("unity.build-id"))
{
Debug.Log("[bfinfo]修正build-id: " + buildIdLine);
break;
}
}
reader.Close();
reader.Dispose();
// var reader = new StreamReader(new FileStream(manifestPath, FileMode.Open));
// string buildIdLine;
// while ((buildIdLine = reader.ReadLine()) != null)
// {
// if (buildIdLine.Contains("unity.build-id"))
// {
// Debug.Log("[bfinfo]修正build-id: " + buildIdLine);
// break;
// }
// }
// reader.Close();
// reader.Dispose();
if (Directory.Exists(javaPath))
{
@ -326,71 +283,62 @@ namespace BFEditor.Build
di.Delete(true);
}
BFEditorUtils.CopyDir(GoogleCommonProjectPath, dir);
// 获取到google_common复制过去的build.gradle和AndroidManifest
var buildGradlePath = Path.Combine(dir, "launcher/build.gradle");
var text = File.ReadAllText(buildGradlePath);
var regex = new Regex("REPLACE_APPLICATION_ID");
text = regex.Replace(text, buildInfo.bundleName);
File.WriteAllText(buildGradlePath, text);
// 如果是俄罗斯支付 则需要覆盖为俄罗斯相关文件
// if (buildInfo.bundleName == BF.BFPlatform.ANDROID_GP_PACKAGE_NAME_RU)
// {
// BFEditorUtils.CopyDir(RuProjectPath, dir);
// // 获取到build.gradle和AndroidManifest
// var buildGradlePath = Path.Combine(dir, "launcher/build.gradle");
// var text = File.ReadAllText(buildGradlePath);
// var regex = new Regex("REPLACE_APPLICATION_ID");
// text = regex.Replace(text, buildInfo.bundleName);
// File.WriteAllText(buildGradlePath, text);
var androidManifestPath = Path.Combine(dir, "launcher/src/main/AndroidManifest.xml");
text = File.ReadAllText(androidManifestPath);
regex = new Regex("REPLACE_APPLICATION_ID");
text = regex.Replace(text, buildInfo.bundleName);
File.WriteAllText(androidManifestPath, text);
// var androidManifestPath = Path.Combine(dir, "launcher/src/main/AndroidManifest.xml");
// text = File.ReadAllText(androidManifestPath);
// regex = new Regex("REPLACE_APPLICATION_ID");
// text = regex.Replace(text, buildInfo.bundleName);
// File.WriteAllText(androidManifestPath, text);
BFEditorUtils.CopyDir(asProjectPath, dir);
// // 还有另一个AndroidManifest
// androidManifestPath = Path.Combine(dir, "unityLibrary/src/main/AndroidManifest.xml");
// text = File.ReadAllText(androidManifestPath);
// regex = new Regex("REPLACE_APPLICATION_ID");
// text = regex.Replace(text, buildInfo.bundleName);
// File.WriteAllText(androidManifestPath, text);
// }
// // 否则使用通用谷歌
// else
// {
BFEditorUtils.CopyDir(GoogleCommonProjectPath, dir);
// 获取到google_common复制过去的build.gradle和AndroidManifest
var buildGradlePath = Path.Combine(dir, "launcher/build.gradle");
var text = File.ReadAllText(buildGradlePath);
var regex = new Regex("REPLACE_APPLICATION_ID");
text = regex.Replace(text, buildInfo.bundleName);
File.WriteAllText(buildGradlePath, text);
text = File.ReadAllText(manifestPath);
regex = new Regex("REPLACE_BUILD_ID");
text = regex.Replace(text, buildIdLine);
File.WriteAllText(manifestPath, text);
}
var androidManifestPath = Path.Combine(dir, "launcher/src/main/AndroidManifest.xml");
text = File.ReadAllText(androidManifestPath);
regex = new Regex("REPLACE_APPLICATION_ID");
text = regex.Replace(text, buildInfo.bundleName);
File.WriteAllText(androidManifestPath, text);
/// <summary>
/// 合并gp工程
/// </summary>
static void MergeGPToReleaseProject(BuildInfo buildInfo)
{
Debug.Log("[bfinfo]正在整合gp渠道sdk...");
var javaPath = GradleExcuteProjectPath + "/src/main/java";
var manifestPath = GradleExcuteProjectPath + "/src/main/AndroidManifest.xml";
// 获取到unity打出的build-id
var reader = new StreamReader(new FileStream(manifestPath, FileMode.Open));
string buildIdLine;
while ((buildIdLine = reader.ReadLine()) != null)
{
if (buildIdLine.Contains("unity.build-id"))
{
Debug.Log("[bfinfo]修正build-id: " + buildIdLine);
break;
}
}
reader.Close();
reader.Dispose();
if (Directory.Exists(javaPath))
{
var di = new DirectoryInfo(javaPath);
di.Delete(true);
}
if (buildInfo.IsGPOfficial())
BFEditorUtils.CopyDir(asProjectPath, dir);
// 还有dz_google_apk下的AndroidManifest
androidManifestPath = Path.Combine(dir, "unityLibrary/src/main/AndroidManifest.xml");
text = File.ReadAllText(androidManifestPath);
regex = new Regex("REPLACE_APPLICATION_ID");
text = regex.Replace(text, buildInfo.bundleName);
File.WriteAllText(androidManifestPath, text);
// }
// 统一替换google-services文件
var gsPath = Path.Combine(GoogleServicesProjectPath, Path.Combine(buildInfo.bundleName, "google-services.json"));
if (File.Exists(gsPath))
{
BFEditorUtils.CopyDir(GPOfficialAsProjectPath, GradleExcuteProjectPath);
var destFilePath = Path.Combine(dir, "launcher/google-services.json");
File.Copy(gsPath, destFilePath, true);
}
else
{
BFEditorUtils.CopyDir(GPAsProjectPath, GradleExcuteProjectPath);
}
var text = File.ReadAllText(manifestPath);
var regex = new Regex("REPLACE_BUILD_ID");
text = regex.Replace(text, buildIdLine);
File.WriteAllText(manifestPath, text);
}
/// <summary>

View File

@ -1,348 +1,348 @@
#if UNITY_EDITOR_OSX
using UnityEngine;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEditor.iOS.Xcode;
using System.IO;
#endif
// #if UNITY_EDITOR_OSX
// using UnityEngine;
// using UnityEditor;
// using UnityEditor.Build.Reporting;
// using UnityEditor.iOS.Xcode;
// using System.IO;
// #endif
namespace BFEditor.Build
{
public static class BuildIOSUtils
{
#if UNITY_EDITOR_OSX
const string IOS_DEFINE_SYMBOLS = "THREAD_SAFE;USE_AB";
const string DEV_XCODE_LOCAL_PATH = "BFVersions/ios/dev";
const string RELEASE_XCODE_LOCAL_PATH = "BFVersions/ios/release";
// namespace BFEditor.Build
// {
// public static class BuildIOSUtils
// {
// #if UNITY_EDITOR_OSX
// const string IOS_DEFINE_SYMBOLS = "THREAD_SAFE;USE_AB";
// const string DEV_XCODE_LOCAL_PATH = "BFVersions/ios/dev";
// const string RELEASE_XCODE_LOCAL_PATH = "BFVersions/ios/release";
static string devXCodePath = Application.dataPath + "/../" + DEV_XCODE_LOCAL_PATH;
static string releaseXCodePath = Application.dataPath + "/../" + RELEASE_XCODE_LOCAL_PATH;
static string devOptionsPListPath = Application.dataPath + "/../" + "BFVersions/ios/exports/dev/ExportOptions.plist";
static string releaseOptionsPListPath = Application.dataPath + "/../" + "BFVersions/ios/exports/release/ExportOptions.plist";
static string publishOptionsPListPath = Application.dataPath + "/../" + "BFVersions/ios/exports/dis/ExportOptions.plist";
// static string devXCodePath = Application.dataPath + "/../" + DEV_XCODE_LOCAL_PATH;
// static string releaseXCodePath = Application.dataPath + "/../" + RELEASE_XCODE_LOCAL_PATH;
// static string devOptionsPListPath = Application.dataPath + "/../" + "BFVersions/ios/exports/dev/ExportOptions.plist";
// static string releaseOptionsPListPath = Application.dataPath + "/../" + "BFVersions/ios/exports/release/ExportOptions.plist";
// static string publishOptionsPListPath = Application.dataPath + "/../" + "BFVersions/ios/exports/dis/ExportOptions.plist";
public static bool BuildIOSPlayer(BuildInfo buildInfo)
{
var buildTarget = BuildTarget.iOS;
// public static bool BuildIOSPlayer(BuildInfo buildInfo)
// {
// var buildTarget = BuildTarget.iOS;
// 检查平台
if (EditorUserBuildSettings.activeBuildTarget != buildTarget)
{
Debug.LogError("[bferror]当前没有在对应平台");
return false;
}
// // 检查平台
// if (EditorUserBuildSettings.activeBuildTarget != buildTarget)
// {
// Debug.LogError("[bferror]当前没有在对应平台");
// return false;
// }
// 重新生成XLua
CompileScriptsUtils.RegenerateXLuaCode(true);
// // 重新生成XLua
// CompileScriptsUtils.RegenerateXLuaCode(true);
// 打包设置
BuildSettings(buildInfo);
// // 打包设置
// BuildSettings(buildInfo);
// 开始打包
var bpOptions = GetBuildOptions(buildInfo);
var report = BuildPipeline.BuildPlayer(bpOptions);
if (report.summary.result == BuildResult.Succeeded)
{
return BuildIpaFromXCode(buildInfo);
}
else
{
Debug.LogError("[bferror]unity打包xcode失败");
return false;
}
}
// // 开始打包
// var bpOptions = GetBuildOptions(buildInfo);
// var report = BuildPipeline.BuildPlayer(bpOptions);
// if (report.summary.result == BuildResult.Succeeded)
// {
// return BuildIpaFromXCode(buildInfo);
// }
// else
// {
// Debug.LogError("[bferror]unity打包xcode失败");
// return false;
// }
// }
/// <summary>
/// 打包设置
/// </summary>
static void BuildSettings(BuildInfo buildInfo)
{
// 设置bundleVersion
PlayerSettings.bundleVersion = buildInfo.version;
// /// <summary>
// /// 打包设置
// /// </summary>
// static void BuildSettings(BuildInfo buildInfo)
// {
// // 设置bundleVersion
// PlayerSettings.bundleVersion = buildInfo.version;
// 设置buildNumber
PlayerSettings.iOS.buildNumber = buildInfo.version_code.ToString();
// // 设置buildNumber
// PlayerSettings.iOS.buildNumber = buildInfo.versionCode.ToString();
// 设置竖屏
PlayerSettings.defaultInterfaceOrientation = UIOrientation.Portrait;
PlayerSettings.allowedAutorotateToPortrait = false;
PlayerSettings.allowedAutorotateToPortraitUpsideDown = false;
PlayerSettings.allowedAutorotateToLandscapeLeft = false;
PlayerSettings.allowedAutorotateToLandscapeRight = false;
// // 设置竖屏
// PlayerSettings.defaultInterfaceOrientation = UIOrientation.Portrait;
// PlayerSettings.allowedAutorotateToPortrait = false;
// PlayerSettings.allowedAutorotateToPortraitUpsideDown = false;
// PlayerSettings.allowedAutorotateToLandscapeLeft = false;
// PlayerSettings.allowedAutorotateToLandscapeRight = false;
// 允许Xcode根据appleDeveloperTeamID自动签署应用程序
PlayerSettings.iOS.appleEnableAutomaticSigning = !buildInfo.IsPublish();
// // 允许Xcode根据appleDeveloperTeamID自动签署应用程序
// PlayerSettings.iOS.appleEnableAutomaticSigning = !buildInfo.IsPublish();
// 使用手动签名时iOS资源调配配置文件的类型自动
PlayerSettings.iOS.iOSManualProvisioningProfileType = buildInfo.IsPublish() ? ProvisioningProfileType.Distribution : ProvisioningProfileType.Automatic;
// // 使用手动签名时iOS资源调配配置文件的类型自动
// PlayerSettings.iOS.iOSManualProvisioningProfileType = buildInfo.IsPublish() ? ProvisioningProfileType.Distribution : ProvisioningProfileType.Automatic;
// 关闭启动动画
PlayerSettings.SplashScreen.show = false;
// // 关闭启动动画
// PlayerSettings.SplashScreen.show = false;
// 设置包名
PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, buildInfo.bundleName);
Debug.Log("[bfinfo]设置包名:" + buildInfo.bundleName);
// // 设置包名
// PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, buildInfo.bundleName);
// Debug.Log("[bfinfo]设置包名:" + buildInfo.bundleName);
// 是否跳过版本控制
var symbols = IOS_DEFINE_SYMBOLS;
if (buildInfo.skipVersion)
{
symbols = symbols + ";SKIP_VERSION;";
}
// // 是否跳过版本控制
// var symbols = IOS_DEFINE_SYMBOLS;
// if (buildInfo.skipVersion)
// {
// symbols = symbols + ";SKIP_VERSION;";
// }
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, symbols);
Debug.Log("[bfinfo]设置defineSymbols: " + symbols);
// PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, symbols);
// Debug.Log("[bfinfo]设置defineSymbols: " + symbols);
// 是否是dev
var development = buildInfo.IsDevChannel();
EditorUserBuildSettings.development = development;
// // 是否是dev
// var development = buildInfo.IsDevChannel();
// EditorUserBuildSettings.development = development;
// 商品名称
if (buildInfo.IsPublish())
{
PlayerSettings.productName = "Heroic Expedition";
}
else
{
PlayerSettings.productName = development ? "b6-dev" : "b6-release";
}
// // 商品名称
// if (buildInfo.IsPublish())
// {
// PlayerSettings.productName = "Heroic Expedition";
// }
// else
// {
// PlayerSettings.productName = development ? "b6-dev" : "b6-release";
// }
// BuildType设置dev/release
EditorUserBuildSettings.iOSXcodeBuildConfig = development ? XcodeBuildConfig.Debug : XcodeBuildConfig.Release;
// // BuildType设置dev/release
// EditorUserBuildSettings.iOSXcodeBuildConfig = development ? XcodeBuildConfig.Debug : XcodeBuildConfig.Release;
// 使用IL2CPP
var scriptImp = ScriptingImplementation.IL2CPP;
PlayerSettings.SetScriptingBackend(BuildTargetGroup.iOS, scriptImp);
// // 使用IL2CPP
// var scriptImp = ScriptingImplementation.IL2CPP;
// PlayerSettings.SetScriptingBackend(BuildTargetGroup.iOS, scriptImp);
// 目标平台架构目前支持ARM64
PlayerSettings.SetArchitecture(BuildTargetGroup.iOS, 1);
}
// // 目标平台架构目前支持ARM64
// PlayerSettings.SetArchitecture(BuildTargetGroup.iOS, 1);
// }
/// <summary>
/// 获取打包参数
/// </summary>
static BuildPlayerOptions GetBuildOptions(BuildInfo buildInfo)
{
var bpOptions = new BuildPlayerOptions();
bpOptions.scenes = AssetBundleUtils.GetBuildScenes();
bpOptions.target = BuildTarget.iOS;
// /// <summary>
// /// 获取打包参数
// /// </summary>
// static BuildPlayerOptions GetBuildOptions(BuildInfo buildInfo)
// {
// var bpOptions = new BuildPlayerOptions();
// bpOptions.scenes = AssetBundleUtils.GetBuildScenes();
// bpOptions.target = BuildTarget.iOS;
var path = buildInfo.IsReleaseChannel() ? RELEASE_XCODE_LOCAL_PATH : DEV_XCODE_LOCAL_PATH;
bpOptions.locationPathName = path;
Debug.Log("[bfinfo]xcode path : " + path);
var absolutePath = buildInfo.IsReleaseChannel() ? releaseXCodePath : devXCodePath;
if (Directory.Exists(absolutePath))
{
Directory.Delete(absolutePath, true);
}
// var path = buildInfo.IsReleaseChannel() ? RELEASE_XCODE_LOCAL_PATH : DEV_XCODE_LOCAL_PATH;
// bpOptions.locationPathName = path;
// Debug.Log("[bfinfo]xcode path : " + path);
// var absolutePath = buildInfo.IsReleaseChannel() ? releaseXCodePath : devXCodePath;
// if (Directory.Exists(absolutePath))
// {
// Directory.Delete(absolutePath, true);
// }
if (!buildInfo.IsPublish())
{
var options = BuildOptions.Development | BuildOptions.ConnectWithProfiler | BuildOptions.AllowDebugging;
bpOptions.options = options;
}
return bpOptions;
}
// if (!buildInfo.IsPublish())
// {
// var options = BuildOptions.Development | BuildOptions.ConnectWithProfiler | BuildOptions.AllowDebugging;
// bpOptions.options = options;
// }
// return bpOptions;
// }
/// <summary>
/// 打包ipa
/// </summary>
static bool BuildIpaFromXCode(BuildInfo buildInfo)
{
// 修改XCode设置
FixXCodeProject(buildInfo);
// /// <summary>
// /// 打包ipa
// /// </summary>
// static bool BuildIpaFromXCode(BuildInfo buildInfo)
// {
// // 修改XCode设置
// FixXCodeProject(buildInfo);
// 权限
UnlockKeyChain();
// // 权限
// UnlockKeyChain();
// archive
if (!Archive(buildInfo))
{
return false;
}
// // archive
// if (!Archive(buildInfo))
// {
// return false;
// }
// 导出ipa
if (!ExportIpa(buildInfo))
{
return false;
}
// // 导出ipa
// if (!ExportIpa(buildInfo))
// {
// return false;
// }
return true;
}
// return true;
// }
/// <summary>
/// xCode工程设置
/// </summary>
static void FixXCodeProject(BuildInfo buildInfo)
{
var isDev = buildInfo.IsDevChannel();
var xCodeProjectPath = isDev ? devXCodePath : releaseXCodePath;
// /// <summary>
// /// xCode工程设置
// /// </summary>
// static void FixXCodeProject(BuildInfo buildInfo)
// {
// var isDev = buildInfo.IsDevChannel();
// var xCodeProjectPath = isDev ? devXCodePath : releaseXCodePath;
var path = xCodeProjectPath + "/Unity-iPhone.xcodeproj/project.pbxproj";
var pbxProject = new PBXProject();
pbxProject.ReadFromFile(path);
var targetGuid = pbxProject.TargetGuidByName("Unity-iPhone");
pbxProject.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC");
pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
pbxProject.SetBuildProperty(targetGuid, "DEVELOPMENT_TEAM", "49QQW8856Q");
if (buildInfo.IsPublish())
{
pbxProject.SetBuildProperty(targetGuid, "PROVISIONING_PROFILE_SPECIFIER", "ub_appstore_dis");
}
// var path = xCodeProjectPath + "/Unity-iPhone.xcodeproj/project.pbxproj";
// var pbxProject = new PBXProject();
// pbxProject.ReadFromFile(path);
// var targetGuid = pbxProject.TargetGuidByName("Unity-iPhone");
// pbxProject.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC");
// pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
// pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
// pbxProject.SetBuildProperty(targetGuid, "DEVELOPMENT_TEAM", "49QQW8856Q");
// if (buildInfo.IsPublish())
// {
// pbxProject.SetBuildProperty(targetGuid, "PROVISIONING_PROFILE_SPECIFIER", "ub_appstore_dis");
// }
// 添加系统库
AddSystemLibReferenceToProject(pbxProject, targetGuid, "libsqlite3.tbd");
AddSystemLibReferenceToProject(pbxProject, targetGuid, "libz.1.tbd");
AddSystemLibReferenceToProject(pbxProject, targetGuid, "libiconv.2.tbd");
AddSystemLibReferenceToProject(pbxProject, targetGuid, "libresolv.9.tbd");
// // 添加系统库
// AddSystemLibReferenceToProject(pbxProject, targetGuid, "libsqlite3.tbd");
// AddSystemLibReferenceToProject(pbxProject, targetGuid, "libz.1.tbd");
// AddSystemLibReferenceToProject(pbxProject, targetGuid, "libiconv.2.tbd");
// AddSystemLibReferenceToProject(pbxProject, targetGuid, "libresolv.9.tbd");
var pListPath = Path.Combine(xCodeProjectPath, "Info.plist");
var pList = new PlistDocument();
pList.ReadFromFile(pListPath);
// var pListPath = Path.Combine(xCodeProjectPath, "Info.plist");
// var pList = new PlistDocument();
// pList.ReadFromFile(pListPath);
// 版本号
var vKey = "CFBundleShortVersionString";
var vValue = new PlistElementString(buildInfo.version);
var pListRoot = pList.root;
var rootDict = pListRoot.values;
if (!rootDict.ContainsKey(vKey))
{
rootDict.Add(vKey, vValue);
}
else
{
rootDict[vKey] = vValue;
}
// // 版本号
// var vKey = "CFBundleShortVersionString";
// var vValue = new PlistElementString(buildInfo.version);
// var pListRoot = pList.root;
// var rootDict = pListRoot.values;
// if (!rootDict.ContainsKey(vKey))
// {
// rootDict.Add(vKey, vValue);
// }
// else
// {
// rootDict[vKey] = vValue;
// }
// VersionCode
var vCodeKey = "CFBundleVersion";
var vCodeValue = new PlistElementString(buildInfo.version_code.ToString());
if (!rootDict.ContainsKey(vCodeKey))
{
rootDict.Add(vCodeKey, vCodeValue);
}
else
{
rootDict[vCodeKey] = vCodeValue;
}
// // VersionCode
// var vCodeKey = "CFBundleVersion";
// var vCodeValue = new PlistElementString(buildInfo.versionCode.ToString());
// if (!rootDict.ContainsKey(vCodeKey))
// {
// rootDict.Add(vCodeKey, vCodeValue);
// }
// else
// {
// rootDict[vCodeKey] = vCodeValue;
// }
// 数美SDK会使用位置必须加入这个说明
var localtionKey = "NSLocationWhenInUseUsageDescription";
var localtionValue = new PlistElementString("We use your location to give you a better localization.");
if (!rootDict.ContainsKey(localtionKey))
{
rootDict.Add(localtionKey, localtionValue);
}
else
{
rootDict[localtionKey] = localtionValue;
}
// // 数美SDK会使用位置必须加入这个说明
// var localtionKey = "NSLocationWhenInUseUsageDescription";
// var localtionValue = new PlistElementString("We use your location to give you a better localization.");
// if (!rootDict.ContainsKey(localtionKey))
// {
// rootDict.Add(localtionKey, localtionValue);
// }
// else
// {
// rootDict[localtionKey] = localtionValue;
// }
// 提审提示缺少出口合规证明,这里直接设置为false即可
var encryptionKey = "ITSAppUsesNonExemptEncryption";
var encryptionValue = new PlistElementBoolean(false);
if (!rootDict.ContainsKey(encryptionKey))
{
rootDict.Add(encryptionKey, encryptionValue);
}
else
{
rootDict[encryptionKey] = encryptionValue;
}
// // 提审提示缺少出口合规证明,这里直接设置为false即可
// var encryptionKey = "ITSAppUsesNonExemptEncryption";
// var encryptionValue = new PlistElementBoolean(false);
// if (!rootDict.ContainsKey(encryptionKey))
// {
// rootDict.Add(encryptionKey, encryptionValue);
// }
// else
// {
// rootDict[encryptionKey] = encryptionValue;
// }
pList.WriteToFile(pListPath);
pbxProject.WriteToFile(path);
}
// pList.WriteToFile(pListPath);
// pbxProject.WriteToFile(path);
// }
//添加系统lib方法
static void AddSystemLibReferenceToProject(PBXProject pbxProject, string targetGuid, string lib)
{
var fileGuid = pbxProject.AddFile("usr/lib/" + lib, "Frameworks/" + lib, PBXSourceTree.Sdk);
pbxProject.AddFileToBuild(targetGuid, fileGuid);
}
// //添加系统lib方法
// static void AddSystemLibReferenceToProject(PBXProject pbxProject, string targetGuid, string lib)
// {
// var fileGuid = pbxProject.AddFile("usr/lib/" + lib, "Frameworks/" + lib, PBXSourceTree.Sdk);
// pbxProject.AddFileToBuild(targetGuid, fileGuid);
// }
/// <summary>
/// Archive
/// </summary>
static bool Archive(BuildInfo buildInfo)
{
Debug.Log("[bfinfo]正在archive...");
var result = true;
var xCodeProjectPath = buildInfo.IsDevChannel() ? devXCodePath : releaseXCodePath;
var archivePath = xCodeProjectPath + "/build/archive/Unity-iPhone.xcarchive";
var args = string.Format("archive -scheme Unity-iPhone -configuration Release -archivePath {0}", archivePath);
BFEditorUtils.RunCommond("xcodebuild", args, xCodeProjectPath,
(info) =>
{
Debug.Log(info);
},
(error) =>
{
if (error.Contains("ARCHIVE FAILED")) // 失败标志
{
result = false;
}
Debug.LogError("[bferror] " + error);
}
);
// /// <summary>
// /// Archive
// /// </summary>
// static bool Archive(BuildInfo buildInfo)
// {
// Debug.Log("[bfinfo]正在archive...");
// var result = true;
// var xCodeProjectPath = buildInfo.IsDevChannel() ? devXCodePath : releaseXCodePath;
// var archivePath = xCodeProjectPath + "/build/archive/Unity-iPhone.xcarchive";
// var args = string.Format("archive -scheme Unity-iPhone -configuration Release -archivePath {0}", archivePath);
// BFEditorUtils.RunCommond("xcodebuild", args, xCodeProjectPath,
// (info) =>
// {
// Debug.Log(info);
// },
// (error) =>
// {
// if (error.Contains("ARCHIVE FAILED")) // 失败标志
// {
// result = false;
// }
// Debug.LogError("[bferror] " + error);
// }
// );
return result;
}
// return result;
// }
/// <summary>
/// 导出ipa
/// </summary>
static bool ExportIpa(BuildInfo buildInfo)
{
Debug.Log("[bfinfo]正在导出ipa...");
var result = false;
var xCodeProjectPath = buildInfo.IsDevChannel() ? devXCodePath : releaseXCodePath;
string exportPListPath;
if (buildInfo.IsPublish())
{
exportPListPath = publishOptionsPListPath;
}
else
{
exportPListPath = buildInfo.IsDevChannel() ? devOptionsPListPath : releaseOptionsPListPath;
}
var ipaPath = xCodeProjectPath + "/build/ipa";
var archivePath = xCodeProjectPath + "/build/archive/Unity-iPhone.xcarchive";
var args = string.Format("-exportArchive -archivePath {0} -exportPath {1} -exportOptionsPlist {2} -allowProvisioningUpdates", archivePath, ipaPath, exportPListPath);
BFEditorUtils.RunCommond("xcodebuild", args, null,
(info) =>
{
if(info.Contains("EXPORT SUCCEEDED"))
{
result = true;
}
},
(error) =>
{
}
);
// /// <summary>
// /// 导出ipa
// /// </summary>
// static bool ExportIpa(BuildInfo buildInfo)
// {
// Debug.Log("[bfinfo]正在导出ipa...");
// var result = false;
// var xCodeProjectPath = buildInfo.IsDevChannel() ? devXCodePath : releaseXCodePath;
// string exportPListPath;
// if (buildInfo.IsPublish())
// {
// exportPListPath = publishOptionsPListPath;
// }
// else
// {
// exportPListPath = buildInfo.IsDevChannel() ? devOptionsPListPath : releaseOptionsPListPath;
// }
// var ipaPath = xCodeProjectPath + "/build/ipa";
// var archivePath = xCodeProjectPath + "/build/archive/Unity-iPhone.xcarchive";
// var args = string.Format("-exportArchive -archivePath {0} -exportPath {1} -exportOptionsPlist {2} -allowProvisioningUpdates", archivePath, ipaPath, exportPListPath);
// BFEditorUtils.RunCommond("xcodebuild", args, null,
// (info) =>
// {
// if(info.Contains("EXPORT SUCCEEDED"))
// {
// result = true;
// }
// },
// (error) =>
// {
// }
// );
return result;
}
// return result;
// }
/// <summary>
/// 远程打包签名ipa时需要钥匙串权限
/// </summary>
static void UnlockKeyChain()
{
BFEditorUtils.RunCommond("security", "-v unlock-keychain -p '123456' /Users/aoddabao/Library/Keychains/login.keychain-db", null,
(msg) =>
{
Debug.Log(msg);
},
(msg) =>
{
Debug.LogError(msg);
}
);
}
#endif
}
}
// /// <summary>
// /// 远程打包签名ipa时需要钥匙串权限
// /// </summary>
// static void UnlockKeyChain()
// {
// BFEditorUtils.RunCommond("security", "-v unlock-keychain -p '123456' /Users/aoddabao/Library/Keychains/login.keychain-db", null,
// (msg) =>
// {
// Debug.Log(msg);
// },
// (msg) =>
// {
// Debug.LogError(msg);
// }
// );
// }
// #endif
// }
// }