Kotlin语言作为Google IO官方主推的开发语言,作为一个Andorid开发人员必须掌握的一门语言。
Kotlin集成AndroidStudio中环境配置
添加kotlin支持库,我已经安装了1.1.3,下载会比较慢。
(File->Setting->Plugins->搜索Kotlin->install支持库)
新建一个Android项目,添加Kotlin支持
添加支持有两种方式:
1.通过工具AndroidStudio配置Kotlin
(Tools->Kotlin->Configure Kotlin in Project)
选择(Configure Kotlin in Project)弹框选项,二级弹框选择 (Android with Gradle)
点击确定配置完成:
2.代码在项目中配置Kotlin
项目根目录配置build.gradle
buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
项目modle配置build.gradle
apply plugin: 'kotlin-android'
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
完整gradle配置:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.daycodeday.kotlin"
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'
}
}
}
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'
})
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
mavenCentral()
}
开始第一个MainActivity类
转换后变成MainActivity.kt文件
网友评论