The following content is original, welcome reprint, Reprint please specify
From every day blog: http://www.cnblogs.com/tiantianbyconan/p/4800656.html
Kotlin is a static type JVM language developed by JetBrains and open source. is more concise than the Java language syntax, supporting many of the syntax features that are not supported in Java, such as higher-order functions, inline functions, NULL security, flexible expansion, operator overloading, and so on. And it's completely Java-compatible, similar to Scala's, but Scala's aim is "to do as much as you can, to use Java", while Kotlin is the opposite: "Reuse Java as much as possible, and make it your own." So in contrast, Kotlin is more concise and lightweight, and is ideal for mobile development. In addition, JetBrains provides a "Anko" open Source Library for Android development, which allows you to write the UI directly in code using a DSL, freeing you from tedious XML and avoiding the performance problems of the XML parsing process.
This article first tells you how to use idea (also the same for Android studio users) to build the Kotlin Android development environment.
First, download the following related idea plugin:
1. Kotlin
2. Kotlin Extensions for Android
3. Anko DSL Preview
The Anko DSL preview plugin is used to preview the UI code written in the DSL, just as you can dynamically preview the effect in the "Preview" window as you previously wrote the UI file using XML.
Second, the new Android project
Under the Src/main directory, create a new Kotlin directory (for placing Kotlin code) with the following configuration gradle:
1 Buildscript {2ext.kotlin_version = ' 0.12.1230 '3 Repositories {4 mavencentral ()5 }6 Dependencies {7Classpath ' com.android.tools.build:gradle:1.1.1 '8Classpath "Org.jetbrains.kotlin:kotlin-gradle-plugin: $kotlin _version"9Classpath "org.jetbrains.kotlin:kotlin-android-extensions: $kotlin _version"Ten } One } AApply plugin: ' Com.android.application ' -Apply plugin: ' Kotlin-android ' - the Repositories { - mavencentral () - } - + Android { -Compilesdkversion 22 +Buildtoolsversion "22.0.1" A at Defaultconfig { -ApplicationID "Com.wangjie.androidwithkotlin" -Minsdkversion 9 -Targetsdkversion 22 -Versioncode 1 -Versionname "1.0" in } - to Sourcesets { +Main.java.srcDirs + = ' Src/main/kotlin ' - } the * Buildtypes { $ Release {Panax NotoginsengMinifyenabledfalse -Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro ' the } + } A } the + Dependencies { -Compile Filetree (dir: ' Libs ', include: [' *.jar ']) $Compile ' com.android.support:appcompat-v7:22.2.0 ' $Compile "Org.jetbrains.kotlin:kotlin-stdlib: $kotlin _version" -Compile "Org.jetbrains.kotlin:kotlin-reflect: $kotlin _version" -Compile ' org.jetbrains.anko:anko:0.6.3-15 ' the}
Then Sync & build.
Third, configuration Kotlin
Call the "Configuring Kotlin in the project" action
d. Turn Java code into Kotlin code with one key
Open the Java file you want to convert, and invoke the "convert Java file to Kotlin file" action.
Before converting the Java code:
Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. intID =Item.getitemid (); //noinspection simplifiableifstatement if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
The Kotlin code after the conversion:
Public classmainactivity:actionbaractivity () {Override fun OnCreate (Savedinstancestate:bundle?) { Super. OnCreate (savedinstancestate) Setcontentview (R.layout.activity_main)} override fun Oncreateoptionsmenu (m Enu:menu?): Boolean {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu)return true} override Fun onoptionsitemselected (Item:menuitem?): Boolean {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.Val id = item!!. Getitemid ()//noinspection simplifiableifstatement if(id = =r.id.action_settings) { return true } return Super. onoptionsitemselected (Item)}}
[Android] using Kotlin+anko to develop Android (i)