贴一段自动编译java,并混淆编译的代码
刚写的一个自动编译、混淆、打包jar的代码,做个记录
用到的NuGet:
1 2 3 4 5 | <?xml version= "1.0" encoding= "utf-8" ?> <packages> <package id= "DotNetZip" version= "1.10.1" targetFramework= "net45" /> <package id= "Newtonsoft.Json" version= "10.0.2" targetFramework= "net45" /> </packages> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | using Ionic.Zip; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ApiCloudModulePackage { class Program { static string tempPath; static ToolConfig toolConfig; static void Main( string [] args) { tempPath = Path.GetTempPath() + Guid.NewGuid().ToString( "N" ) + "\\" ; if (Directory.Exists(tempPath) == false ) { Directory.CreateDirectory(tempPath); } toolConfig = Newtonsoft.Json.JsonConvert.DeserializeObject<ToolConfig>( File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "projects.json" ) ); try { foreach (ProjectConfig project in toolConfig.projects) { //compile jar jarProject(project); } Console.Write( "需要现在生成模块的zip文件吗?[y/n]:" ); while ( true ) { var keyResult = Console.ReadKey(); if (keyResult.Key == ConsoleKey.Y) { Console.WriteLine( "\r\n" ); //make zip file from module folder foreach ( var moduleFolder in toolConfig.modules) { makeZip(getPathFromConfig(moduleFolder)); Console.WriteLine( "打包输出:" + moduleFolder + ".zip" ); } break ; } else if (keyResult.Key == ConsoleKey.N) { Process.GetCurrentProcess().Kill(); } } Console.WriteLine( "打包完成! 按任意键退出" ); } catch (Exception ex) { Console.WriteLine(ex.Message); } try { Directory.Delete(tempPath, true ); } catch { } Console.ReadKey(); } static void makeZip( string folder) { string zipfilepath = Path.GetDirectoryName(folder) + "\\" + Path.GetFileName(folder) + ".zip" ; if (File.Exists(zipfilepath)) File.Delete(zipfilepath); using (ZipFile zip = new ZipFile(zipfilepath)) { string root = Path.GetFileName(folder); zip.AddDirectoryByName(root); zip.AddDirectory(folder , root); zip.Save(); } } static void jarProject(ProjectConfig project) { Console.WriteLine( "正在编译" + project.sourceFolder); string srcPath = getPathFromConfig(project.sourceFolder); //create temp folder string tempFolder = tempPath + Guid.NewGuid().ToString( "N" ) + "\\" ; string classTempFolder = tempFolder + Guid.NewGuid().ToString( "N" ) + "\\" ; string jarDstFolder = tempFolder + "jar\\" ; string proguardFolder = tempFolder + "jar\\" ; if (Directory.Exists(jarDstFolder) == false ) { Directory.CreateDirectory(jarDstFolder); } if (Directory.Exists(classTempFolder) == false ) { Directory.CreateDirectory(classTempFolder); } List< string > javaFiles = new List< string >(); getFiles(javaFiles, srcPath , "*.java" ); //copy java files to temp folder string encoding = null ; foreach ( string javafile in javaFiles ) { if (encoding == null ) { var encode = GetFileEncodeType(javafile); if (encode == System.Text.Encoding.UTF8) { encoding = "utf-8" ; } else { encoding = "GBK" ; } } File.Copy(javafile, tempFolder + Path.GetFileName(javafile) , true ); } StringBuilder libJars = new StringBuilder(); StringBuilder proguardJars = new StringBuilder(); if (! string .IsNullOrEmpty(project.libPath)) { List< string > jarList = new List< string >(); Program.getFiles(jarList, Program.getPathFromConfig(project.libPath), "*.jar" ); if (jarList.Count > 0) { libJars.Append( " -cp " ); foreach ( string jarfile in jarList) { libJars.Append( '"' ); libJars.Append(jarfile); libJars.Append( '"' ); libJars.Append( ';' ); proguardJars.Append( " -libraryjars " ); proguardJars.Append( '"' ); proguardJars.Append(jarfile); proguardJars.Append( '"' ); } } } ProcessStartInfo proStartInfo = new ProcessStartInfo( "javac" ); // -source 1.6 -target 1.6 表示按照jdk1.6标准编译,默认是1.8,太高了,eclipse使用时会出错,因为eclipse项目里面设置的是1.6 proStartInfo.Arguments = $ "-Xlint:unchecked -Xlint:deprecation -source 1.6 -target 1.6 -encoding {encoding} -bootclasspath {AppDomain.CurrentDomain.BaseDirectory}android.jar {libJars} -d {classTempFolder} {tempFolder}*.java" ; proStartInfo.UseShellExecute = false ; Process process = Process.Start(proStartInfo); process.WaitForExit(); if ( true ) { //build jar, jar file actually is zip file using (ZipFile zip = new ZipFile($ "{jarDstFolder}result.jar" )) { zip.AddDirectory(classTempFolder); var content = @"Manifest-Version: 1.0 Created-By: 1.8.0_40 (Oracle Corporation) " ; byte [] bs = System.Text.Encoding.UTF8.GetBytes(content); zip.AddEntry( "META-INF/MANIFEST.MF" , bs); zip.Save(); } } else { ////这里是利用jar程序生成jar文件 //List<string> classFiles = new List<string>(); //getFiles(classFiles, tempFolder, "*.class"); //foreach (string classfile in classFiles) //{ // File.Copy(classfile, jarDstFolder + Path.GetFileName(classfile), true); //} //proStartInfo = new ProcessStartInfo("jar"); //proStartInfo.Arguments = $"cvf {jarDstFolder}result.jar {jarDstFolder}*.class"; //proStartInfo.UseShellExecute = false; //process = Process.Start(proStartInfo); //process.WaitForExit(); } if (! string .IsNullOrEmpty(project.proguardConfigFile)) { Console.WriteLine( "正在混淆代码..." ); process = Process.Start( new ProcessStartInfo( "java" ) { Arguments = $ "-jar proguard.jar @{project.proguardConfigFile} -injars {proguardFolder}result.jar -outjars {proguardFolder}result2.jar -libraryjars android.jar {proguardJars}" , UseShellExecute = false }); process.WaitForExit(); if (!File.Exists( string .Format( "{0}result2.jar" , proguardFolder))) { throw new Exception(project.sourceFolder + "代码混淆出错" ); } File.Delete( $ "{proguardFolder}result.jar" ); File.Move($ "{proguardFolder}result2.jar" , $ "{proguardFolder}result.jar" ); } if (File.Exists($ "{jarDstFolder}result.jar" ) == false ) throw new Exception( "编译" + project.sourceFolder + "失败!" ); //copy jar to destination foreach ( var dstpath in project.buildDestinations ) { string destination = getPathFromConfig(dstpath); File.Copy($ "{jarDstFolder}result.jar" , destination, true ); Console.WriteLine($ "Maked {destination}" ); } } static System.Text.Encoding GetFileEncodeType( string filename) { using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { System.IO.BinaryReader br = new System.IO.BinaryReader(fs); Byte[] buffer = br.ReadBytes(2); if (buffer[0] >= 0xEF) { if (buffer[0] == 0xEF && buffer[1] == 0xBB) { return System.Text.Encoding.UTF8; } else if (buffer[0] == 0xFE && buffer[1] == 0xFF) { return System.Text.Encoding.BigEndianUnicode; } else if (buffer[0] == 0xFF && buffer[1] == 0xFE) { return System.Text.Encoding.Unicode; } else { return System.Text.Encoding.Default; } } else { return System.Text.Encoding.Default; } } } static string getPathFromConfig( string path) { if (path[1] == ':' ) return path.Replace( "/" , "\\" ); else { return AppDomain.CurrentDomain.BaseDirectory + path.Replace( "/" , "\\" ); } } static void getFiles(List< string > result, string folder , string pattern) { var files = Directory.GetFiles(folder, pattern); result.AddRange(files); var dirs = Directory.GetDirectories(folder); foreach ( var dir in dirs ) { getFiles(result, dir , pattern); } } } } |
分类:
Android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)