Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Seamless Java and Kotlin Interoperability in Android Studio

Notes 2

Android Studio natively supports mixed-language codebases, enabling seamless method invocations between Java and Kotlin classes without boilerplate bridging logic.

Invoking Kotlin from Java

Consider a Kotlin utility class MessageDispatcher.kt designed to show a brief notification:

class MessageDispatcher {
    fun triggerNotification(ctx: Context, text: String) {
        Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show()
    }
}

Accessing this from a Java Activity requires instantiating the Kotlin class exactly as one would a standard Java class:

public class DashboardActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        MessageDispatcher dispatcher = new MessageDispatcher();
        dispatcher.triggerNotification(this, "Notification sent from Kotlin");
    }
}

Invoking Java from Kotlin

Conversely, existing Java classes integrate smoothly into Kotlin code. Define a Java class SystemLogger.java:

public class SystemLogger {
    public static void writeLog(String tag, String msg) {
        Log.d(tag, msg);
    }
}

In a Kotlin Activity, the static method is accessed directly using standard Kotlin syntax:

class HomeScreen : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        SystemLogger.writeLog("HomeScreen", "Log entry from Java class")
    }
}

Build Configuration

To establish a project that compiles both lnaguages, ensure the Kotlin Gradle plugin is applied in the build.gradle file. While older practices separated files into distinct java and kotlin source directories, the standard convention in modern Android Studio is to co-locate both Java and Kotlin files within the src/main/java directory. The build system automatically identifies and compiles files based on their extensions.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.