jabal
jabal

Reputation: 12337

Gradle ShadowJar output contains obfuscated and non-obfuscated classes

in my simple gradle build I would like to use ShadowJar and ProGuard together. I have found examples where the output of the shadowJar task is the input of the proguard one, which works fine, however in my case I would prefer first creating the small obfuscated jar first where I nicely specify the library dependencies and get proguard to focus only on my code and then I would like to pass that to the shadowjar plugin for fatjar packaging.

My setup is the following:

task obfuscate(type: proguard.gradle.ProGuardTask) {
    injars jar
    outjars "build/libs/foo-${project.version}-pg.jar"
    ...
}

shadowJar {
  from obfuscate
  configurations = [project.configurations.embed]
}

shadowJar.dependsOn obfuscate

And my problem is that the shadowJar output contains all the libraries unobfuscated (fine), my obfuscated code (fine) and my unobfuscated code. So somehow the original classes sneak in and I am not seeing how is that happening. I am not able to specify to shadowJar to package the dependencies and the proguard output jar together.

Do you see where is the problem in my approach?

Upvotes: 1

Views: 1659

Answers (1)

Phillip
Phillip

Reputation: 5656

Try this, works for me:

task shadowJar2( type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar ) {
    baseName = jar.baseName
    from obfuscate
    configurations = [project.configurations.embed]
    classifier = 'shadow'
    //version =
}

The problem is that the default shadowJar task takes your 'main' sourceset in addition to the obfuscated + library jars. By defining your own custom 'shadowJar2' task, you are explicitly defining your sources, which in this case is only jars ('obfuscate' + 'embed') and not a sourceset.

Upvotes: 0

Related Questions