https://kotlinlang.org logo
#announcements
Title
# announcements
m

Marko Mitic

08/23/2019, 4:02 PM
Just found that
internal
kotlin classes are
public
to java code. How to write kotlin library without exposing all classes to java?
1
c

christiangruber

08/23/2019, 5:40 PM
Doesn't kotlin do name mangling to avoid allowing Java to see those internal types and functions? We had to explicitly annotate it not to do that for dagger modules where we needed the annotation processor to consume them correctly.
m

Marko Mitic

08/23/2019, 6:06 PM
Well, java code sees the internal classes, I just tested accessing internal object - IDE complains about it but compiler has no issue with it.
a

Amirul Zin

08/23/2019, 7:41 PM
Until we have better Java interop, I don't think its currently possible to fully hide a class from Java consumer. I wish we can at least annotate something like
@JvmPackageVisibility
for package private class since that visibility is super helpful for library devs to cater for Java consumer. In any case, you can look into
@JvmSynthetic
. It only applies to functions, fields etc though, and not applicable to classes. You can, however, use that with
internal
, which while the classes are still visible, the methods will at least be hidden from Kotlin and Java consumer.