英文:
How to Obfuscate a fatJar with Klassmaster
问题
build.gradle:
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath files("E:\\softs\\ZKM\\ZKMEval\\ZKM.jar") // 必须将ZKM_JAR_PATH设置为指向您的ZKM.jar
classpath 'com.zelix.gradle:plugin:1.0.0'
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'com.zelix.gradle.plugin'
group = 'com.github.jitpack'
sourceCompatibility = 1.8 // 使用Java 8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
}
jar {
manifest {
attributes "Main-Class": "com.github.jitpack.Hello"
}
zip64 = true
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
zkmSetting {
scriptName = "Obfuscate.txt" // 必须设置为指向要执行的ZKM脚本的位置。
}
Obfuscate.txt:
print "Obfuscating fatJar.....";
classpath
"C:\\Program Files\\Java\\jdk-10.0.2\\lib\\jrt-fs.jar"
".\\obfuscateFatJar.jar";
open ".\\obfuscateFatJar.jar" {"*.class"};
exclude org.apache.commons.*.*;
exclude com.github.jitpack.Hello.*;
obfuscate keepInnerClassInfo=false
keepGenericsInfo=true
exceptionObfuscation=heavy
encryptStringLiterals=flowObfuscate;
saveAll archiveCompression=asIs
deleteEmptyDirectories=true
deleteXMLComments=false
"ObfusactedTest.jar";
此外,Hello.java 包含主方法。
英文:
I have a fat jar which is generated by using gradle script. Post the gradle script when I run the following command :-
> java -jar fileName.jar
it is running the main method and things are fine. Nevertheless when I try to obfuscate this jar, the resulting jar is complaining that :-
> Error: Invalid or corrupt jarfile ObfusactedTest.jar
My code is as follows:-
build.gradle:-
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath files("E:\\softs\\ZKM\\ZKMEval\\ZKM.jar") //ZKM_JAR_PATH must be set to point to your ZKM.jar
classpath 'com.zelix.gradle:plugin:1.0.0'
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'com.zelix.gradle.plugin'
group = 'com.github.jitpack'
sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
}
jar {
manifest {
attributes "Main-Class": "com.github.jitpack.Hello"
}
zip64 = true
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
zkmSetting {
scriptName = "Obfuscate.txt" //Must be set to point to the ZKM Script to execute.
}
Obfuscate.txt:-
print "Obfuscating fatJar.....";
classpath
"C:\Program Files\Java\jdk-10.0.2\lib\jrt-fs.jar"
".\obfuscateFatJar.jar";
open ".\obfuscateFatJar.jar" {"*.class"};
exclude org.apache.commons.*.*;
exclude com.github.jitpack.Hello.*;
obfuscate keepInnerClassInfo=false
keepGenericsInfo=true
exceptionObfuscation=heavy
encryptStringLiterals=flowObfuscate;
saveAll archiveCompression=asIs
deleteEmptyDirectories=true
deleteXMLComments=false
"ObfusactedTest.jar";
By the way Hello.java has got the main method.
答案1
得分: 0
你的ZKM脚本中的“open”语句指定了{“*.class”}文件过滤器。因此,您正在过滤掉所有非class文件,包括您的MANIFEST.MF。请参阅https://www.zelix.com/klassmaster/docs/openStatement.html#filter。
缺少MANIFEST.MF将导致“无效或损坏的jar文件”错误。请注意,您的Zelix KlassMaster日志文件将包含如下消息。
MESSAGE:过滤路径'obfuscateFatJar.jar!META-INF/MANIFEST.MF',因为它不匹配指定的过滤器'{“.class”}'(D)*
您可以通过不使用文件过滤器(在这种情况下是最安全的选项)或扩展文件过滤器以包括其他文件类型来解决此问题。例如{“.class” || “.MF”}。
英文:
Your ZKM Script "open" statement specifies the {"*.class"} file filter. So you are filtering out ALL non-class files including your MANIFEST.MF. See https://www.zelix.com/klassmaster/docs/openStatement.html#filter.
A missing MANIFEST.MF will give you a "Invalid or corrupt jarfile" error. Note that your Zelix KlassMaster log file will contain messages like the following.
MESSAGE: Filtering out path 'obfuscateFatJar.jar!META-INF/MANIFEST.MF' because it does not match specified filter '{".class"}>' (D)*
You can work around this by not using a file filter (the safest option in this case) or by broadening your file filter to include other file types. E.g. {".class" || ".MF"}
答案2
得分: 0
更新类路径中的过滤器。代码现在如下所示。运行得很好。
execute "del ObfusactedTest.jar";
classpath
"C:\Program Files\Java\jdk-10.0.2\lib\jrt-fs.jar"
".\obfuscateFatJar.jar";
open ".\obfuscateFatJar.jar" {"*.class" || "*.MF"};
exclude org.apache.commons.*.*;
obfuscate keepInnerClassInfo=false
keepGenericsInfo=true
exceptionObfuscation=heavy
encryptStringLiterals=flowObfuscate;
saveAll archiveCompression=asIs
deleteEmptyDirectories=true
deleteXMLComments=false
"ObfusactedTest.jar";
英文:
Update the filter in the class path. The code looks like this now. Works like a charm.
execute "del ObfusactedTest.jar";
classpath
"C:\Program Files\Java\jdk-10.0.2\lib\jrt-fs.jar"
".\obfuscateFatJar.jar";
open ".\obfuscateFatJar.jar" {"*.class" || "*.MF"};
exclude org.apache.commons.*.*;
obfuscate keepInnerClassInfo=false
keepGenericsInfo=true
exceptionObfuscation=heavy
encryptStringLiterals=flowObfuscate;
saveAll archiveCompression=asIs
deleteEmptyDirectories=true
deleteXMLComments=false
"ObfusactedTest.jar";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论