I have an Android App developed using Java. I now want to start using Kotlin for the same app. Is it possible to use Kotlin and Java side-by-side in an existing app?
simplest way to do this is :
It is possible to use Java and Kotlin side by side. If it is a small project I would recommend you converting your existing java files into Kotlin, this can be easily done using the ctrl+alt+shift_k shortcut. if the project is pretty large you can keep your java classes while creating new activities in Kotlin. I have an app that many of my model classes are written in java yet the activity and the UI itself is written in Kotlin. Since Kotlin is based on Java there is no problem having both languages in the same project as long as your java files are created in different classes than your Kotlin classes
First of all you have to download Kotlin Plugin and integrate that into android studio, after that using double shift write convert java file to kotlin file or you can use short cut Ctrl+Alt+Shift+K
To enable Kotlin support for your existing Java-only project follow the steps and Android Studio will automatically set-up everything for you :-
You're done and good to go!
NOTE :- Android Studio will automatically add the following lines of code for you in the respective files -
In Application-level Gradle file :-
apply plugin: 'kotlin-android'
......
dependencies{
......
implementation "androidx.core:core-ktx:+"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
......
repositories {
mavenCentral()
}
In Project-level Gradle file
buildscript {
ext.kotlin_version = '1.3.72'
......
dependencies {
......
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Just create a new Activity and set Source Language as Kotlin while creating activity Image
Yes you can mix Java and Kotlin in one project.
From Android doc :
If you're interested in using Kotlin, it's easy to get started because it works side by side with Java and C++ on Android. So you can keep your existing code, continue to use the various Android libraries, and incrementally add Kotlin code to your project. Unlike almost any other language, Kotlin is a drop-in replacement you can use bi-directionally—you can call into the Java language from Kotlin, and you can call into Kotlin from the Java language.
Also you should take a view of kotlin official doc
You can also convert Java code to Kotlin with IntelliJIDEA.