Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Kotlin generated byte code affect the method count?

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 ?

like image 391
Mel Avatar asked Mar 29 '19 15:03

Mel


People also ask

Is Kotlin bytecode the same as Java?

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.

Is Kotlin compiled to bytecode?

Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 8 compatible bytecode.

What is bytecode in Kotlin?

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.

How do I find my Kotlin compiled code?

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.


1 Answers

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)

like image 67
Eugene Petrenko Avatar answered Oct 16 '22 13:10

Eugene Petrenko