Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin "internal" visibility modifier in Android

Tags:

android

kotlin

Suppose you're writing an Android project (not a library). All the files are compiled together so... is there any sense to use an internal visibility modifier in this case?

like image 509
Nikolay Kulachenko Avatar asked Dec 20 '17 09:12

Nikolay Kulachenko


People also ask

What is internal modifiers in Kotlin?

The internal visibility modifier means that the member is visible within the same module. More specifically, a module is a set of Kotlin files compiled together: an IntelliJ IDEA module; a Maven project; a Gradle source set; a set of files compiled with one invocation of the Ant task.

Which is the default visibility modifier in Kotlin?

There are four visibility modifiers in Kotlin: private , protected , internal , and public . The default visibility is public .

What visibility modifier is not available in Kotlin?

In Kotlin the default visibility modifier is public while in Java is package-private. This modifier does not exist in Kotlin.

What is internal in Android Kotlin?

Internal is a new modifier available in Kotlin that's not there in Java. Setting a declaration as internal means that it'll be available in the same module only. By module in Kotlin, we mean a group of files that are compiled together.


2 Answers

You can have multiple Gradle modules that depend on each other within a single Android application, in that case, internal restricts visibility to within a given module. This could be useful if, for example, you have a separate data module that handles database and network tasks, and you only want to expose a couple interfaces from that module, but not their implementations.

Otherwise, if you're not using multiple modules, and your entire application is just in the default app module, then the internal modifier makes no difference in comparison to the default public visibility.

like image 55
zsmb13 Avatar answered Sep 24 '22 06:09

zsmb13


No, because you would only have one module. Take a look at the definition.

The internal visibility modifier means that the member is visible within the same module. More specifically, a module is a set of Kotlin files compiled together:

  • an IntelliJ IDEA module;
  • a Maven project; a Gradle source set;
  • a set of files compiled with one invocation of the Ant task.

(Source)

internal only has effect across several modules.

like image 35
Willi Mentzel Avatar answered Sep 24 '22 06:09

Willi Mentzel