Java project refactored with kotlin

1. Using the app developed by Android studio, the shortcut key on the mac is command+A. This can be set freely. You can go to Prefrences-> keymap -> search for find action or option name to view the corresponding shortcut key. Open the window: After finding action or option name, enter convert java file to kotlin file.  


2. After clicking confirm, the current java class will be automatically converted to the file ending in kt. Of course, there will be many errors that will become popular, and we need to change it manually. Writing this article is mainly to record the problems that I need to pay attention to after refactoring in this way:

The first question: Kotlin has strict definitions for the case where the object is null. During the coding phase, it is necessary to use code to indicate whether the reference can be null. The case of null requires mandatory judgment and processing. E.g. statement

var source : String? = null

here? That means the source value can be empty

Or lateinit var source: String 

The lateinit here refers to the initialization later. When the programmer writes the code, it is clear when the object has a value and when it needs to be judged whether it is empty. 

Not only variable declarations, but also the parameter declaration type in the method to tell whether it is nullable. For example, if a parameter that is not null is passed in, it may be null, and it needs to be added! ! Tell that this value is definitely not empty, if! ! If the previous parameter is empty, a kotlinNullPointerException will be reported.

Second question: Regarding the use of butterknife in the Kotlin language, there are also many solutions on the Internet, I use it directly

kotlin-android-extensions
, Note that when using it, you need to override the onCreateView to accurately pass in the layout in the Fragment, and you can directly use the component operation initialization in OnViewCreated, otherwise it will report an error that the component cannot be found. Kotlin also provides accurate solutions in ViewHolder to implement the following interfaces

import kotlinx.android.extensions.LayoutContainer
Pass in the corresponding layout View in the constructor of ViewHolder to find the corresponding component.

The third question: When inheriting DialogFragment, you also need to override the method in the second question above. Of course, it can also be directly used in the OnCreateDialog method to manually find the component and operate it with the passed view. Another thing to say is that OncreateView cannot be overwritten when using metarialDialog. An error will be reported in the onActivityCreated method. MetarialDialog does not support the setContentView method. You need to manually find and initialize the component in the onCreateDialog method.

The fourth question: I am using AS version 3.0.1, gradle is 3.0.1, the kotlin plugin is currently the latest version 1.2.21, there are some problems to downgrade gradle reported on the Internet, I chose to upgrade, kotlin The latest version solves gradle-related issues, and it’s fairly smooth. During the period, GC OOM-related issues appeared, which was added to build.gradle 

androidExtensions {
    experimental = true
}

The fifth question: When declaring a variable of non-val type in Kotlin, the compiler will provide setter and getter methods by default. On the other hand, there is a method setData in Java, and converting to Kotlin is just a declaration of a variable instead of a method. This problem causes a lot of problems during interface conversion, so before conversion, the methods that need to be overwritten and implemented are checked to see if the beginning of set and get needs to be changed to something else.

In the beginning, it was not easy to have many problems, but now I can only remember a few in retrospect. The first time I wrote it, I roughly explained the following, as a record. If you want to write more, it will be more detailed. Follow-up questions will be added.

Guess you like

Origin blog.csdn.net/guoyingmiss/article/details/79279798