I want to obfuscate Java code in Gradle using yGuard. So far, I have managed to get the obfuscation working using:
dependencies {
compile 'com.yworks:yguard:2.9.2'
}
task yGuardObfuscate {
group 'yGuard'
description 'Obfuscates existing archives.'
dependsOn "installDist"
doLast {
ant.taskdef(
name: 'yguard',
classname: 'com.yworks.yguard.YGuardTask',
classpath: sourceSets.main.runtimeClasspath.asPath
)
ant.yguard {
inoutpairs {
fileset(dir: "./build/install/module") {
include(name: "module*.jar")
exclude(name: "*_obf.jar")
}
}
rename(logFile: "./build/install/module/rename.log") {
property(name: "naming-scheme", value: "mix")
}
}
}
}
However, that has the problem that the main
function gets obscured, and I can't run it anymore using java -jar module_obf.jar
(which works with the unobfuscated jar).
I have tried several ways of excluding the main
function from obfuscation.
First, I tried using the @Obfuscate annotation as described in the documentation (https://yworks.github.io/yGuard/task_documentation/#controlling-obfuscation-exclusion-with-annotations), like this:
@com.yworks.util.annotation.Obfuscation( exclude = true, applyToMembers = false)
public static void main(String[] args) {
try {
new Start();
} catch (SQLException e) {
LOGGER.error(e, e);
}
}
In my IDE (Eclipse 2019-14), this line was shown as valid. However, when trying to compile it with the gradle task I created, I got the following error:
...\Start.java:22: error: package com.yworks.util.annotation does not exist
@com.yworks.util.annotation.Obfuscation( exclude = true, applyToMembers = false)
^
I tried a number of things to get this to work, but could not arrive at a working solution.
So next, I tried to use a keep > method element in the yGuardObfuscate
task to explicitly keep the main
function of the Start
class, like this:
task yGuardObfuscate {
group 'yGuard'
description 'Obfuscates existing archives.'
dependsOn "installDist"
doLast {
ant.taskdef(
name: 'yguard',
classname: 'com.yworks.yguard.YGuardTask',
classpath: sourceSets.main.runtimeClasspath.asPath
)
ant.yguard {
inoutpairs {
fileset(dir: "./build/install/module") {
include(name: "module*.jar")
exclude(name: "*_obf.jar")
}
}
rename(logFile: "./build/install/module/rename.log") {
property(name: "naming-scheme", value: "mix")
keep {
method("class": "com.company.project.module.Start", name: "public static void main(String[] args)")
}
}
}
}
}
This caused the Gradle build to fail with the following exception:
: java.lang.IllegalArgumentException: '(' expected but found void
[...]
Caused by: java.lang.IllegalArgumentException: '(' expected but found void
at com.yworks.yguard.ObfuscatorTask.toNativeMethod(ObfuscatorTask.java:188)
at com.yworks.yguard.ant.MethodSection.addEntries(MethodSection.java:35)
at com.yworks.yguard.ant.ExposeSection.createEntries(ExposeSection.java:170)
at com.yworks.yguard.ObfuscatorTask.execute(ObfuscatorTask.java:745)
at com.yworks.yguard.YGuardTask.execute(YGuardTask.java:116)
[...]
Root cause: java.lang.IllegalArgumentException: '(' expected but found void
at com.yworks.yguard.ObfuscatorTask.toNativeMethod(ObfuscatorTask.java:188)
at com.yworks.yguard.ant.MethodSection.addEntries(MethodSection.java:35)
at com.yworks.yguard.ant.ExposeSection.createEntries(ExposeSection.java:170)
at com.yworks.yguard.ObfuscatorTask.execute(ObfuscatorTask.java:745)
at com.yworks.yguard.YGuardTask.execute(YGuardTask.java:116)
Again, I tried several things here, such as writing "class" without the "", adding extra {}, but nothing helped.
So the question here is: What am I doing wrong? And how can I prevent yGuard from obfuscating the main
function?