Migrating from Java to Kotlin

On May Google I / O, Google announced that First-Kotlin , Kotlin became the preferred language for Android developers, then Google continually for the relevant construction of Kotlin.

ARTICLE launch of the Google Kotlin Migration Guide content.

He began to migrate from Java to Kotlin

Google migration is recommended "by the point and the surface," including people with two levels of code:

Human dimension, within the team elected after a Kotlin "ambassador", he needs to do:

  • Learn Kotlin and become an expert

  • Development of Kotlin development process

  • Participate in code reviews to ensure the smooth implementation of the development process Kotlin

  • Company formation / study group within the team

  • Initiative to collect internal problems and to discuss with the developer community feedback

And the code level, you need to do:

  • Kotlin write tests first use (in this case the project is still in use Java)

  • Use Kotlin write new code

  • Update the existing code Code Kotlin

When the existing code update Kotlin code can be directly transferred Kotlin use Java tools provided by Android Studio, just select the "Code → Convert Java File to Kotlin File" menu to:

△ Java tools turn Kotlin

In addition, Kotlin of migration is not necessary to be achieved, developers can gradually migrate at your own pace. A project can contain both .java and .kt file, but not both in the same document also Kotlin Java, developers can use any Java library in Kotlin code.

Lint check △ Android Studio may be opened in the checking interoperability Kotlin

When you call in Kotlin in Java, there are several points to note:

  • And code marked @Nullable @NotNull

  • Use attribute prefix (getName (), setName (), isActive ())

  • Avoid using Kotlin in the reserved keywords (when, is, etc.)

更详细的 Kotlin 互操作指南可以查看 Android 开发者网站:

https://developer.android.google.cn/kotlin/interop

内容引用自:谷歌开发者 

Guess you like

Origin www.oschina.net/news/111189/migrating-from-java-to-kotlin
Recommended