https://kotlinlang.org logo
#announcements
Title
# announcements
k

Kevin

06/24/2019, 10:05 PM
Is there any way to name the receiver parameter that is generated for Java interop when declaring an extension function? For instance, given a file
extensions.kt
with a top-level function
@file:JvmName("Utils")
and
fun String.secretFeature()
, in the Java code, this translates to
Utils.secretFeature(myString)
. However, the parameter name for
myString
becomes
$this$secretFeature
and generates a constant available at runtime used in the generated
Intrinsics.checkParameterIsNotNull()
invocation. We rely on ProGuard for stripping out unreleased features and have a script to decompile and search for keywords. In this case, this generated parameter name is leaked as there's no way for ProGuard to remove that constant. I'd like to do something like
@receiver:JvmName("stringParam") fun String.secretFeature()
.
d

Dico

06/24/2019, 10:34 PM
You can mark calls to these functions as no-ops with a proguard rule, so they will be removed
👍 1
Along with the parameter constant
k

Kevin

06/24/2019, 10:38 PM
The Intrinsics? I guess that could work, you'd just lose that check though, so it would technically be a behavioral change if you did pass null.
I found one other workaround which is a bit ugly, but works. I added a non-extension function (e.g.
fun mySecretFeature(stringParam: String)
and then a Kotlin extension function that delegates to it (
@JvmName("_") fun String.mySecretFeature()
) and then the Java classes never use the generated extension. I guess the generated Java method gets stripped out as unused via ProGuard. Technically it's still visible in autocomplete as
Utils._
but it's good enough for now.
3 Views