I wanted to convert my company's app to Java-> Kotlin, so I first tried converting everything to Kotlin with my Android app (all Java code).
About the measures taken at that time. It didn't take much effort, probably because my app was small.
A peripheral search app called PLACE SEARCH.
https://play.google.com/store/apps/details?id=com.hikarusato.placesearch
Day 2
build.gradle (for the project)
buildscript {
ext.kotlin_version = '1.0.6'//add to
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"//add to
}
}
bash:build.gradle(Module:app)
apply plugin: 'kotlin-kapt'//Added if you want to make annotations available in kotlin.
apply plugin: 'kotlin-android'//add to.
・
・
・
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"//add to.
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"//Added when using reflection (property name or class name acquisition)
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"//Added when using a test project
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"//Added when using a test project
}
Reference: https://kotlinlang.org/docs/reference/using-gradle.html
This will convert all java code into Kotlin code.
Example
//Java in android studio->After converting to Kotlin
mWaitDialog = ProgressDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
//Here, mWaitDialog!!.setMessage or mWaitDialog?.I get an error when I change to setMessage
mWaitDialog.setMessage(resources.getString(R.string.now_updating))
Kotlin code after automatic conversion
class A {
private enum class TYPE {
TYPE_1,
TYPE_2
}
internal class B {
var state = TYPE.TYPE_1//Error not recognizing enum TYPE here
}
}
Revised
class A {
//Remove private
enum class TYPE {
TYPE_1,
TYPE_2
}
internal class B {
var state = TYPE.TYPE_1
}
}
Kotlin code after automatic conversion
enum class TYPE {
TYPE_1,
TYPE_2
}
・
・
・
Enum.valueOf<TYPE>(TYPE::class.java!!, "Enum enumeration")//Enum.valueOf<TYPE>Error in
Revised
enum class TYPE {
TYPE_1,
TYPE_2
}
・
・
・
TYPE.valueOf("Enum enumeration")//Enum name.Change to valueOf format
Before correction
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
Revised
//The favicon can be null, so?Attach.
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
Before correction
override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation
Revised
//Animation can be null, so?Attach.
override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation?
Recommended Posts