I’m encountering an issue in Kotlin when obfuscation is enabled, specifically when calling a function dynamically using KFunction.callBy. The code works fine in a non-obfuscated environment but fails once obfuscation is applied.
data class User(
val name: String
)
private fun <T> buildObject(
...
constructor: KFunction<T>,
): T {
val params = constructor.valueParameters.mapIndexed { index, param ->
val res = getValueForParam()
param to res
}.toMap()
return constructor.callBy(params)
}
After enabling obfuscation, I receive the following error:
Callable expects 2 arguments, but 1 were provided.
It seems that constructor.caller.parameterTypes includes an additional parameter of type DefaultConstructorMarker.
proguard rules:
-keep class my.package.** { *;<init>(...); }
-keepclassmembers class my.package.**$* { *; }
-keepclassmembernames class my.package.**$* { *; }
-keep class kotlin.Metadata { *; }