As we said, we need to make a decision whether we will completely rewrite our modules into Kotlin or continue writing our code in Kotlin but keep its legacy in pure Java. What will we do? In this chapter, we will demonstrate a little bit of each.
Our current project, at this point, doesn't have anything to migrate. So, we will create some code. If you don't have the Java sources directory with the packages structure, create it. Now, add the following packages:
- activity
- model
These packages are equivalent to the packages we already have in our Kotlin source code. In the activity package, add the following classes:
- The MigrationActivity.java code is as follows:
package com.journaler.activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import com.journaler.R; public class MigrationActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume(); } }
- MigrationActivity2.java: Make sure it has exactly the same implementation as MigrationActivity.java. We just need some code base to present and migrate.
Register both activities in the Android manifest file as follows:
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android" package="com.journaler"> ... <application ... > ... <activity android:name=".activity.MainActivity" android:configChanges="orientation" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name=
"android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".activity.NoteActivity" android:configChanges="orientation" android:screenOrientation="portrait" /> <activity android:name=".activity.TodoActivity" android:configChanges="orientation" android:screenOrientation="portrait" /> <activity android:name=".activity.MigrationActivity" android:configChanges="orientation" android:screenOrientation="portrait" /> <activity android:name=".activity.MigrationActivity2" android:configChanges="orientation" android:screenOrientation="portrait" /> </application> </manifest>
As you can see, the Java code stands together with the Kotlin code without any issue. Your Android project can use both! Now, think, do you really need to do any conversion at all or are you fine to keep the existing Java stuff? Let's add classes in the model package:
- The Dummy.java code is as follows:
package com.journaler.model; public class Dummy { private String title; private String content; public Dummy(String title) { this.title = title; } public Dummy(String title, String content) { this.title = title; this.content = content; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
- The Dummy2.java code is as follows:
package com.journaler.model; import android.os.Parcel; import android.os.Parcelable; public class Dummy2 implements Parcelable { private int count; private float result; public Dummy2(int count) { this.count = count; this.result = count * 100; } public Dummy2(Parcel in) { count = in.readInt(); result = in.readFloat(); } public static final Creator<Dummy2>
CREATOR = new Creator<Dummy2>() { @Override public Dummy2 createFromParcel(Parcel in) { return new Dummy2(in); } @Override public Dummy2[] newArray(int size) { return new Dummy2[size]; } }; @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(count); parcel.writeFloat(result); } @Override public int describeContents() { return 0; } public int getCount() { return count; } public float getResult() { return result; }
}
Let's check again if the Kotlin part of the project sees these classes. Create a new .kt file in the root of your Kotlin sources directory. Let's call it kotlin_calls_java.kt:
package com.journaler import android.content.Context import android.content.Intent import com.journaler.activity.MigrationActivity import com.journaler.model.Dummy2 fun kotlinCallsJava(ctx: Context) { /** * We access Java class and instantiate it. */ val dummy = Dummy2(10) /** * We use Android related Java code with no problems as well. */ val intent = Intent(ctx, MigrationActivity::class.java) intent.putExtra("dummy", dummy) ctx.startActivity(intent) }
As you can see, Kotlin doesn't have any problems using the Java code. So, if you still want to proceed with the migration, you can do it. No problem. We will do so in the following sections.