Search code examples
kotlinproguard

How to keep internal var in companion object


I have the following class definition:

class SomeObject {

    companion object {
       internal val instance = SomeObject()
    }

}

This class is self sustaining, and is just listening to events. My problem is that proguard removes the "instance" field.

What do I write in my proguard file to keep the instance field (it can be obfuscated, but i want to keep the field from being deemed as "unused code")


Solution

  • The proguard rule should look like this:

    -keepclassmembers class com.your.package.path.SomeObject {
        public static ** Companion;
    }
    

    This way you can specify classes of which the companion object should not be removed (including the property).