Goal
This article is intended to guide developers to use Kotlin to develop Android apps.
As for the grammar and tutorials of the Kotlin language, which are not covered in this article, please refer to the following official website documentation and online development tutorials.
- Kotlin-android
- "Kotlin for Android Developers" Chinese translation
- Kotlin-in-chinese
- Kotlin Official Reference Document Chinese version
- Kotlin Official documents Chinese Translation version
Introduction noun explanation kotlin
Kotlin is a new JVM-based programming language developed by JetBrains.
Kotlin can be compiled into Java bytecode or compiled into JavaScript for easy running on devices without a JVM.
JetBrains, as the current provider of the popular Java IDE IntelliJ, has open source its Kotlin programming language under the Apache license.
Official website: http://kotlinlang.org/
GitHub Warehouse: Https://github.com/JetBrains/kotlin
Tutorial
This section describes how to develop Android apps using Kotlin.
Here are a few things to keep in mind:
- All Kotlin class files are suffixed with. Kt.
- The Kotlin source directory rules are the same as the default. They were placed in Src/main/kotlin, Src/test/kotlin, Src/androidtest/kotlin and arbitrary Src/${buildvariant}/kotlin.
Kotlin and Java
The use of Kotlin to develop Android requires several steps to configure.
1. Add the following code to the Build.gradle file in the project root directory:
buildscript { ext.kotlin_version = ‘1.0.1-2‘ repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:2.1.2‘ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }}
2. Add the following code to the Build.gradle file in the module directory:
apply plugin: ‘kotlin-android‘apply plugin: ‘kotlin-android-extensions‘dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}
3. The configuration is complete and you can use Kotlin to write Android apps happily in the Src/main/kotlin directory.
Examples show:
- Https://github.com/JetBrains/kotlin-examples
- Https://github.com/snowdream/test/tree/master/android/kotlin/HelloWorld
Java 2 Kotlin
Above is a manual add Kotlin support for Android projects.
In fact, there is an automatic conversion method, you can also add Kotlin support.
- Through the menu "Help | Find action or shortcut key "Ctrl+shift+a" to bring up the motion query window
- Enter Configure Kotlin in Project, enter, and follow the prompts to add the Kotlin configuration.
- Repeat the first step to bring up the action query window. Enter "Convert Java file to Kotlin file". You can automatically convert your existing Java files to a Kotlin file. Of course, if you just want to convert a Java file, open the Java file and choose the menu "Code | Convert Java file to Kotlin file so that the currently open Java files are automatically converted to Kotlin files.
- The conversion is complete.
Summarize
According to Kotlin's official website, Kotlin is a suitable jvm,android
Based on personal development practices, summarizes the pros and cons of using Kotlin to develop Android apps:
Advantages
- Compared to Java, it is more concise and more secure.
- Seamlessly integrated with Java, the official website claims that Kotlin can be mixed with 100% and Java.
- With the launch of JetBrains, idea can be better supported.
Disadvantages
- The associated jar packages that support Kotlin will be broken up and packaged into the APK. This part of the content will eventually increase the apk size by about 700k. This is a much better situation than the previous groovy, barely acceptable.
- There are too few developers using Kotlin compared to Java.
- The birth time is later, waiting for the test of time.
Conclusion
- Using Kotlin is a faster and more efficient way to develop Android apps.
- Before applying to production practice, more evaluations are needed, including stability, operating efficiency, power consumption, compatibility, acceptance of research and development, etc.
Reference
- Kotlin noun explanation
- Kotlin official website
- Kotlins-android-roadmap
- Getting started with Android and Kotlin
- Kotlin Android Extensions
- Kotlin-examples
- HelloWorld
- How to evaluate Kotlin language
- Swift, Kotlin:android World
- Kotlin
- What pits have you encountered in Android development using Kotlin?
Developing Android Apps with Kotlin