谷歌I/O大会公布Kotlin成为了android的官方开发语言,并且有不少大牛说Kotlin的各种好,那就试试呗
1.首先是安装插件:File-Settings-Plugins搜索Kotlin,如果没有结果可以点击查找。
之后会弹出两个框选择Kotlin版本,选择相应的版本(一般会自动选择)直接ok。
2.安装完成之后在新建类是就会出现如图:
3.gradle配置,Kotlin插件加入以后gradle文件发生了改变,需要增加一些配置才能方便使用:
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.example.administrator.refreshrecycleview" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } buildscript { repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.0' testCompile 'junit:junit:4.12' compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" } repositories { mavenCentral() }4.Kotlin集成完毕使用时,注意导入
import android.os.Bundle import android.support.v7.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* /** * Created by Administrator on 2017/6/9. */ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) initView() initData() } private fun initView() { change_btn.setOnClickListener { main_tv.text = "Hello Kotlin" } } private fun initData() { } }activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/main_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/change_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/main_tv" android:text="change" /> </RelativeLayout>main_tv是TextView的id,再也不用findviewById了。