How can we prove that Java and Kotlin can co-exist and its interoperable? Kotlin is designed with Java Interoperability in mind. Existing Java code can be called from Kotlin in a natural way, and Kotlin code can be used from Java rather smoothly as well. In this section we describe some details about calling Java code from Kotlin.
Let me explain the working of the below program.
There is one Kotlin class which is passing two values to the Java class and then that is further sent to another Kotlin class. This Kotlin class will process the data and send back to previous Java class and then this Java class will send this to first Kotlin class. This way, we tried to showcase that how Java and Kotlin can work together seamlessly.
Vides also attached at the bottom.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.technicaljungle.helloworld.hellokotlinworld" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.0.1' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.1.4-2' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.technicaljungle.helloworld.hellokotlinworld.KotlinMainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textHelloWorld" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.technicaljungle.helloworld.hellokotlinworld import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.widget.TextView import android.widget.Toast import kotlinx.android.synthetic.main.activity_hello_world.* class KotlinMainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_hello_world) Log.i("testing", "Kotlin1 request ") val result = CommonUtilityJava().add(1.2f, 2.7f) Log.i("testing", "Kotlin1 result " + result) } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.technicaljungle.helloworld.hellokotlinworld; import android.util.Log; public class CommonUtilityJava { float add(float a, float b) { Log.i("testing", "Java2 request " ); CommonUtilityKotlin commonUtilityKotlin = new CommonUtilityKotlin(); float result = commonUtilityKotlin.add(a, b); Log.i("testing", "Java2 result " + result); return result; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.technicaljungle.helloworld.hellokotlinworld import android.util.Log class CommonUtilityKotlin { protected fun add(a: Float, b: Float): Float { Log.i("testing", "Kotlin3 request ") val c = a + b Log.i("testing", "Kotlin3 result " + c) return c; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.technicaljungle.helloworld.hellokotlinworld"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".KotlinMainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
1 2 3 4 5 6 |
08-26 01:26:54.537 6163-6163/com.technicaljungle.helloworld.hellokotlinworld I/testing: Kotlin1 request 08-26 01:26:54.538 6163-6163/com.technicaljungle.helloworld.hellokotlinworld I/testing: Java2 request 08-26 01:26:54.538 6163-6163/com.technicaljungle.helloworld.hellokotlinworld I/testing: Kotlin3 request 08-26 01:26:54.538 6163-6163/com.technicaljungle.helloworld.hellokotlinworld I/testing: Kotlin3 result 3.9 08-26 01:26:54.538 6163-6163/com.technicaljungle.helloworld.hellokotlinworld I/testing: Java2 result 3.9 08-26 01:26:54.538 6163-6163/com.technicaljungle.helloworld.hellokotlinworld I/testing: Kotlin1 result 3.9 |