Joint Compilation
We'll cover the following...
How to intermix code
You can intermix code written using Java and Kotlin two different ways:
-
Bring code written in either of those languages into a Java or Kotlin project as a JAR file dependency.
-
Have source files written in the two languages, side by side, in a project.
The first approach is the most common. Currently you bring dependencies from Maven or JCenter into your Java projects using a build tool like Maven or Gradle. Likewise, you can bring dependencies into your Kotlin projects. The JAR files your code depends on may be created from Java, Kotlin, or both. If there are no compatibility issues, using these JAR files written in Kotlin or Java feels natural, just like using Java JAR files in Java projects. Any compatibility issues that may surface will be due to the language differences and not due to the JAR file integration. The techniques discussed later in this chapter will help resolve those issues.
The second approach, of mixing both Java and Kotlin source code in one project, is an option if you’re introducing Kotlin into legacy Java projects and you want to make use of the power of Kotlin in some areas of the application.
Obviously, to compile the code written in the two different languages, you’ll have to use the compilers of the ...