How to convert code from Java to Kotlin: Now you can use Kotlin (KAD 29) and kotlinkad
By Antonio Leiva
Time: Jul, 4, 2017
Link: https://antonioleiva.com/kotlin-from-java/
One of the most amazing features of Kotlin is its ability to fully integrate with Java. This means that although all the code of your application is written in Java, you can still create a class in Kotlin and use it from Java without any problems.
There are two advantages:
Today we will look at how this compatibility works and how to use Kotlin code from Java.
Software Package-level functions
In Kotlin, functions do not need to be in the class, but Java is not. So how do we call functions? Imagine that we have a file utils. kt, as shown below:
1 fun logD(message: String) {2 Log.d("", message)3 }4 5 fun logE(message: String) {6 Log.e("", message)7 }
In Java, we can access them through the UtilsKt class and use some static methods:
1 UtilsKt.logD("Debug");2 UtilsKt.logE("Error");
In the previous article, you have seen that I like extended functions. In Java, how do they work? If we have the following:
1 fun ViewGroup.inflate(resId: Int, attachToRoot: Boolean = false): View {2 return LayoutInflater.from(context).inflate(resId, this, attachToRoot)3 }
Note:
Although they may appear at some point in time, I have not explicitly explained this. The independent variables of the function may have default values. That is to say, if we do not specify them specifically, they will use the value specified during declaration. If we want to use it in Java, this will prevent us from using method overloading.
This function is usedViewGroup
. It receives a layout and expands it in its parent view.
What do we get if we want to use it in Java?
1 View v = UtilsKt.inflate(parent, R.layout.view_item, false);
As you can see, the object (receiver) that applies this function is added to the function as a parameter. In addition, because we cannot use the default value in Java, it is mandatory to select a parameter.
To generate the corresponding overload in Java, you can use the @ JvmOverloads annotation for this function. In this way, you do not need to specify false in Java:
1 @JvmOverloads2 fun ViewGroup.inflate(resId: Int, attachToRoot: Boolean = false): View {3 return LayoutInflater.from(context).inflate(resId, this, attachToRoot)4 }
1 View v = UtilsKt.inflate(parent, R.layout.view_item);
If you want to specify the class name when using Java, you can use annotations to modify it. In the utils. kt file, add it before package:
1 @file:JvmName("AndroidUtils")
Now the classes in Java will be named:
1 AndroidUtils.logD("Debug");2 AndroidUtils.logE("Error");3 View v = AndroidUtils.inflate(parent, R.layout.view_item, false);
Instance and static Fields
In Java, we use fields to store the status. They can be instance fields, which means that each object has its own or static (all class instances will share them ).
If we try to find the corresponding object in Kotlin, it will be the property and companion object. If we have such a class:
1 class App : Application() { 2 3 val appHelper = AppHelper() 4 5 companion object { 6 lateinit var instance: App 7 } 8 9 override fun onCreate() {10 super.onCreate()11 instance = this12 }13 14 }
How does this work in Java? You can simply access the companion object of static fields and use the attributes of getter and setter:
1 AppHelper helper = App.instance.getAppHelper();
You will compile it. As val, it only generates getter in Java. If it is var, we also have a setter.
Because it uses the lateinit annotation, access to the instance has been automatically executed, it will automatically expose the fields of Kotlin used to store the status. But suppose we create a constant:
1 companion object {2 lateinit var instance: App3 val CONSTANT = 274 }
You will see that you cannot directly access it. You must use the Companion internal class to access:
1 KotlinTest.Companion.getCONSTANT()
Who is better? To expose a static field in the same way in Java, you need a new comment:
1 @JvmField val CONSTANT = 27
Now you can use the Java code:
1 int c = App.CONSTANT;
If you have a function in the companion object, you can use the @ JvmStatic annotation to convert it to a static method.
Conclusion
You can see that the Kotlin code used by Java is very simple. Here I have demonstrated some of the most typical examples, and others can be implemented in a very similar way.
I hope if you have any questions, this will persuade you to start using Kotlin in the project. If you want to think carefully, I suggest you read this group of Kotlin articles.