我有一个图书馆项目,该项目为其他项目提供共享功能。该实现是用Kotlin编写的。但是,我确保该库的公共接口不使用Kotlin提供的任何类。
如果我们将此库作为依赖项添加到纯Java项目中,它将可传递地提取kotlin-stdlib
和kotlin-runtime
。现在,可以在仅Java的使用者项目中潜在地(但错误地)使用Kotlin-stdlib中的类。
有没有办法防止这种类型的泄漏?我可以想到的一些选择:
在Maven中将kotlin-stdlib-*
标记为provided
:这会在运行时引起任何问题吗? (ClassNotFoundException
等
阴影+重新定位kotlin-stdlib-*
和/或kotlin-runtime
:通过阴影,如果我们依赖多个Kotlin库项目,我可以预见问题。另外,我不确定这是否会隐藏Kotlin类型。
注意:问题Using a kotlin library in java code在这里不适用(标题误导)。我特别避免这种情况。
答案 0 :(得分:0)
使用implementation
关键字来声明您的库依赖项是否足够不传递它们?如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.