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 covered in the latest revisions, but Android developers are not yet able to use them. This makes the language of similar 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 changes. Kotlin is an object-oriented programming language that includes very 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 to 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}

Build.gradle in module joins Kotlin dependency

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

Add support for Android

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

Then add the Java compilation path. Create the Kotlin folder under Main and write on the Gradle file
The complete build.gradle such as the following

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"}

What the code sees

3. Code Conversion

Ability to select Mainactivity and select Code–>>convert Java file to Kotlin file ...
Then cut the resulting file kt into the Kotlin folder.
KT codes such as the following

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}}

Finally performed what was seen:

4. Sample 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.