- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 881字
- 2021-06-24 14:13:24
Mixing Kotlin and Java in a project
Using different languages within the same project is quite common; I have encountered projects where a mix of Java and Scala files formed the code base. Could we do the same with Kotlin? Absolutely. Let's work on the project we created earlier in the Kotlin with Gradle section. You should see the following directory structure in your IntelliJ (the standard template for a Java/Kotlin project):

You can place the Java code within the java folder. Add a new package to the java folder with the same name as the one present in the kotlin folder: com.programming.kotlin.chapter01. Navigate to New | Java class named CarManufacturer.java and use the following code for the purpose of the exercise:
public class CarManufacturer { private final String name; public CarManufacturer(String name) { this.name = name; } public String getName() { return name; } }
What if you want to add a Java class under the kotlin subfolder? Let's create a Student class similar to the previous one and provide a field name for simplicity, as shown in the following code:
public class Student { private final String name; public Student(String name) { this.name = name; } public String getName() { return name; } }
In the main function, let's instantiate our classes using the following code:
fun main(args: Array<String>) { println("Hellow World!") val student = Student("Alexandra Miller") println("Student name:${student.name}") val carManufacturer = CarManufacturer("Mercedes") println("Car manufacturer:${carManufacturer.name}") }
While the code compiles just fine, trying to run it will throw a runtime exception, saying that it can't find the Student class. We need to let the Java compiler look for code under the src/main/kotlin folder. In your build.gradle, add the following instruction:
sourceSets { main.java.srcDirs += 'src/main/kotlin' }
Now, we can compile and run the program, as follows:
$ gradle jar $ java -jar build/libs/chapter01-1.0-SNAPSHOT.jar
As your Kotlin code gets bigger, compilation will slow down since it will have to go and recompile each file. However, we can speed it up by only compiling the files that were changed between builds. The easiest way to enable this is to create a file called gradle.properties alongside build.gradle and add kotlin.incremental=true to it. While the first build will not be incremental, the following ones will be, and you should see your compilation time cut down quite a bit.
Maven is still, probably, the most widely used build system on the JVM. So, let's see how we can achieve our goal of mixing Kotlin and Java code in Maven. Starting with IntelliJ, choose New | Project, pick Maven as the option, and look for kotlin-archetype-jvm from the list of archetypes. We already covered this, so it should be a lot easier the second time around. We now have a project.
From the project tree, you will notice that there is no java folder source code created. Go ahead and create src/main/java, followed by the namespace folder com.programming.kotlin (this will be a subfolder of the java folder). You will notice that right-clicking on the java folder won't give you the option to create a package. The project is not yet configured to include Java code. But first, what makes Maven handle Kotlin code? If you open the pom.xml file and go to the plugins section, you will notice the kotlin plugin, as shown in the following code:
<plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin>
To add Java code to the mix, we need to set a new plugin that will be able to compile good old Java, as follows:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <executions> <execution> <id>default-compile</id> <phase>none</phase> </execution> <execution> <id>default-testCompile</id> <phase>none</phase> </execution> <execution> <id>java-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>java-test-compile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin>
The Kotlin compiler has to run before the Java compiler to get it all working, so we will need to amend the Kotlin plugin to do just that, as follows:
<plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <goals> <goal>compile</goal> </goals> <configuration> <sourceDirs> <sourceDir>${project.basedir}/src/main/kotlin</sourceDir> <sourceDir>${project.basedir}/src/main/java</sourceDir> </sourceDirs> </configuration> </execution> <execution> <id>test-compile</id> <goals> <goal>test-compile</goal> </goals> <configuration> <sourceDirs> <sourceDir>${project.basedir}/src/main/kotlin</sourceDir> <sourceDir>${project.basedir}/src/main/java</sourceDir> </sourceDirs> </configuration> </execution> </executions> </plugin>
To be able to produce the executable JAR for the code we are about to write, we need yet another Maven plugin, as shown in the following code:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.programming.kotlin.HelloKt</mainClass> </manifest> </archive> </configuration> </plugin>
The preceding code will give you a JAR containing just your code; if you want to run it, then you need the extra dependencies in relation to the classpath, as follows:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <archive> <manifest> <mainClass>com.programming.kotlin.HelloKt</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </execution> </executions> </plugin>
Now, we are in a position to add the classes from the previous example (the CarManufacturer and Student classes) and change the main class to contain the following:
val student = Student("Jenny Wood") println("Student:${student.name}") val manufacturer = CarManufacturer("Honda") println("Car manufacturer:${manufacturer.name}")
This is not ready yet. While compiling will go well, trying to execute the JAR will yield an error at runtime due to the Student class not being found. The Java compiler needs to know about the Java code sitting under the kotlin folder. For that, we bring in another plugin, as shown in the following code:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <phase>generate-sources</phase> <goals><goal>add-source</goal></goals> <configuration> <sources> <source>${project.basedir}/src/main/kotlin</source> </sources> </configuration> </execution> </executions> </plugin>
Finally, we are in a position to compile and run the code. First, we package up the JAR using the Maven package command. Then, we execute this assembled JAR, as follows:
$ mvn package $ java -jar target/chapter01-maven-mix-1.0-SNAPSHOT-jar-with-dependencies.jar
Running these commands will result in the print statements being executed and outputted to the console.
- Learning Java Functional Programming
- JavaScript從入門到精通(微視頻精編版)
- Oracle 12c中文版數據庫管理、應用與開發實踐教程 (清華電腦學堂)
- 算法大爆炸:面試通關步步為營
- oreilly精品圖書:軟件開發者路線圖叢書(共8冊)
- 基于差分進化的優化方法及應用
- Internet of Things with the Arduino Yún
- jQuery從入門到精通 (軟件開發視頻大講堂)
- Python:Master the Art of Design Patterns
- Unity UI Cookbook
- 利用Python進行數據分析
- Learning Apache Cassandra
- C++ System Programming Cookbook
- Application Development with Parse using iOS SDK
- Android Studio開發實戰:從零基礎到App上線 (移動開發叢書)