Developing Android Apps with Kotlin

Source: Internet
Author: User

1, Kotlin Introduction
 [Kotlin](https://kotlinlang.org/) Kotlin是一门基于JVM的编程语言,它正成长为Android开发中用于替代Java语言的继承者。Java是世界上使用最多的编程语言之一,当其他编程语言为更加便于开发者使用而不断进化时,Java并没有像预期那样及时跟进。

Many of the features that are missing from Java are gradually being overwritten in the latest revisions, but Android developers are not yet able to use them. This makes language like Kotlin useful: Use the features of modern languages in the old development environment.

What is Kotlin?

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

Kotlin is born to make up for the features of modern languages that are missing from Java, and greatly simplifies the code, allowing developers to write as few boilerplate code as possible.

2. Configuration Engineering

Create a new project "Mykotlin"
Add the Kotlin version number on the Build.gradle of the project

sub-projects/modules.ext.kotlin_version = "1.0.0-rc-1036"buildscript {    repositories {        jcenter()    }    dependencies {        ‘com.android.tools.build:gradle:1.3.0‘        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}

Adding Kotlin dependencies to Build.gradle in module

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

Add support for Android

‘com.android.application‘‘kotlin-android‘‘kotlin-android-extensions‘

Add the Java compilation path, create the Kotlin directory under Main and write 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 atBuildtoolsversion"23.0.1"Defaultconfig {ApplicationID"Com.example.mykotlin"Minsdkversion -Targetsdkversion atVersioncode1Versionname"1.0"} buildtypes {release {minifyenabledfalseProguardfiles 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 ...
The resulting file kt is then cut into the Kotlin directory.
KT code is as follows

Packagecom. 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:

4, example download

Click to view

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.