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?
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.
String path = context. getFilesDir(). getAbsolutePath(); File file = new File(path + "/filename");
If your question is can you use kotlin files in java files and vice versa then the answer is yes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With