Fábio Carballo
Fábio Carballo

Reputation: 3275

How to use Kotlin with Proguard

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

Upvotes: 50

Views: 32281

Answers (6)

宋敏超
宋敏超

Reputation: 1

You should add configuration under compose.desktop ->application->

compose.desktop {
       application {
            buildTypes.release.proguard {
                obfuscate.set(true)
                configurationFiles.from(project.file("proguard-rules.pro"))
              }
          }
    }

Upvotes: 0

Emre Gürses
Emre Gürses

Reputation: 2180

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

Upvotes: 0

Check in your build.gradle. Did you include:

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

Upvotes: -3

Misagh
Misagh

Reputation: 3623

-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

Upvotes: 20

Adel Nizamuddin
Adel Nizamuddin

Reputation: 821

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

Upvotes: 11

Michael
Michael

Reputation: 54715

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

Upvotes: 43

Related Questions