@Keep annotation and keep rules are not working for interfaces nested inside classes or other interfaces by enabling Android R8 obfuscation

1.3k views Asked by At



I am using Android R8 obfuscation in my Android library. After including the AAR file in the application module of the library's client project, I am getting compile time errors for interfaces that are declared inside the class or another interface.

e.g. one of the public interfaces is MyPublicInterface:

interface InterfaceA {
    fun a()
}

interface InterfaceB {
    fun b()
}

class ClassA {
    interface NestedInterfaceOfA {
        fun nia()
    }
}

interface MyPublicInterface : InterfaceA, InterfaceB, ClassA.NestedInterfaceOfA

The interface MyPublicInterface is used by the application module.

I am getting the below errors while compiling the application module:

"Cannot access ClassA$NestedInterfaceOfA which is a supertype of MyPublicInterface. Please make sure you have the required dependencies in the classpath."
"unresolved supertypes ClassA$NestedInterfaceOfA"

I tried all the below rules:

-keepclasseswithmembers public interface * {
*;
}
-keepclasseswithmembernames public interface * {
*;
}
-keep public interface *$* {
*;
}
-keepclasseswithmembers public interface *$* {
*;
}
-keepclasseswithmembernames public interface *$* {
*;
}
-keepattributes InnerClasses, Signature

I tried by explicitly adding @Keep annotation in code and by explicitly adding the keep rule for a particular interface as well. But it didn't work.

Note: The entire code of the library is in Kotlin.

1

There are 1 answers

0
gsv On

The above specified issue is resolved now. I used below solution for it:

Android studio version: 4.1.x

Android tools version: classpath 'com.android.tools.build:gradle:4.1.1' (Issue was occurring with version 4.0.2)

Gradle version: 6.5.1 (Issue was occurring with version 6.3)