Shreyas Patil
Shreyas Patil

Reputation: 954

How to hide visibility of Kotlin internal class in Java from different modules?

I was working on the Android library which I'm developing in Kotlin. I kept access modifier of some classes as internal. Internal classes are only visible in that library module in Kotlin. If I implement that library in the app then it's not visible at all.

But the problem comes when accessing that library from Java code. If I create .java file and type name of that internal class of library then IDE is suggesting name and it's resolved and compiled without any error.

For e.g.

Library Module:

internal class LibClass {
    // Fields and Methods
}

After implementing above library in DemoApp module:

App Module

Kotlin:
fun stuff() {
    val lib = LibClass() // Error.. Not resolving
}
Java:
public void stuff() {
    LibClass lib = new LibClass() // Successfully resolving and compiling
}

So that's the problem. How can I achieve securing that class from Java?

Thank you!

Upvotes: 2

Views: 4205

Answers (3)

Mergim Rama
Mergim Rama

Reputation: 388

If you want to hide visibility of the class instead of each functions or fields, you can use @file:JvmSynthetic on top of your class.

E.g.

@file:JvmSynthetic
package  com.example.app

internal class LibClass {
    var count: Int = 0

    internal fun exampleFun() {
        
    }
}

Upvotes: 1

Shreyas Patil
Shreyas Patil

Reputation: 954

Not perfect solution but I found two hacky solutions

Annotate every public method of that internal class by @JvmName with blank spaces or special symbols by which it'll generate syntax error in Java.

For e.g.

internal class LibClass {

    @JvmName(" ") // Blank Space will generate error in Java
    fun foo() {}

    @JvmName(" $#") // These characters will cause error in Java
    fun bar() {}
}

Since this above solution isn't appropriate for managing huge project or not seems good practice, this below solution might help.

Annotate every public method of that internal class by @JvmSynthetic by which public methods aren't accessible by Java.

For e.g.

internal class LibClass {

    @JvmSynthetic
    fun foo() {}

    @JvmSynthetic
    fun bar() {}
}

Note:

This is explained in detail in this article.

Upvotes: 2

Mustaqode
Mustaqode

Reputation: 453

I see you have got class with internal modifier that cannot be instantiated in kotlin class but can be instantiated in Java file. If it’s a standalone class, you can make its constructor private and allow the instantiation using a static method (Companion object).

Upvotes: 2

Related Questions