Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Kotlin files be put in a separate source directory in Android?

I'm going to start using Kotlin for Android development in addition to Java because of its benefits. I have installed the Android Studio plugin and included the relevant dependencies in my gradle files.

So I've read from the Kotlin documentation and Stack Overflow that it's possible to include a separate source directory for Kotlin files, like so:

app:
-manifest
-java
-kotlin
-res

I know I can create this directory by adding the following to my build.gradle file:

sourceSets {     main.java.srcDirs += 'src/main/kotlin' } 

My question is: should Kotlin files 'live' with Java files in the same directory or not?

In addition to opinions, I would like to know whether there is a particular convention for this, and if so, why it is the way that it is.

If not, then what are the advantages and disadvantages of each option?

like image 216
Farbod Salamat-Zadeh Avatar asked Jul 17 '16 20:07

Farbod Salamat-Zadeh


People also ask

Where do I put the Kotlin code in Android Studio?

To convert Java code to Kotlin, open the Java file in Android Studio, and select Code > Convert Java File to Kotlin File. Alternatively, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file.

Where is the file directory in Kotlin?

String path = context. getFilesDir(). getAbsolutePath(); File file = new File(path + "/filename");

Can I use Kotlin and Java at the same time?

If your question is can you use kotlin files in java files and vice versa then the answer is yes.


1 Answers

Putting the Kotlin files in a separate source directory exists as a documented possibility because in early (pre-1.0) versions of Kotlin this was the only supported setup. Afterwards, the Kotlin Gradle plugin was made more flexible, so the directory separation is no longer necessary. I'm not aware of any benefits that could be gained by putting Kotlin files in a separate source directory.

Having a separate source directory is especially inconvenient when you have a Java project which you're gradually converting to Kotlin. Moving each converted file to a different source directory makes the conversion process unnecessarily more cumbersome.

like image 90
yole Avatar answered Oct 09 '22 04:10

yole