2

I have a library project which provides shared functionality for other projects. The implementation is written in Kotlin. However, I've ensured that the public interface of the library does not use any classes provided by Kotlin.

If we add this library as a dependency to a Java-only project, it transitively pulls in kotlin-stdlib and kotlin-runtime. Classes from Kotlin-stdlib can now be potentially (but mistakenly) used in the Java-only consumer project.

Is there a way to prevent this type leakage? Some options that I can think of:

  • Mark kotlin-stdlib-* as provided in Maven: Will this cause any issues at runtime? (ClassNotFoundException and such)

  • Shade + relocate kotlin-stdlib-* and/or kotlin-runtime: With shading, I foresee issues if we depend on multiple Kotlin library projects. Also, I'm not sure if this will actually hide the Kotlin types.

Note: The question Using a kotlin library in java code does not apply here (misleading title). I specifically avoid that scenario.

metacubed
  • 7,031
  • 6
  • 36
  • 65
  • It sounds more like an IDE configuration issue than a build issue. What IDE are you using? When you say that "[...] they now see ambiguous library types [...]", do you mean for autocomplete and import statements? – Robby Cornelissen Aug 29 '18 at 06:39
  • @RobbyCornelissen, my intention is to prevent Kotlin stdlib classes from being available in the Java project. Everything else is cosmetic. – metacubed Aug 29 '18 at 07:13
  • If they're not available, how can your Kotlin library use them? – Robby Cornelissen Aug 29 '18 at 07:14
  • 2
    @RobbyCornelissen, yes, that's the rub. I can't think of a way to encapsulate these dependencies so they are only available to my library implementation (Java 9 modules, anyone?). – metacubed Aug 29 '18 at 07:19

1 Answers1

0

Isn't declaring your library dependencies using implementation keyword enough for not exposing them transitively ? like stated here We use the implementation dependency configuration so that we don’t expose any of the Kotlin standard library through our API. This has architectural and performance benefits.

Guerneen4
  • 708
  • 8
  • 17
  • 1
    The `implementation` keyword is specific to Gradle, right? I'm looking for a Maven solution (see tags). – metacubed Nov 08 '18 at 16:49