https://kotlinlang.org logo
#android
Title
# android
s

sirrah

05/09/2019, 9:05 AM
Apologies for taking us away from the declarative UI goodness but I'd appreciate some help with an issue with Kotlin lambdas and ProGuard. When any lambda code is run at runtime I receive an exception stating that the
invoke
method of that lambda is abstract.
In general lambdas should be translated to a new class extending from
Lambda
and
FunctionN
. So one of my lambdas gets translated to this:
Copy code
new Lambda(success)
{
    public final void invoke()
    {        MyAccessory.access$getModule$p(this.this$0).disconnect(success, failure);
    }
}
With details depending on the exact input arguments and captured variables for the specific lambda. But ProGuard mangles it to this:
Copy code
static final class e
            extends Lambda
            implements Function1<MyError, Unit>
{
    e(MyAccessory myAccessory, Function0 paramFunction0, Function1 paramFunction1)
    {
        super();
    }
}
Note that the invoke method is missing entirely.
If I add both
-dontshrink
and
-dontobfuscate
to the rules the first version is generated and the code will work. If I disable either the second version is generated and the code fails. Since I do want to obfuscate the code I'm looking for more specific rules to keep the lambdas intact. I'm trying rules like these but they don't seem to have any effect:
Copy code
-keep class kotlin.jvm.internal.Lambda { *; }
-keep class * extends kotlin.jvm.internal.Lambda { *; }
-keep class kotlin.coroutines.jvm.internal.SuspendLambda { *; }
-keep class * extends kotlin.coroutines.jvm.internal.SuspendLambda { *; }

-keepclassmembers class * extends kotlin.jvm.internal.Lambda {
    *;
}
-keepclassmembers class * extends kotlin.coroutines.jvm.internal.SuspendLambda {
    *;
}
-keepclassmembers class kotlin.jvm.internal.Lambda {
    *;
}
-keepclassmembers class kotlin.coroutines.jvm.internal.SuspendLambda {
    *;
}
This is for a jvm library project where I'm applying ProGuard (v6.0.3) manually. So it's not directly related to Android but I figured there is a good chance someone here knows more about ProGuard than I do.