https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Ju

03/06/2024, 3:30 PM
in general how often kotlin and java used in the same project? is it recommended?
c

CLOVIS

03/06/2024, 4:04 PM
It is very often used, and yes it is recommended. The only difficulties you may have will be with coroutines (because they don't exist in Java), everything else interoperates really easily.
r

Robert Jaros

03/06/2024, 4:05 PM
You can easily mix java and kotlin. There are cases when it's necessary. I personally often use Kotlin with SOAP web services, when some artifacts can only be generated as java sources.
k

Klitos Kyriacou

03/06/2024, 4:09 PM
It's a bit easier if you're using Gradle than Maven.
👍 1
s

Stephan Schröder

03/06/2024, 9:58 PM
I mostly see it while I - over time - refactor a java project to kotlin. Or while I use a java lib in kotlin code. Works like a charm. There are some useful annotations to know about to finetune the jvm representation of your kotlin code to better mix with java code: https://www.baeldung.com/kotlin/jvm-annotations
1
j

Ju

03/07/2024, 12:07 AM
thank you guys understood!
s

Stefan Oltmann

03/08/2024, 6:09 AM
Indeed, I mixed it too while porting/migrating Java libs to Kotlin in many steps. It’s a seamless experience. It helped a lot while converting Apache Commons Imaging to Ashampoo Kim. I first copied all the original Java code into a KMP template and added unit tests written in Kotlin. The original project didn’t have good ones and I needed to make sure not to break anything. And then I converted class by class while executing the tests in between. Sometimes the interop is a bit strange with data classes and you need to do stuff with the JVM annotations to make it work, but in general everything can be made to work and is stable. Personally I would have no fear to gradually convert any Java project to Kotlin in production. Just start writing new code in Kotlin today and convert old code when there is time. 🙂
1