Your Kotlin and Java files can exist within the same source directories inside your project. You do not need to separate Kotlin and Java into homogeneous packages, modules, and so on. In general, your Kotlin and Java files will follow similar conventions for package naming and imports; however, Kotlin files can specify an arbitrary package name. This means that even though two files may exist within the same directory, they may have different declared packages, and therefore might require an explicit import.
Let's revisit theĀ AuthScreen example:
internal class AuthScreen private constructor(){
...
}
Both AuthScreen.kt and Main.kt are within the same folder, and exist within the default global package, so AuthScreen can be used from Main.kt without any explicit import statement:
val screen = AuthScreen.create()
However, we can specify a custom package name for AuthScreen.kt, as shown in the following code:
package authscreen
internal class AuthScreen private constructor(){
...
}
But specifying a package name for the existing code within Main.kt will cause it to break:
val screen = AuthScreen.create() // won't compile
We must now add an explicit import to Main.kt:
import authscreen.AuthScreen
val screen = AuthScreen.create()
Specifying custom package names can help you organize your code exactly how you see fit, but it adds complexity, and is something to be aware of as a difference between Java and Kotlin when you start to integrate Kotlin into an existing project.