private void UnPack_Logic() { OutPut("【I】"); if (Apktool.isApkFile(file.Text)) // 解包 { OutPut("【I】apk解包开始..."); String result = Apktool.unPackage(file.Text, OutPut, false, false); // 使用apktool进行apk的解包 if (result.Contains("【E】")) { return; } OutPut("【I】apk解包结束!\r\n"); } else if (Apktool.isApkDir(file.Text)) // 打包 { OutPut("【I】apk打包开始..."); String result = Apktool.package(file.Text, OutPut); // 使用apktool进行打包 if (result.Contains("【E】")) { return; } OutPut("【I】apk未签名文件已生成!\r\n"); // 若有签名文件,则进行签名 if (!comboBox_sign.Text.Equals("")) { OutPut("【I】apk签名中..."); String apkName = file.Text + "..apk"; String pem = SinPath() + "\\" + comboBox_sign.Text + ".x509.pem"; String pk8 = SinPath() + "\\" + comboBox_sign.Text + ".pk8"; String psw = "letang123"; result = Apktool.Sign(apkName, pem, pk8, psw, OutPut); if (result.Contains("【E】")) { return; } OutPut("【I】apk签名、对齐 完成!\r\n"); // 删除打包生成的未签名文件 if (System.IO.File.Exists(apkName)) { System.IO.File.Delete(apkName); } } OutPut("【I】apk打包结束!\r\n"); } else { Cmd.Run(file.Text, OutPut); // 执行cmd命令 } }
//从dirTarget目录下获取res资源,重新编译生成public.xml private static bool UpdatePublicXML(String dirTarget, Cmd.Callback call) { ToolSetting settting = ToolSetting.Instance(); // 载入设置信息 if (call != null) { call("【I】- 2.解包Empty.apk"); } String emptyApk = DependentFiles.curDir() + "\\tools\\Empty.apk"; // 空项目资源路径 if (emptyApk.Contains("\\\\")) { emptyApk = emptyApk.Replace("\\\\", "\\"); } String emptyDir = Apktool.unPackage(emptyApk, null, false); // 解包空apk if (emptyDir.Contains("【E】") && call != null) { call("【E】 解包Empty.apk异常"); return(false); } if (call != null) { call("【I】- 复制游戏res资源"); } String Res = emptyDir + "\\res"; ApkCombine.CopyFolderTo(dirTarget + "\\res", Res, true); // 复制Target目录到res目录,到空工程解包路径下 Program.Delay(3000); // 部分机器复制文件,存在异步延时,确保文件复制完成 if (call != null) { call("【I】- 3.使用新的res资源,生成新的Empty.apk"); } String apkFile = Apktool.package(emptyDir, null); // 使用apktool进行打包 if (apkFile.Contains("【E】") && call != null) { call("【E】 打包Empty.apk异常"); call("【E】 异常信息:" + apkFile); return(false); } if (call != null) { call("【I】- 4.解包Empty.apk"); } string unpackDir = Apktool.unPackage(apkFile, null, false); // 使用apktool进行apk的解包,生成新的public.xml if (unpackDir.Contains("【E】") || unpackDir.Trim().Equals("")) { if (call != null) { call("【E】 解包Empty.apk异常"); } return(false); } if (call != null) { call("【I】- 5.复制生成的public.xml文件,到游戏res目录中"); } String relativePath = @"\res\values\public.xml"; File.Copy(unpackDir + relativePath, dirTarget + relativePath, true); // 替换原有public.xml relativePath = @"\res\drawable\empty_ic_launcher.png"; File.Copy(unpackDir + relativePath, dirTarget + relativePath, true); relativePath = @"\res\layout\empty_activity_main.xml"; File.Copy(unpackDir + relativePath, dirTarget + relativePath, true); if (call != null) { call("【I】- 清除Empty.apk相关缓存资源..."); } // 清除缓存资源 Directory.Delete(emptyDir, true); // 删除空项目解包文件 File.Delete(apkFile); // 删除生成的临时文件 Directory.Delete(unpackDir, true); // 删除空工程解包目录 return(true); }
/// <summary> /// 执行apk打包逻辑 /// </summary> public static String apkPackage(String apkUnPackageDir, String signFileName, Cmd.Callback call) { // 重新打包apk,并签名 if (call != null) { call("【I】apk打包开始..."); } String result = Apktool.package(apkUnPackageDir, call); // 使用apktool进行打包 if (result.Contains("【E】")) { return(result); } if (call != null) { call("【I】apk未签名文件已生成!\r\n"); } // 若有签名文件,则进行签名 if (!signFileName.Equals("")) { if (call != null) { call("【I】apk签名中..."); } String apkName = apkUnPackageDir + "..apk"; if (signFileName.EndsWith(".keystore")) { String keysotreName = Form1.SinPath() + "\\" + signFileName; String[] I = getKestoreInfo(signFileName); // 获取对应的签名信息 String alias = I[0]; String password = I[1]; result = Apktool.SignKeyStore(apkName, keysotreName, alias, password, call); } else { String pem = Form1.SinPath() + "\\" + signFileName + ".x509.pem"; String pk8 = Form1.SinPath() + "\\" + signFileName + ".pk8"; String psw = "letang123"; result = Apktool.Sign(apkName, pem, pk8, psw, call); } if (result.Contains("【E】")) { return(result); } if (call != null) { call("【I】apk签名、对齐 完成!\r\n"); } // 删除打包生成的未签名文件 if (File.Exists(apkName)) { File.Delete(apkName); } } if (call != null) { call("【I】apk打包结束!\r\n"); } return(""); }