Getting Started with Kotlin‑Java Mixed Development for Android

This guide walks through creating an Android project that combines Java and Kotlin, adding Kotlin dependencies via Maven, and implementing a simple controller with Kotlin syntax, illustrating variable definitions, method signatures, and basic service interaction.

Coder Trainee
Coder Trainee
Coder Trainee
Getting Started with Kotlin‑Java Mixed Development for Android

The author, previously focused on Android, decided to explore mixed Java‑Kotlin development and built a basic project to demonstrate the process.

Project creation

A standard Java project is created using the usual steps (see screenshot).

pom.xml configuration

The pom.xml is updated to include Kotlin libraries, selecting version 1.2.50. The added dependencies are:

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-reflect</artifactId>
    <version>${kotlin.version}</version>
</dependency>
<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-runtime</artifactId>
    <version>${kotlin.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-kotlin</artifactId>
    <version>2.9.6</version>
</dependency>
<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib-jdk8</artifactId>
    <version>${kotlin.version}</version>
</dependency>
<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-test</artifactId>
    <version>${kotlin.version}</version>
    <scope>test</scope>
</dependency>

With the dependencies in place, the author proceeds to write Kotlin code.

Controller code development

Kotlin variable definitions resemble JavaScript; they use var and do not require a trailing semicolon, e.g., var coupon = Coupon().

Method signatures differ from Java: the type follows the variable name, separated by a colon, and the return type follows the parameter list, also after a colon.

open fun homeList(type : String?, couponName : String?, couponType : String?, @RequestParam status: String): String {

The method defines four parameters— type, couponName, couponType, and status —and returns a String.

/**
 * 优惠券列表
 */
@RequestMapping(value="lists")
open fun homeList(type : String?, couponName : String?, couponType : String?, @RequestParam status: String): String {
    var coupon = Coupon()
    if (!StringUtils.isEmpty(type)) {
        coupon.type = type?.toInt()
    }
    coupon.title = couponName
    coupon.status = status.toInt()
    coupon.couponType = couponType?.toInt()
    val pageInfo = couponService.test()
    return "success"
}

The example demonstrates creating a Coupon object, setting its fields based on the incoming parameters, invoking a service method, and returning a success string.

The basic Java‑Kotlin mixed project is now complete.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AndroidKotlinMavenControllerMixed Development
Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.