Progurad for android kotlin

Progurad for android kotlin

For more stories.

Something interesting for #AndroidDev

If already new to Proguard read my this two article which will help you to enable proguard for any android project.

  1. Enabling Proguard for Android
  2. How to de-obfuscate stack Trace is here?
This is it.

Google has announced to official kotlin support for Android development. which is really a very cool language. If you haven’t try now.

This is the link sample Project AndroidWithKotlin.

Now in this post, we will look into How we can use progurad in kotlin android project.

Before deep dive here I will recommend you to read my first article Enabling Proguard for Android, then move on.

Enabling proguard is the same process for any kind of the android project.

If you enable proguard for the android project and if you are using some of the kotlin extension libraries then you will come across some issues.

jackson-kotlin-module that provides deserializing kotlin classes and data classes which is a really cool feature.

The fix is here:-

This rule will help you to keep your annotation classes and it won’t warn for reflection classes.

-dontwarn kotlin.**
-dontwarn kotlin.reflect.jvm.internal.**
-keep class kotlin.reflect.jvm.internal.** { *; }

If you have an issue with kotlin MetaData. Especially in case of Jackson kotlin module.

-keep class kotlin.Metadata { *; }
-keepclassmembers public class com.mypackage.** {
    public synthetic <methods>;
}
-keepclassmembers class kotlin.Metadata {
    public <methods>;
}

For enum

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

The consolidated rule for kotlin android project.

-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);
}

Above rule will sufficient for kotlin android Project. But if you are using different libraries in your project then you have to add those specific configuration based on the error and warnings.

for Moshi

-keep class kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoaderImpl

which will keep only the no-arg constructor of the service defined in META-INF/services/kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoader

Thanks, Jake Wharton for this config.

To Learn more about kolin,

kotlin-bootcamp-for-programmers.

I have been following this path, love to hear more from you.

For more stories.

Lets connect on Stackoverflow , LinkedIn , Facebook& Twitter.