Zyoma Zyoma - 2 months ago 40
Scala Question

How to obfuscate fat Scala Jar with Proguard and SBT

I've managed to obfuscate my Scala code but I can't figure out how to run obfuscation against a jar file that is built by

sbt assembly
step. Or at least how to build a fat jar during the
proguard
step. I found that there is a
injar
argument for proguard but I have no clue how to apply it to
build.sbt
. Can someone please point me to how I can do this.

UPD Actually the JAR that is built by Proguard contains only obfuscated classes and not a single class from those that are ignored by Proguard.

Answer

To run a Proguard obfuscation against a jar file that was built by assembly task previously you should override inputs setting in build.sbt:

ProguardKeys.inputs in Proguard := Seq(baseDirectory.value / "target" / s"scala-${scalaVersion.value.dropRight(2)}" / s"${name.value}-assembly-${version.value}.jar")

This would specify your fat jar as a single input for Proguard. Also you probably want to modify the input libraries that are used for obfuscation. Here is an example of how to exclude all the libraries:

ProguardKeys.libraries in Proguard := Seq()

Finally disable input filter since you have only one source of classes:

ProguardKeys.inputFilter in Proguard := { file => None }

The setting above is required because Proguard will exclude META-INF/MANIFEST.MF file by default. Also merge step is no longer required since assembly task already performed this step:

ProguardKeys.merge in Proguard := false

And finally add assembly dependency to your Proguard task:

(ProguardKeys.proguard in Proguard) <<= (ProguardKeys.proguard in Proguard).dependsOn(assembly)