Kotlin has become the official language of Android, and it seems that we will develop more with kotlin from now on. I had the opportunity to replace some java classes with kotlin at work Make a note of how you fixed the parts that did not work with batch replacement. Since Android development is still in its first year, please understand that there may be some parts that cannot be reached.
https://www.slideshare.net/kenichitatsuhama/java-kotlin
First, select convert Java File to Kotlin File from the Code menu.
Mostly it will replace it well.
Basic return is used when exiting a method It seems that if it is written in lambda, it will not correctly judge whether to exit the method or lambda. If you type @ after return, the correct one will come out as a candidate.
This is a fix to allow java to access kotlin's static methods.
If you rename it with the function of the IDE, there is no mistake than manual work, so use it.
This will be done even if the member is null, so fix it. There are the following methods.
Before correction.kt
hoge.fuga(piyo!!)
Revised.kt
piyo?.let {
hoge.fuga(it)
}
Example.kt
internal val icon: Drawable? by lazy {
ResourcesCompat.getDrawable(resources, R.drawable.icon, null)
}
Before correction.kt
bundle.getLong(hogehoge) != 0)
Revised.kt
bundle.getLong(hogehoge) != 0L)
I think that it is possible to take measures such as delay initialization using the lazy property for parts that are initialized by onCreate etc.
internal -> private
The part where findFragmentByTag is done, etc.
Replace with a singleton using the object keyword
We also refactored this time.
There seems to be other corrections, but that's all for this time. In addition, I will update it when new corrections come out.
Recommended Posts