Created
March 30, 2020 14:04
-
-
Save FireZenk/2ce0c22e12e15e9744b173f154fef3ec to your computer and use it in GitHub Desktop.
Obfuscate jar from java-library gradle projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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