预计阅读时间: 2 分钟

If you have some restrictions on your JAR size (for example when deploying a free application to heroku), you can use proguard to shrink it. If you are using gradle, it is pretty straightforward to use the proguard-gradle plugin. You only have to remember to keep: your main module method, the EngineMain class, and the Kotlin reflect classes. You can then fine-tune it as required:

buildscript {
    ext.proguard_version = '6.0.1'
    dependencies {
        classpath "net.sf.proguard:proguard-gradle:$proguard_version"

task minimizedJar(type: proguard.gradle.ProGuardTask, dependsOn: shadowJar) {
    injars "build/libs/my-application.jar"
    outjars "build/libs/my-application.min.jar"
    libraryjars'java.home' + "/lib/rt.jar"
    printmapping "build/libs/"

    def keepClasses = [
            'io.ktor.server.netty.EngineMain', // The EngineMain you use, netty in this case.
            'io.ktor.samples.hello.HelloApplicationKt', // The class containing your module defined in the application.conf

    for (keepClass in keepClasses) {
        keep access: 'public', name: keepClass, {
            method access: 'public'
            method access: 'private'


You have a full example on: