Use Kotlin to develop Android applications

Source: Internet
Author: User

Use Kotlin to develop Android applications
1. Introduction to Kotlin

[Kotlin] (https://kotlinlang.org/) Kotlin is a JVM-based programming language that is becoming a successor in Android Development to replace the Java language. Java is one of the most widely used programming languages in the world. When other programming Languages evolve to make it easier for developers to use, Java does not follow up as expected.

Many features missing from Java are gradually covered in the latest revision, but Android Developers have not yet been able to use them. This makes a language like Kotlin useful: the use of modern language features in the old development environment.

What is Kotlin?

Kotlin is a JVM-based programming language created by JetBrains. IntelliJ is the masterpiece of JetBrains, and Android Studio is modified based on IntelliJ. Kotlin is an object-oriented programming language that contains many functional programming ideas.

Kotlin was born to make up for the features of modern languages missing from Java and greatly simplify the code so that developers can write as few sample code as possible.

2. configuration project

Create a Project "MyKotlin"
Add the kotlin version number to build. gradle of the project.

// Top-level build file where you can add configuration options common to all sub-projects/modules.ext.kotlin_version = "1.0.0-rc-1036"buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:1.3.0'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        jcenter()    }}task clean(type: Delete) {    delete rootProject.buildDir}

Add kotlin dependency to build. gradle in module

buildscript {    repositories {        jcenter()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"    }}

Add support for android

apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'

Add the java compilation path, create the kotlin directory under main, and write it in the gradle File
The complete build. gradle is as follows:

buildscript {    repositories {        jcenter()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"    }}apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'android {    compileSdkVersion 23    buildToolsVersion "23.0.1"    defaultConfig {        applicationId "com.example.mykotlin"        minSdkVersion 14        targetSdkVersion 23        versionCode 1        versionName "1.0"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    sourceSets {        main.java.srcDirs += 'src/main/kotlin'    }}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:23.1.1'    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}

Code

3. Code Conversion

You can select MainActivity and select code> Convert Java File to Kotlin File...
Then, cut the generated file kt to the kotlin directory.
The kt code is as follows:

package com.example.mykotlinimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        tv.text = "Hello MyKotlin"        tv.textSize = 20.0f    }}

Final run:

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.