Hassan Syyid Hassan Syyid - 1 day ago 4
Java Question

ProGuard Obufscation and Shadow Dependencies

I am trying to use ProGuard to obfuscate my code, and then using the outputted code, use Shadow to add the needed dependencies into the JAR.

Currently, I am simply getting a

-obf
JAR and a
-all
JAR. What I need is a
-all
JAR with obfuscated code.

import proguard.gradle.ProGuardTask

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'

buildscript {
repositories {
mavenCentral()
jcenter()
}

dependencies {
classpath group: 'net.sf.proguard', name: 'proguard-gradle', version: '5.3.1'
classpath group: 'com.github.jengelman.gradle.plugins', name: 'shadow', version: '1.2.4'
}
}

repositories {
mavenCentral()
jcenter()
}

jar {
manifest { attributes("Main-Class": "com.skcraft.launcher.Launcher") }
}

dependencies {
compile 'org.projectlombok:lombok:1.12.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.0'
compile 'commons-lang:commons-lang:2.6'
compile 'commons-io:commons-io:1.2'
compile 'com.google.guava:guava:15.0'
compile 'com.beust:jcommander:1.32'
compile 'com.miglayout:miglayout:3.7.4'
compile 'com.google.code.findbugs:jsr305:3.0.0'
compile files ('libs/javax-crypto.jar')
}

processResources {
filesMatching('**/*.properties') {
filter {
it.replace('${project.version}', project.version)
}
}
}

task obfuscate(type: proguard.gradle.ProGuardTask) {
configuration '../proguard.txt'

injars jar
outjars "${buildDir}/libs/launcher-${version}-obf.jar"

libraryjars files(configurations.compile.collect())
}

shadowJar {
dependencies {
exclude(dependency('org.projectlombok:lombok'))
}
}

build.dependsOn(obfuscate)
build.dependsOn(shadowJar)

task wrapper(type: Wrapper) { gradleVersion = "3.1" }


Any ideas? One idea I did come up with using the
shadowJar
and then obfuscate that, however I need to configure my
proguard.txt
to only obfuscate my code. I attempted to do this below, but it still tried to obfuscate all the packages, throwing errors:

# Include java runtime classes
-libraryjars <java.home>/lib/rt.jar

# Output a source map file
-printmapping proguard.map

# Keep filenames and line numbers
-keepattributes SourceFile, LineNumberTable

# Disable certain proguard optimizations which remove stackframes (same as Android defaults)
-optimizations !method/inlining/*

-keep public class * {
public protected *;
}

-keepclassmembernames class * {
java.lang.Class class$(java.lang.String);
java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames class * {
native <methods>;
}

-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep class !com.skcraft.** { *; }


Thanks in advance

Answer

I figured it out, figured I'd post the solution here.

Using the Shadow task, I was able to specify the file I wanted it to read from:

shadowJar {
 from file("${buildDir}/libs/launcher-${version}-obf.jar")
 dependencies {
   exclude(dependency('org.projectlombok:lombok'))
 }
}

Thanks!

Comments