How do I set up Proguard with Kotlin JVM (not android) and Gradle

I posted this question regarding Proguard with Kotlin JVM and Gradle, but didn’t get an answer, I really want to use Proguard to minify and obfuscate some java plugins I’m making. Here is the Stack Overflow link of my question: How do I set up Proguard with Kotlin JVM (not android) and Gradle - Stack Overflow

I already tried to set up Proguard but I got stuck in a lot of errors regarding missing Kotlin libraries, and have not found a single guide (that is for jvm, not android) teaching how to use Proguard with Kotlin, so as my last resource I came here to ask for help, a guide, anything, thanks. I also would love to know how do I set up custom dictionaries for Proguard (on Gradle).

Hi @secretx,

Welcome to the ProGuard community.

Did you already find your way to ProGuard’s manual? You can find the documentation here: ProGuard manual | Introduction | Guardsquare

Some interesting pages for you to start with;

Regarding the custom dictionary;
On he configuration page you will see the option -obfuscationdictionary explained, which can be used to set up a custom dictionary.

Regarding the Kotlin errors;
Could you please provide the full stack trace? Do you see the error/warning during the build or at runtime? The troubleshooting guide might be able to help you tackle the issue.

Have you already found the ProGuard Playground? Here you can visualise the ProGuard configuration on your class tree without the need of building the project every single time.

Best regards,

Jonas

2 Likes

The documentation only includes guides to Gradle in Android, there is not a single mention of desktop Java in it (which is surprising), not a single Gradle buildscript usage in anything that is not Android.

The error is the following:

Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not find com.android.tools.build:gradle:3.0.0.
Searched in the following locations:
  - https://jcenter.bintray.com/com/android/tools/build/gradle/3.0.0/gradle-3.0.0.pom
  - https://repo.maven.apache.org/maven2/com/android/tools/build/gradle/3.0.0/gradle-3.0.0.pom
  - https://plugins.gradle.org/m2/com/android/tools/build/gradle/3.0.0/gradle-3.0.0.pom
Required by:
    project : > com.guardsquare:proguard-gradle:7.0.1

Buildscript (KTS)

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath("com.guardsquare:proguard-gradle:7.0.1")
    }
}

I cannot even set up the task if I cannot Proguard Gradle imported, I would really appreciate a solution to this missing android-tools dependency. I’m not in android, why is Proguard requiring it?

Hi @secretx!

This is a known issue in ProGuard 7.0.x Use ProGuard 7 without hard dependency on Android · Issue #66 · Guardsquare/proguard · GitHub

You can workaround this by excluding the android tools dependency:

classpath('com.guardsquare:proguard-gradle:7.0.0') {
    exclude group: 'com.android.tools.build'
}

This is fixed in 7.1.0-beta2 but there is a potential optimization issue in beta2 at the moment: 7.1.0-beta2 breaks the obfuscated application · Issue #141 · Guardsquare/proguard · GitHub

Regarding the lack of Java app documentation, we are aware of this and plan to improve this!

Thanks, that solved the issue, now my proguard runs, but I got another issue: My proguard task runs, and more often than not the proguard.jar is not generated, it reports in Gradle > Task :proguard UP-TO-DATE even when there’s no proguard.jar file. I tried to keep running the task to see if that would solve the issue, tried to delete the built shaded jar file, but no, didn’t work out. Sometimes clearing the cache or changing the Proguard version works, but it’s a guessing game, some times it generate the jar file and other times it don’t do anything and just report “up-to-date”.

gradle.build.kts

dependencies {
    classpath("com.guardsquare:proguard-gradle:7.0.1")/* {
        exclude("com.android.tools.build")
    }*/
}

tasks.register<proguard.gradle.ProGuardTask>("proguard") {
    configuration("proguard-rules.pro")
}

tasks.build.get().finalizedBy(tasks.getByName("proguard"))

proguard-rules.pro

-injars  build/libs/MagicWands.jar
-outjars build/libs/proguard.jar

-libraryjar "C:\Program Files\Java\jdk1.8.0_281\jre\lib\rt.jar"
# morestuff below

I’m using Kotlin version 1.4.32 with Gradle 6.8.3 and gradle.build.kts, I don’t know if that’s helpful or not.

Hi @secretx ! There have been some recent improvements to the ProGuardTask - could you try compiling the latest ProGuard from source? You can build it by following the instructions here and installing it into a maven local repo to use it: GitHub - Guardsquare/proguard: ProGuard, Java optimizer and obfuscator