In Kotlin, does changing internal classes break ABI?

Viewed 84

In the Kotlin programming language, let's say there is a module structure with classes like below

/// module A 
   public interface Hello {
      fun doSomething()
   }

   internal class HelloImpl {
       override fun doSomething(){}
   }
// end module A



// Module B--depends on---> Module A

import Hello

class HelloConsumer(private val hello: Hello) {
    fun useDoSomething() {
        hello.doSomething();
    }
}

My question is does making changes to the HellImpl 's doSomething function break the ABI for the module A, and therefore invalidate teh module B and have it recompile/build again?

1 Answers

(I assume HelloImpl implements Hello)

No, it doesn't break ABI, it is safe to do.

I'm not sure what sources to provide to proof this claim, but this is one of the main reasons why we split the code into public interfaces and private/internal implementations. Module B does not really know about the HelloImpl, it does not reference it. In practice, it uses HelloImpl, but it doesn't know about it. Hello is the only type it "sees".

We can verify this by decompiling HelloConsumer. If using decompiler provided in IntelliJ (I believe this is the FernFlower decompiler) we receive the following code:

public final class HelloConsumer {
   private final Hello hello;

   public final void useDoSomething() {
      this.hello.doSomething();
   }

   public HelloConsumer(@NotNull Hello hello) {
      Intrinsics.checkNotNullParameter(hello, "hello");
      super();
      this.hello = hello;
   }
}

As we can see, HelloConsumer uses Hello type only, it doesn't reference HelloImpl in any way.

Related