如下内容为原创,欢迎转载,转载请注明html
来自每天博客:http://www.cnblogs.com/tiantianbyconan/p/4800656.html java
Kotlin是由JetBrains开发而且开源的静态类型JVM语言。比Java语言语法简洁,支持不少Java中不支持的语法特性,如高阶函数、內联函数、null安全、灵活扩展、操做符重载等等。并且它还彻底兼容Java,与Scala相似,可是Scala的宗旨是“尽量本身实现,不得已才使用Java”,而Kotlin却相反:“尽量复用Java的实现,不得已才本身实现”。因此相比之下Kotlin更简洁轻量,很是适合移动端的开发。另外JetBrains针对Android开发提供了一个由Kotlin实现的“anko”开源库,可让你使用DSL的方式直接用代码编写UI,让你从繁琐的xml中解脱出来,并且避免了xml解析过程所带来的性能问题。android
这篇先讲怎么去使用idea(Android Studio用户也同样)搭建Kotlin的Android开发环境。安全
1、下载如下相关idea插件:app
1. Kotlinmaven
2. Kotlin Extensions For Androidide
3. Anko DSL Preview函数
其中Anko DSL Preview插件用于预览使用DSL编写的UI代码,就像之前使用xml编写UI文件时能够动态在“Preview”窗口预览效果同样。性能
2、新建Android项目gradle
在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。
3、配置Kotlin
调用“Configuring Kotlin in the project”这个Action
4、把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) } }