public static void BuildApk(string APK_ROOT, ScriptingImplementation scriptBackend, bool isDevelop = false) { try { try { if (Directory.Exists(APK_ROOT)) { Directory.Delete(APK_ROOT, true); } if (!Directory.Exists(APK_ROOT)) { Directory.CreateDirectory(APK_ROOT); } } catch (Exception e) { } BuildAndriodTasks tasks = JsonUtility.FromJson <BuildAndriodTasks>(File.ReadAllText(ROOT_DIR + "/build_android_task.json")); BuildAndriodCommon common = tasks.common; //设置 打包的场景 foreach (var task in tasks.tasks) { Thread.Sleep(1000); //sync AssetDatabase.Refresh(ImportAssetOptions.Default); List <string> _scenes = new System.Collections.Generic.List <string>(); foreach (var sc in EditorBuildSettings.scenes) { if (sc.enabled == false) { continue; } _scenes.Add(sc.path); } //设置apk 包体信息 if (task.devconfig_download_sources == 10) { // 百度 PlayerSettings.SplashScreen.show = false; } else { PlayerSettings.SplashScreen.show = true; } Patches.Version v = new Patches.Version(); v.Parse(common.__ENGINE_VERSION__); PlayerSettings.Android.bundleVersionCode = common.bundle_version_code; PlayerSettings.bundleVersion = string.Format("{0}.{1}.{2}", v.MainVersion, v.SubVersion, task.devconfig_download_sources); PlayerSettings.applicationIdentifier = task.package_name; PlayerSettings.Android.minSdkVersion = (AndroidSdkVersions)common.minSdkVersion; //修正 target SDK version 的值 // if (common.devconfig_server_platform == "ServerRelease") // { // PlayerSettings.Android.targetSdkVersion = AndroidSdkVersions.AndroidApiLevel23; // } // else // { // PlayerSettings.Android.targetSdkVersion = AndroidSdkVersions.AndroidApiLevel25; // } Process_DevCinfig_Auto_GEN(task.devconfig_download_sources, common.devconfig_server_platform, common.__Version__, common.__ENGINE_VERSION__); Process_AD_SDK(task); Process_PUSH_SDK(task); Process_LebianClientChId(task); PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, scriptBackend); string file = APK_ROOT + "/" + common.output_name_pre + common.version + "." + task.devconfig_download_sources + ".apk"; try { File.Delete(file); } catch (Exception e) { } //sync AssetDatabase.Refresh(ImportAssetOptions.Default); /*UnityEditor.Build.Reporting.BuildReport error = */ if (isDevelop) { BuildPipeline.BuildPlayer(_scenes.ToArray(), file, BuildTarget.Android, BuildOptions.Development | BuildOptions.ConnectWithProfiler); } else { BuildPipeline.BuildPlayer(_scenes.ToArray(), file, BuildTarget.Android, BuildOptions.None); } /* if (error.) * { * Debug.LogError("build faild " + error); * return; * }*/ Debug.Log("build " + file + " ok"); } } catch (Exception e) { Debug.LogError("build faild " + e.Message); } }
public static void PostProcessBuild(BuildTarget target, string path) { ScriptingImplementation scriptingBackend = PlayerSettings.GetScriptingBackend(BuildTargetGroup.Android); string apkRoot = scriptingBackend == ScriptingImplementation.IL2CPP ? APK_DIR_ROOT_IL2CPP : APK_DIR_ROOT_MONO; if (target == BuildTarget.Android) { string file_name = Path.GetFileNameWithoutExtension(path); if (scriptingBackend == ScriptingImplementation.IL2CPP) { //copy IL2CPP symbol文件到目标目录 if (!Directory.Exists("IL2CPP_Symbols")) { Directory.CreateDirectory("IL2CPP_Symbols"); } if (!Directory.Exists("IL2CPP_Symbols")) { Directory.CreateDirectory("IL2CPP_Symbols"); } if (!Directory.Exists("IL2CPP_Symbols/" + file_name)) { Directory.CreateDirectory("IL2CPP_Symbols/" + file_name); } File.Copy("Temp/StagingArea/symbols/armeabi-v7a/libil2cpp.so.debug", "IL2CPP_Symbols/" + file_name + "/libil2cpp.so.debug.so", true); File.Copy("Temp/StagingArea/symbols/armeabi-v7a/libil2cpp.sym", "IL2CPP_Symbols/" + file_name + "/libil2cpp.sym.so", true); } if (Directory.Exists(file_name)) { try { Directory.Delete(file_name, true); } catch (Exception e) { } } try { File.Delete(apkRoot + "/" + file_name + "_unsigned.apk"); } catch (Exception e) { } //setp depress { System.Diagnostics.Process proc = System.Diagnostics.Process.Start(Application.dataPath + "/../RuntimeBuild/tools/apktool.bat", "d " + apkRoot + "/" + file_name + ".apk"); proc.WaitForExit(); File.Delete(path); } // step rename package name { string packageName = Application.identifier; string androidManifest = File.ReadAllText(file_name + "/AndroidManifest.xml"); // 把其他渠道的包名都改成官网包包名 androidManifest = androidManifest.Replace(packageName, "com.example.gcloudu3ddemo"); // androidManifest = androidManifest.Replace("com.example.gcloudu3ddemo", packageName); string[] strs = file_name.Split('.'); int downloadChannel; if (strs == null || strs.Length == 0 || !int.TryParse(strs[strs.Length - 1], out downloadChannel)) { Debug.LogError("APK生成失败,APK输出名字格式错误(后缀应该是下载渠道)."); } else { BuildAndriodTasks tasks = JsonUtility.FromJson <BuildAndriodTasks>(File.ReadAllText(Application.dataPath + "/../RuntimeBuild/build_android_task.json")); for (int i = 0; i < tasks.tasks_full.Count; i++) { var task = tasks.tasks_full[i]; if (task.devconfig_download_sources == downloadChannel && task.package_name == packageName) { // 个推 参数配置 androidManifest = androidManifest.Replace("GETUI_APP_ID_VALUE", task.getui_app_id); androidManifest = androidManifest.Replace("GETUI_APP_KEY_VALUE", task.getui_app_key); androidManifest = androidManifest.Replace("GETUI_APP_SECRET_VALUE", task.getui_app_secret); androidManifest = androidManifest.Replace("GInsight_APP_ID_VALUE", task.gisight_app_id); androidManifest = androidManifest.Replace("GT_INSTALL_CHANNEL_VALUE", task.gisight_channel); // APPLOG 参数配置 // channel androidManifest = androidManifest.Replace("official", task.applog_channel); // appId androidManifest = androidManifest.Replace("164262", task.applog_appId); // applog_config_value androidManifest = androidManifest.Replace("TTAL_15", "TTAL_" + task.applog_configValue); break; } } } // 分享参数配置 if (packageName == "com.example.gcloudu3ddemo.aligames") { string mars_share_sdk = File.ReadAllText(file_name + "/assets/mars_share_sdk.xml"); // 微信分享 mars_share_sdk = mars_share_sdk.Replace("wx8cc3dd18515604d6", "wx3b5d79381953b954"); mars_share_sdk = mars_share_sdk.Replace("66782839cb8e3569a461e20964b53338", "8dbef4da7f345eaf3f51e8f46eb13847"); File.WriteAllText(file_name + "/assets/mars_share_sdk.xml", mars_share_sdk); } File.WriteAllText(file_name + "/AndroidManifest.xml", androidManifest); } { //step rewrite yml string yml = ""; var x = File.ReadAllLines(file_name + "/apktool.yml"); foreach (var p in x) { if (p.StartsWith("- assets/AssetBundle")) { } else { yml += (p + "\n"); } } File.WriteAllText(file_name + "/apktool.yml", yml); } //setp encode //foreach (var pp in dll_list) //{ // string p = file_name + "/" + pp; // var data = File.ReadAllBytes(p); // File.Delete(p); // File.WriteAllBytes(p, Xxtea.XXTEA.Encrypt(data, ABC)); //} //if (GetLastIsBuildEngineStandlone()) //{ // if (!Directory.Exists("AssetBundles")) // { // Directory.CreateDirectory("AssetBundles"); // } // if (Directory.Exists("AssetBundles/engine")) // { // Directory.Delete("AssetBundles/engine", true); // } // Directory.CreateDirectory("AssetBundles/engine"); // //last is build engine // for (int i = 0; i < dll_list.Length; i++) // { // try // { // File.Delete(engne_list[i]); // } // catch (Exception e) // { // } // File.Copy(file_name + "/" + dll_list[i], engne_list[i]); // } // SetLastIsBuildEngineStandlone(false); // try // { // Directory.Delete(file_name, true); // } // catch (Exception e) { } // return; //} string unsign_apk_path = apkRoot + "/" + file_name + "_unsigned.apk"; //setp build apk { System.Diagnostics.Process proc = System.Diagnostics.Process.Start(Application.dataPath + "/../RuntimeBuild/tools/apktool.bat", "b " + file_name + " -o " + unsign_apk_path); proc.WaitForExit(); try { Directory.Delete(file_name, true); } catch (Exception e) { } } // sign & zliap { try { string sign_apk_path = apkRoot + "/" + file_name + ".apk"; if (File.Exists(unsign_apk_path)) { Process proc = Process.Start(Application.dataPath + "/../RuntimeBuild/tools/jarsigner.bat", unsign_apk_path + " " + sign_apk_path); proc.WaitForExit(); } } catch (Exception e) { Debug.LogError(e.StackTrace); } } // del unsigned { if (File.Exists(unsign_apk_path)) { File.Delete(unsign_apk_path); } } } }