[Android] Using Kotlin + Anko to develop Android (1) and kotlinanko

Source: Internet
Author: User

[Android] Using Kotlin + Anko to develop Android (1) and kotlinanko

The following content is original. You are welcome to reprint it.

From Daily Blog: http://www.cnblogs.com/tiantianbyconan/p/4800656.html

 

Kotlin is a static JVM language developed and open-source by JetBrains. Compared with Java, the syntax is concise and supports many syntax features not supported in Java, such as higher-order functions, internal functions, null security, flexible extension, and operator overloading. It is also fully compatible with Java, similar to Scala, but Scala's purpose is to "implement it as much as possible, so you have to use Java", while Kotlin is opposite: "try to reuse Java Implementation as much as possible, ". Therefore, Kotlin is more concise and lightweight, and is very suitable for mobile development. In addition, JetBrains provides an "anko" open-source library implemented by Kotlin for Android development, allowing you to use DSL to directly write the UI using code, this frees you from the tedious xml and avoids the performance problems caused by the xml parsing process.

 

This article describes how to use idea (same for Android Studio users) to build a Kotlin Android development environment.

 

1. download the following idea plug-ins:

1. Kotlin

2. Kotlin Extensions For Android

3. Anko DSL Preview

The Anko DSL Preview plug-in is used to Preview the UI code written in DSL, just as you can dynamically Preview the UI file in the "Preview" window when using xml to write the UI file.

 

2. Create an Android Project

Under the src/main directory, create the kotlin directory (used to place the kotlin code) and configure Gradle as follows:

 1 buildscript { 2     ext.kotlin_version = '0.12.1230' 3     repositories { 4         mavenCentral() 5     } 6     dependencies { 7         classpath 'com.android.tools.build:gradle:1.1.1' 8         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 9         classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"10     }11 }12 apply plugin: 'com.android.application'13 apply plugin: 'kotlin-android'14 15 repositories {16     mavenCentral()17 }18 19 android {20     compileSdkVersion 2221     buildToolsVersion "22.0.1"22 23     defaultConfig {24         applicationId "com.wangjie.androidwithkotlin"25         minSdkVersion 926         targetSdkVersion 2227         versionCode 128         versionName "1.0"29     }30 31     sourceSets {32         main.java.srcDirs += 'src/main/kotlin'33     }34 35     buildTypes {36         release {37             minifyEnabled false38             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'39         }40     }41 }42 43 dependencies {44     compile fileTree(dir: 'libs', include: ['*.jar'])45     compile 'com.android.support:appcompat-v7:22.2.0'46     compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"47     compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"48     compile 'org.jetbrains.anko:anko:0.6.3-15'49 }

Then sync & build.

 

3. Configure Kotlin

Call the Action "processing ing Kotlin in the project"

 

4. one-click conversion of Java code into kotlin code

Open the Java File to be converted and call the Action "Convert Java File to Kotlin File.

Java code before conversion:

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    public boolean onCreateOptionsMenu(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 boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

The converted Kotlin code:

public class MainActivity : ActionBarActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)    }    override fun onCreateOptionsMenu(menu: 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 will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        val id = item!!.getItemId()        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true        }        return super.onOptionsItemSelected(item)    }}

 

Related Article

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.