First of all, please note that I'm not expecting why do you want to obfuscate library
comments. This is a genuine problem I'm asking about.
I have been having an issue dealing with R8/obfuscation with an Android library written in Kotlin.
I've a public API method which is annotated with @JvmStatic
and that method takes a Lambda
as parameter.
For example, take a look at code below,
typealias MyLambdaCallback = (String, Map<String, Any>) -> Unit
@Keep
object MyApi {
private var callback: MyLambdaCallback? = null
@JvmStatic
fun setCallback(callback: MyLambdaCallback) {
this.callback = callback
}
}
I have added @Jvmstatic
so that Java
calling code can call the method statically rather than doing MyApi.INSTANCE.setCallback()
When I release the library without minification
, everything is fine and calling code from both Java
and Kotlin
is written as expected.
But now I want to release the library while turning on minification
.
That creates an issue.
Here is the error
java.lang.IncompatibleClassChangeError: The method 'void setCallback(kotlin.jvm.functions.Function2)' was expected to be of type virtual but instead was found to be of type static (declaration of 'com.demo.basic.Application' appears in /data/app/com.demo.basic-_0uJXPbtfs3UZ2Rp2h-RdQ==/base.apk!classes2.dex)
Am I making a mistake somewhere or this is expected as some kind of limitation ?
What did I Try ?
Removing
@Jvmstatic
resolves the issue but it created ugly Java calling codeKept
@Jvmstatic
but removedLambda
converting Lambda into aninterface with one method
and everything is working fine. UnfortunatelySAM for Kotlin classes
is not there yet, so callingKotlin
code looks ugly.