以下内容为原创,欢迎转载,转载请注明
来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4800656.html
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如下:
buildscript {
ext.kotlin_version = '0.12.1230'
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' repositories {
mavenCentral()
} android {
compileSdkVersion 22
buildToolsVersion "22.0.1" defaultConfig {
applicationId "com.wangjie.androidwithkotlin"
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0"
} sourceSets {
main.java.srcDirs += 'src/main/kotlin'
} buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} 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'
}
然后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)
}
}