I have a Kotlin class with some variables in companion object. After enabling proguard, The variables are not getting accessed.
class Test{
......
companion object {
const val USER_NAME = "user_name"
.....
}
.....
}
Proguard rules include:-
-keep class kotlin.** { *; }
-keep class kotlin.Metadata { *; }
-dontwarn kotlin.**
-keepclassmembers class **$WhenMappings {
<fields>;
}
-keepclassmembers class kotlin.Metadata {
public <fields>;
public <methods>;
}
-keepclassmembers class * {
static final % *;
static final java.lang.String *;
}
To create a companion object, you need to add the companion keyword in front of the object declaration. The output of the above code is “ You are calling me :) ” This is all about the companion object in Kotlin. Hope you liked the blog and will use the concept of companion in your Android application.
In Android, the common tools are ProGuard and more recently, R8. This means that you can run ProGuard on your Kotlin app and everything should work as intended without crashing.
The companion object can be declared within an existing class and the functions or parameters can be defined inside the companion object declaration. As you can see above, we can declare a companion object inside an existing class.
Problem resolved using @Keep
before companion object
class Test{
......
@Keep companion object {
const val USER_NAME = "user_name"
.....
}
.....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With