In the article Introduction to the Kotlin Language, I introduced Kotlin and promised to share my experience in the process of integrating Kotlin. This is one of the articles in that series.
First, we need a Java project, and I chose the project sample from the article Custom Exception and this repo!
Run the following commands (you can copy and run them at once):
1
2
3
4
5
# clone git sample repogit clone https://gitlab.com/thanhtrixx/trile-dev-sample/
cd trile-dev-sample/
# copy to new projectcp -r custom-exception how-to-use-kotlin-in-java-project
Open the IDE. Here, I’m using IntelliJ IDEA CE and follow these steps:
1. Update Gradle build config
Rename the project (skip this step if you don’t need to rename it). Open the settings.gradle file and update it as follows:
// Apply the Kotlin JVM plugin to add support for Kotlin.
id'org.jetbrains.kotlin.jvm'version"${kotlin_version}"
Similarly, update dependencies
1
2
3
4
// Reflection is a set of language and library features that allows you to introspect the structure of your program at runtime.
implementation'org.jetbrains.kotlin:kotlin-reflect'// Reflection is a set of language and library features that allows you to introspect the structure of your program at runtime.
implementation'org.jetbrains.kotlin:kotlin-stdlib'
Add config for compileKotlin and build jar file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
tasks.withType(KotlinCompile).configureEach{kotlinOptions{freeCompilerArgs=['-Xjsr305=strict']jvmTarget=jdk_version}}sourceCompatibility=jdk_versionjar{// This line of code recursively collects and copies all of a project's files
// and adds them to the JAR itself. One can extend this task, to skip certain
// files or particular types at will
from{configurations.compileClasspath.collect{it.isDirectory()?it:zipTree(it)}}duplicatesStrategy(DuplicatesStrategy.EXCLUDE)}
Finally, add gradle.properties:
1
2
kotlin_version=1.8.0jdk_version=17
2. Kotlin call Java
Create a directory to store Kotlin class. The rule is that .kt files of Kotlin must be placed in the src/main/kotlin directory:
mkdir -p src/main/kotlin/dev/trile/java2kotlin
Create KotlinSalaryTransfer class with the provided content:
Please check the result to see if it matches the Custom Exception article ;))
3. Java call Kotlin
Rewrite the VietcomBank class using Kotlin kotlin:
1
2
3
4
5
6
7
8
9
10
11
12
classVietcomBank:Bank{@Throws(SalaryException::class)overridefuntransferMoney(accountNo:String,amount:Int){checkTransferParams(accountNo,amount)if(amount>10000000)throwSalaryException(ErrorType.TRANSFER_MONEY_NOT_ENOUGH_MONEY,"VCB only support transfer amount less than 10M")println("VCB Kotlin transfer success")}}
Update MoneyTransfer to use the new VietcomBank class:
1
2
// private final Bank vietcomBank = new VietcomBank();
privatefinalBankvietcomBank=newdev.trile.java2kotlin.VietcomBank();
Transfer 5000000 to EM1
VCB Kotlin transfer success
Transfer Salary success
Conclusion: With this article, you will learn the first step to integrate Kotlin into Java, but the complete transition to Kotlin is a long journey. I will write a few more articles to make it easier for those who want to join me on this journey ;))
I updated the code sample in this repo. Thank you for reading this far.
Update: I have made updates to the content of build.gradle and gradle.properties to align with the development of Kotlin and Java.