For example, if I use
methodReference = ::method
rather than
methodReference = { method(it) }
decompiled code will contain getOwner
, getName
, getSignature
methods in Java code, due to reflection. Do these methods counted against 64k limit ?
For the most part, Kotlin compiles into the same bytecode as Java. Classes, functions, function arguments, and standard flow control operators, such as if and for, work in the same way. However, there are differences between Kotlin and Java. For example, Kotlin has inline functions.
Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 8 compatible bytecode.
Kotlin compiles to Java bytecode, just like Java code, and is executed at runtime by the Java Virtual Machine. The JVM itself doesn't know the difference—it receives and executes bytecode in the same way, regardless of whether it was produced from Java or Kotlin.
IntelliJ IDEA and Android Studio both offer this capability. From the Tools > Kotlin menu, choose “Show Kotlin Bytecode”. This will open up the “Kotlin Bytecode” view and will show you the JVM bytecode associated with the current Kotlin source file.
The methods are counted, only if they are not removed by proguard/R8
An example
fun method(t : Any) {}
val reference1: KFunction1<Any, Unit> = ::method
val reference2: (Any) -> Unit = { method(it) }
For the reference1
the bytecode (decompiled to Java) would be:
@NotNull
final KFunction reference1 = new Function1((X)this) {
// $FF: synthetic method
// $FF: bridge method
public Object invoke(Object var1) {.. }
public final void invoke(@NotNull Object p1) {..}
public final KDeclarationContainer getOwner() {..}
public final String getName() {..}
public final String getSignature() {..}
};
for the lambda (or reference2) the equivalent java code is:
@NotNull
final Function1 reference2 = (Function1)(new Function1() {
// $FF: synthetic method
// $FF: bridge method
public Object invoke(Object var1) {..}
public final void invoke(@NotNull Object it) {..}
});
So the difference is 4 +1 for method reference and 1 +1 for lambda, where +1
comes from the the bridge method invoke(t:Any)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With