Plugin obfuscation through a Gradle task

Like many other developers, I'm using the IntelliJ Gradle plugin to build my plugin. I'm absolutely new to obfuscation but looking over the documentation of Zelix KlassMaster, it doesn't seem that they have an existing Gradle integration (only a Java API).

My overall goal would be to have a Gradle task that is run automatically so that one has e.g. a  "buildPluginObfuscated" task  which provides the final jar/zip of my plugin. Is there anyone who is already using something similar and who can provide some feedback?

0
1 comment

Hi, 
I currently do it in gradle like this:
(only relevant parts)

def ideaHome = System.getenv('IDEA_BASE_DIR')
def ideaBinaries = "${ideaHome}/IC-191.6183.87"


repositories {
mavenCentral()
// plugin verifier
flatDir {
name 'jb_jre'
dirs "${ideaBinaries}/jre64/lib"
}
flatDir {
name 'zkm'
dirs "${zkmJarPath}"
}
}


configurations {
obfuscator
}

dependencies {
obfuscator ':rt'
obfuscator ':jce'
obfuscator ':ZKM'
// $buildDir doesn't work - use direct path
obfuscator files('build/classes/java/main')
obfuscator files('build/classes/kotlin/main')
obfuscator fileTree(dir: "${ideaBinaries}/lib", include: ['*.jar'])
}

task obfuscate(dependsOn: classes, type: JavaExec) {
main = 'ZKM'
classpath = configurations.obfuscator
args "$projectDir/zelix_obfuscator.txt"
}

task obfuscatedJar(dependsOn: jar, overwrite: true, type: Jar) {
dependsOn obfuscate
// output of ZKM - defined in ZKM config file
from "$buildDir/classes-obfuscated"
from ("$buildDir/classes/kotlin/main", { include('META-INF/**')})
// use generated plugin.xml
from sourceSets.main.resources.exclude('META-INF/plugin.xml')
from "${buildDir}/patchedPluginXmlFiles"
}

if (withObfuscate == "true") {
jar.enabled = false
} else {
obfuscatedJar.enabled = false
}

buildPlugin {
if (withObfuscate == "true") {
dependsOn obfuscatedJar
}
baseName "${rootProject.name}"
}

prepareSandbox {
pluginJar "${buildDir}/libs/${rootProject.name}-${fileversion}.jar"
}

patchPluginXml {
sinceBuild "191.6707.61"
untilBuild "192"
destinationDir "${buildDir}/patchedPluginXmlFiles/META-INF"
changeNotes """
"""
}

You need the IntelliJ jars, RT.jar and jce.jar and ZKM libs.
And you need to overwrite some default task else it won't run if you disable jar task - when using obfuscatedJar task.

1

Please sign in to leave a comment.