[Android]使用Kotlin+Anko开发Android(一)


Kotlin是由JetBrains开发并且开源的静态类型JVM语言。比Java语言语法简洁,支持很多Java中不支持的语法特性,如高阶函数、內联函数、null安全、灵活扩展、操作符重载等等。而且它还完全兼容Java,与Scala类似,但是Scala的宗旨是“尽可能自己实现,不得已才使用Java”,而Kotlin却相反:“尽可能复用Java的实现,不得已才自己实现”。所以相比之下Kotlin更简洁轻量,非常适合移动端的开发。另外JetBrains针对Android开发提供了一个由Kotlin实现的“anko”开源库,可以让你使用DSL的方式直接用代码编写UI,让你从繁琐的xml中解脱出来,而且避免了xml解析过程所带来的性能问题。

 

这篇先讲怎么去使用idea(Android Studio用户也一样)搭建Kotlin的Android开发环境。

 

一、下载以下相关idea插件:

1. Kotlin

2. Kotlin Extensions For Android

3. Anko DSL Preview

其中Anko DSL Preview插件用于预览使用DSL编写的UI代码,就像以前使用xml编写UI文件时可以动态在“Preview”窗口预览效果一样。

 

二、新建Android项目

在src/main目录下,新建kotlin目录(用于放置kotlin代码),配置Gradle如下:


 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 22
21 buildToolsVersion “22.0.1”
22
23 defaultConfig {
24 applicationId “com.wangjie.androidwithkotlin”
25 minSdkVersion 9
26 targetSdkVersion 22
27 versionCode 1
28 versionName “1.0”
29 }
30
31 sourceSets {
32 main.java.srcDirs += ‘src/main/kotlin’
33 }
34
35 buildTypes {
36 release {
37 minifyEnabled false
38 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 }

然后sync & build。

 

三、配置Kotlin

调用“Configuring Kotlin in the project”这个Action

 

四、把Java代码一键转成kotlin代码

打开要转换的Java文件,调用“Convert Java File to Kotlin File”这个Action即可。

转换之前的Java代码:


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

转换之后的Kotlin代码:


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

 



来源博客:Wang Jie's Blog's Blog
本文链接:https://blog.wangjiegulu.com/2015/09/11/Android-使用Kotlin-Anko开发Android(一)/
版权声明:本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处。