1.首先在setting>plugins下载两个插件Kotlin和Kotlin Extensions For Android,并且需要重启AS。
2.需要在project根目录的gradle文件中添加
dependencies {
classpath'com.android.tools.build:gradle:1.3.0'
classpath'org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.0-beta-1038'
}
3.在您开发的module根目录的gradle中需要加入
apply plugin:'kotlin-android'
buildscript{
repositories {
jcenter()
}
dependencies {
classpath'org.jetbrains.kotlin:kotlin-android-extensions:1.0.0-beta-1038'
}
}
dependencies {
compile fileTree(dir:'libs',include: ['*.jar'])
compile'com.android.support:appcompat-v7:23.0.1'
compile'org.jetbrains.kotlin:kotlin-stdlib:1.0.0-beta-1038'
}
4.新建kotlin文件,通过import kotlinx.android.synthetic.<xml名字>.* 可以根据id直接获取相应的view
import android.app.Activity
import kotlinx.android.synthetic.activity_main.*
import android.os.Bundle
classMainActivity : Activity() {
override funonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
text_view.setText("890")//自动根据id找到相应的view
}
}