49

I'm trying to add Kotlin to my project and I need to use proguard. Which rules should I add to proguard to support Kotlin?

Thank you

Fábio Carballo
  • 3,245
  • 5
  • 26
  • 36

5 Answers5

42

You don't need to do anything special. Kotlin works with ProGuard out of the box. But you may face some strange errors when processing your application with ProGuard. In this case just add

-dontwarn kotlin.**

Also if you want to get rid of null checks at runtime you may use the following rule:

-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
    static void checkParameterIsNotNull(java.lang.Object, java.lang.String);
}
Michael
  • 53,859
  • 22
  • 133
  • 139
  • 2
    I believe a flag for kotlinc sets the policy for null checks. No need for proguard for that. – voddan May 25 '16 at 08:41
  • Where did you find the thing about the flag? I would very much like to try that, but can't seem to find it anywhere :/ – daemontus Jan 14 '17 at 14:35
  • @daemontus `kotlinc -X` yields `-Xno-param-assertions`, `-Xno-receiver-assertions` and `-Xno-call-assertions`; see also [source code](https://github.com/JetBrains/kotlin/blob/v1.2.71/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt#L89-L105) – TWiStErRob Oct 15 '18 at 18:54
  • Kotlin works with Proguard but Proguard is not working effectively with Kotlin yet. Kotlin support in Proguard is still in beta at this moment. E.g. Proguard cannot distinguish `internal` classes and members from `public` ones. It sees them as `public` because they are marked `public` in the bytecode. – WindRider Feb 24 '20 at 15:24
19
-keep class kotlin.** { *; }
-keep class kotlin.Metadata { *; }
-dontwarn kotlin.**
-keepclassmembers class **$WhenMappings {
    <fields>;
}
-keepclassmembers class kotlin.Metadata {
    public <methods>;
}
-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
    static void checkParameterIsNotNull(java.lang.Object, java.lang.String);
}

build gradle :

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

https://kotlinlang.org/docs/tutorials/kotlin-android.html

Misagh
  • 3,403
  • 1
  • 20
  • 17
11

In Kotlin 1.0.2 EAP proguard strips out when mappings for enums, so I have to keep them explicitly, so

-keepclassmembers class **$WhenMappings {
    <fields>;
}

is sufficient for correct obfuscation. Although if you want some performance improvements, you can also add

-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
    static void checkParameterIsNotNull(java.lang.Object, java.lang.String);
}
Adel Nizamuddin
  • 823
  • 14
  • 31
0

if you use android studio, proguards comes with default. But you should on "Enables code shrinking" and "Enables resource shrinking" options for your code security and code optimization.

open your gradile file and check below.

android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

Source : https://developer.android.com/studio/build/shrink-code

Emre Gürses
  • 1,992
  • 1
  • 23
  • 28
-3

Check in your build.gradle. Did you include:

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
David Rawson
  • 20,912
  • 7
  • 88
  • 124