Developing Android Apps with Kotlin

Source: Internet
Author: User

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.

    1. Kotlin-android
    2. "Kotlin for Android Developers" Chinese translation
    3. Kotlin-in-chinese
    4. Kotlin Official Reference Document Chinese version
    5. 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:

    1. All Kotlin class files are suffixed with. Kt.
    2. 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:

    1. Https://github.com/JetBrains/kotlin-examples
    2. 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.

    1. Through the menu "Help | Find action or shortcut key "Ctrl+shift+a" to bring up the motion query window
    2. Enter Configure Kotlin in Project, enter, and follow the prompts to add the Kotlin configuration.
    3. 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.
    4. 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
    1. Compared to Java, it is more concise and more secure.
    2. Seamlessly integrated with Java, the official website claims that Kotlin can be mixed with 100% and Java.
    3. With the launch of JetBrains, idea can be better supported.
Disadvantages
    1. 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.
    2. There are too few developers using Kotlin compared to Java.
    3. The birth time is later, waiting for the test of time.
Conclusion
    1. Using Kotlin is a faster and more efficient way to develop Android apps.
    2. Before applying to production practice, more evaluations are needed, including stability, operating efficiency, power consumption, compatibility, acceptance of research and development, etc.
Reference
      1. Kotlin noun explanation
      2. Kotlin official website
      3. Kotlins-android-roadmap
      4. Getting started with Android and Kotlin
      5. Kotlin Android Extensions
      6. Kotlin-examples
      7. HelloWorld
      8. How to evaluate Kotlin language
      9. Swift, Kotlin:android World
      10. Kotlin
      11. What pits have you encountered in Android development using Kotlin?

Developing Android Apps with Kotlin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.