Why does Kotlin allow two classes with the same name and package in different folders (i.e.: main and androidTest)?

I found that in Kotlin it is possible to have two classes with the same name in the same package, but one of them is in the androidTest folder, and when running Espresso tests it will be used by one of them. This is not possible in Java and I am wondering if this is the intended behavior or is related to something else.

enter image description here

In the image, AppCollaborator in Kotlin exists in androidTest and main, AS is not complaining, but this is about JavaCollaborator.

The thing is, it is very useful to double some classes during a test (API, etc.), but I don't know if I can rely on it.

+3


source to share


1 answer


This is supported because in the output-build folder you have different subfolders for example.

  • Build / classes / java / home
  • build / classes / java / test
  • Build / Kotlin-classes / home


Your class files are stored in different target folders, but at run time they have the same fully qualified name due to the same package and class name.

This is not a Kotlin problem. This is a common name. Don't use the same name for testing and implementation. Usually the test is Test

appended to its name. In your case:JavaCollaboratorTest

+2


source







All Articles