Java – Does the bytecode generated by Kotlin affect the method count?

Does the bytecode generated by Kotlin affect the method count?… here is a solution to the problem.

Does the bytecode generated by Kotlin affect the method count?

For example, if I use

methodReference = ::method

And not

methodReference = { method(it) }

Due to reflection, the decompiled code will contain the getOwner, getName, getSignature methods in Java code. Do these methods count towards the 64k limit?

Solution

Only methods that have not been deleted by proguard/R8 will be counted

An example

  fun method(t : Any) {}
  val reference1: KFunction1<Any, Unit> = ::method
  val reference2: (Any) -> Unit = { method(it) }

For reference1 bytecode (decompiled to Java) it 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 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 for method references is 4 +1 and the difference for lambda is 1 +1, where +1 comes from the bridge method invoke(t:Any).

Related Problems and Solutions