Step-by-Step Explanation: Adding Kotlin to a Java Project
This document explains the steps taken to integrate Kotlin into the existing Java application and call Kotlin code from Java.
1. Project Configuration
Update Version Catalog (libs.versions.toml)
First, we added the Kotlin version and defined the Kotlin JVM plugin and standard library in the Gradle version catalog.
- File: libs.versions.toml
- Changes:
- Added
kotlin = "2.4.20"to the[versions]section. - Added
kotlin-stdlibto the[libraries]section. - Added
kotlin-jvmto the[plugins]section.
- Added
Apply Kotlin Plugin (build.gradle.kts)
Next, we applied the Kotlin JVM plugin to the application module and added the standard library dependency.
- File: build.gradle.kts
- Changes:
- Added
alias(libs.plugins.kotlin.jvm)to thepluginsblock. - Added
implementation(libs.kotlin.stdlib)to thedependenciesblock (though the plugin often adds this automatically, it's good practice to be explicit).
- Added
2. Implementing Kotlin Logic
Create Kotlin File (Hello.kt)
We created a new Kotlin file containing basic syntax examples: a function with a return value, a function that prints a sum, and a greeting function.
- File: Hello.kt
- Code:
package learn_kotlin
fun sum(a: Int, b: Int): Int {
return a + b
}
fun printSum(a: Int, b: Int) {
println("sum of $a and $b is ${a + b}")
}
fun getGreeting(): String {
return "Hello from Kotlin!"
}
3. Interoperability
Call Kotlin from Java (App.java)
Finally, we updated the Java main method to call the Kotlin functions. In Kotlin, top-level functions are compiled into a class named after the file with a Kt suffix (e.g., HelloKt).
- File: App.java
- Changes:
- Called
HelloKt.getGreeting(). - Called
HelloKt.printSum(10, 20). - Called
HelloKt.sum(5, 7)and printed the result.
- Called
4. Verification
We verified the changes by running a Gradle build:
- Ran ./gradlew :app:assemble to ensure everything compiles correctly.
- The IDE correctly resolved the HelloKt class after the build/sync.
Comments
Post a Comment