Proguard

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 theproguard-gradle plugin. You only have to remember to keep: your main module method, the EngineMainclass, and the Kotlin reflect classes. You can then fine-tune it as required:

build.gradle

  1. buildscript {
  2. ext.proguard_version = '6.0.1'
  3. dependencies {
  4. classpath "net.sf.proguard:proguard-gradle:$proguard_version"
  5. }
  6. }
  7. task minimizedJar(type: proguard.gradle.ProGuardTask, dependsOn: shadowJar) {
  8. injars "build/libs/my-application.jar"
  9. outjars "build/libs/my-application.min.jar"
  10. libraryjars System.properties.'java.home' + "/lib/rt.jar"
  11. printmapping "build/libs/my-application.map"
  12. ignorewarnings
  13. dontobfuscate
  14. dontoptimize
  15. dontwarn
  16. def keepClasses = [
  17. 'io.ktor.server.netty.EngineMain', // The EngineMain you use, netty in this case.
  18. 'kotlin.reflect.jvm.internal.**',
  19. 'io.ktor.samples.hello.HelloApplicationKt', // The class containing your module defined in the application.conf
  20. 'kotlin.text.RegexOption'
  21. ]
  22. for (keepClass in keepClasses) {
  23. keep access: 'public', name: keepClass, {
  24. method access: 'public'
  25. method access: 'private'
  26. }
  27. }
  28. }

You have a full example on: https://github.com/ktorio/ktor-samples/tree/master/other/proguard