Kotlin static methods not recognized after Proguard/R8 minimization?

33 Views Asked by At

I'm using R8 to minimize my library, and the companion object's methods aren't being picked up as references of the main class. The companion and the methods still exist, I just can't call them without referencing the companion object.

// Works
MyClass.Companion.foo()

// Errors with an unresolved reference.
MyClass.foo(this)

My proguard rules:

-keep @interface kotlin.Metadata { *; }
-keepattributes RuntimeVisibleAnnotations

-keep public class com.example.MyClass {
    public <methods>;
    public static <methods>;
}
-keep public class com.example.MyClass$Companion {
    public <methods>;
}

The companion methods are also labeled @JvmStatic, and those static methods exist in the generated code, but the kotlin compiler doesn't recognize those either.

1

There are 1 best solutions below

0
Jaxon M On

This is due to your models getting obfuscated when minify is enabled. To fix add the path to your models package to your Proguard rules.

Something like this should work:

-keep class your.package.name.model.** { *; } //remember to modify this
-keepattributes Signature
-keepattributes *Annotation*
-keep class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    private void readObjectNoData();
    Object writeReplace();
    Object readResolve();
}