Convert all Android apps (Java) to Kotlin

Overview

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.


target


My Android, Kotlin experience


App converted to Kotlin

A peripheral search app called PLACE SEARCH.

qiita.gif

https://play.google.com/store/apps/details?id=com.hikarusato.placesearch


Corresponding man-hours

Day 2


manner

  1. If Kotlin is not included in Android Studio, install the plugin. There are ways to do it in various articles.
    http://blog.ch3cooh.jp/entry/20160217/1455676200
  2. Edit build.gladle.

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

  1. In the Android project navigation, select the app-> java folder and select the main menu [Code]-> [Convert Java File to Kotlin File]. スクリーンショット 2017-02-04 23.51.16.png

This will convert all java code into Kotlin code.


Crushed compile error


-NullableType object. Add !! or? Where it is in the method format.

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))


-Unable to recognize private Enum in the same class in the internal class.

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
   }
}

· Java Enum.valueOf (Enum name.class, "Enum enum") is not converted correctly

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

Crushed run-time error


・ Null, parameter favicon at ~ .onPageStarted

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?) {


· Java.lang.IllegalStateException: ViewUtils.createNaviTran… transit, enter, nextAnim) must not be null


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?

Advantages / disadvantages of kotlin conversion (impression)


merit




Demerit



Summary


Recommended Posts

Convert all Android apps (Java) to Kotlin
[Android] Convert Android Java code to Kotlin
[Android] Convert Map to JSON using GSON in Kotlin and Java
Specify Java / Kotlin compile-time options for Android apps
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 3
Kotlin Class to send to Java developers
[Android] Convert Android Java code to Kotlin
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 2
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 1
How to convert Java radix
[Java] Convert ArrayList to array
Refer to C ++ in the Android Studio module (Java / kotlin)
Kotlin Class to send to Java developers
[Java] Convert 1-to-N List to Map
[Java] Convert array to ArrayList * Caution
Kotlin Class part.2 to send to Java developers
Use ViewModel to rotate Android apps roughly
Convert from java UTC time to JST time
[Java] Convert Object type null to String type
Convert SVG files to PNG files in Java
Kotlin scope functions to send to Java developers
Creating Java Web Applications to Azure Web Apps
Memo for migration from java to kotlin
Java to C and C to Java in Android Studio
Introduction to Java Web Apps Performance Troubleshooting
Kotlin vs. Java: Which Programming Language to Choose for Your Android App
Summary of good points and precautions when converting Java Android application to Kotlin
Kotlin functions and lambdas to send to Java developers
[Android] How to convert a character string to resourceId
Introduce Kotlin to your existing Java Maven Project
Migrate from Java to Server Side Kotlin + Spring-boot
[Java] Convert DB code to code value using enum
Initial settings for rewriting Java projects to Kotlin
How to convert a solidity contract to a Java contract class
Getting started with Kotlin to send to Java developers
[Java] Introduction to Java
What I did when I converted java to Kotlin
Convert Java nested beans to aaa.bbb [0] .ccc format
Introduction to java
[Java] 4 steps to implement splash screen on Android
Where Java programmers tend to trip over Kotlin
How to write Java String # getBytes in Kotlin?
What Java inexperienced people did to study Kotlin
Technical causes and countermeasures for points addicted to Android apps & Kotlin (3. Processing related to Android images)
Launch Docker from Java to convert Office documents to PDF
<java> Read Zip file and convert directly to string
Convert Java enum enums and JSON to and from Jackson
Sample code to convert List to List <String> in Java Stream
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
[Android / Java] Set up a button to return to Fragment
[Android Studio] [Java] How to fix the screen vertically
How to use Truth (assertion library for Java / Android)
Java applications convert Word (DOC / DOCX) documents to PDF
Introducing New Relic to Java apps running on Heroku
I want to transition screens with kotlin and java!
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
[Kotlin] Convert ZonedDateTime to String by specifying the format
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
How to set up Android OR mapper "Orma" (Kotlin)
[Java] Convert PDF version
Changes from Java 8 to Java 11