As a beginner, developers might wonder how big companies handle their project, they mostly have mix of programming languages. The same happens with Android too. An Application can contain pre written java code and newly written Kotlin code on the same project and you wonder how this is done?
Well, lets look at this topic more clearly and go through all the scenarios of an Android project.
Scenario 1 :
If the project already contain Kotlin and one wants to add Java in it, then it's the Android Studio which better handles this. It actually shows a pop-up that if you want to convert this Java code to Kotlin. It has a smart AI for recognising Java codes and converting them into Kotlin super quick.
Note: As the code automatically gets converted into Kotlin, it needs more optimizations from the user like nullable checks and so on.
Scenario 2 :
If the project already contain Java and one wants to add Kotlin in it, then
You can simply add any Android template like an Empty Activity or a blank fragment. Or if you create your own custom kotlin class, enum, object, interface, etc. Then Android Studio displays a warning that Kotlin is not configured in the project as shown below:
Configure Kotlin by clicking Configure either in the upper right corner of the editor or in the event log alert that pops up in the lower-right corner. Select All modules containing Kotlin files when prompted. On hitting OK, the studio actually adds all the necessary dependencies and classpaths to the build.gradle file. And now the gradle file will contain some codes like:
//inside project build
buildscript {
ext.kotlin_version = '1.4.10'
...
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// Inside each module using kotlin
plugins {
...
id 'kotlin-android'
}
...
dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
This is basically the easiest way to config your java project to kotlin (as its the GUI method).
Hope you learnt something new by reading this small and quick article.
I am planning to make some long tutorials on Networking and its best practices and also on Caching. And i am also up for bringing these awesome small articles too.
Thanks and happy learning ๐