Skip to content

Instantly share code, notes, and snippets.

@FireZenk
Created March 30, 2020 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FireZenk/2ce0c22e12e15e9744b173f154fef3ec to your computer and use it in GitHub Desktop.
Save FireZenk/2ce0c22e12e15e9744b173f154fef3ec to your computer and use it in GitHub Desktop.
Obfuscate jar from java-library gradle projects
import proguard.gradle.ProGuardTask
import java.nio.file.Paths
dependencies {
[...]
// Required by @task::obfuscate
compileOnly 'org.jetbrains:annotations:13.0'
}
def jarNameWithoutExtension = jar.archiveName.with { it.take(it.lastIndexOf(".")) }
def obfuscatedJarName = "${jarNameWithoutExtension}-release.jar"
def jarFileLocation = jar.archivePath.parent
def obfuscatedFilePath = Paths.get(jarFileLocation, obfuscatedJarName)
task obfuscate(type: ProGuardTask) {
configuration 'proguard-rules.pro' // Use the standard proguard file from gradle projects
injars jar.archivePath
outjars obfuscatedFilePath.toString()
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
libraryjars configurations.findByName('runtimeClasspath').getFiles() // Add all libraries from dependencies
doLast {
jar.archivePath.renameTo(Paths.get(jarFileLocation, "$jarNameWithoutExtension-original.jar").toFile())
obfuscatedFilePath.toFile().renameTo(jar.archivePath) // Rename obfuscated jar to the original name
}
}
jar.finalizedBy(project.tasks.obfuscate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment