I am building a library (JAR) in kotlin, which should be obfuscated. There is (for the moment) one class and one method which should be able to be called from outside.
To obfuscate i would like to use yGuard and gradle (kotlin)
My build.gradle.kts yGuard configuration looks like:
...
configurations {
create("yguard")
}
...
dependencies {
...
"yguard"("com.yworks:yguard:4.1.1") // obfuscation
...
}
tasks.register("obfuscate") {
dependsOn(tasks.jar)
group = "yGuard"
description = "Obfuscates the java archive."
doLast {
val archivePath = tasks.jar.get().archiveFile.get().asFile.path
val unobfJar = archivePath.replace(".jar", "_unobf.jar")
ant.withGroovyBuilder {
"move"("file" to archivePath, "tofile" to unobfJar, "verbose" to true)
"taskdef"(
"name" to "yguard",
"classname" to "com.yworks.yguard.YGuardTask",
"classpath" to configurations["yguard"].asPath
)
"yguard" {
"inoutpair"("in" to unobfJar, "out" to archivePath)
// Prevent yGuard from removing "Deprecated" attributes from .class files.
"attribute"("name" to "Deprecated")
"rename"("logfile" to "${projectDir}/build/${rootProject.name}_renamelog.xml") {
// Obfuscation settings
"keep" {
"method"(
"name" to "boolean doProcess()",
"class" to "my.x.y.ProcessUtil"
)
}
}
// "shrink"("logfile" to "${projectDir}/build/${rootProject.name}_shrinklog.xml") {
// }
}
}
}
The class:
package my.x.y
class ProcessUtil(
private val param1: Object,
private val param2: Object,
) {
fun doProcess(
private val paramX: Object
) {
...
}
}
This configuration runs successful, but everything is obfuscated to package names like A.A.A and so on.
I am stuck on how to define the class and method in the "rename" and/or "shrink" section to obfuscate all of the code except the one class and method.
Unfortunately can't get the understanding out of the documentation of yGuard. Any hints and help are appreciated!
I found a solution how to configure yGuard to exclude the one method in a class:
...
"rename"("logfile" to "${projectDir}/build/${rootProject.name}_renamelog.xml") {
"keep" {
"class"(
"name" to "my.x.y.ProcessUtil",
)
"method"(
"name" to "boolean doProcess()",
"class" to "my.x.y.ProcessUtil"
)
}
}
...