How do you use Proguard with Kotlin? (Not Android)


#1

As title, there are many examples about using Proguard with Android. However, there is nearly no example for a Kotlin Gradle project. When I trying to include Proguard, it creates many unresolved references.

Here is my build.gradle:

import proguard.gradle.ProGuardTask

group "com.example"
version "1.0.0"

buildscript {
    ext.kotlin_version = "1.1.3-2"

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "net.sf.proguard:proguard-gradle:5.3.3"
    }
}

apply plugin: "kotlin"

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    testCompile "org.mockito:mockito-core:2.8.47"
    testCompile "junit:junit:4.12"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

jar {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

task proguard(type: ProGuardTask, dependsOn: "jar") {
    injars jar.archivePath
    injars configurations.compile
    outjars "proguard.jar"
    keep 'public class com.example { *; }'
}