好久不写博客了,新的一年,现在疫情已经被打倒,为了迎接疫情成功的号角,写一篇博客庆祝一下,这篇博客主要讲解kotlin的基本使用,主要是个人理解,如有问题,欢迎一起讨论和学习。
一:安装kotlin插件
1: 首先如果是新创建的项目,只需要选择kotlin即可,如下图所示。
2:另一种情况是现在已经选择java的项目,进行支持kotlin。首先安装kotlin插件。如下图所示:
在项目的build.gradle里面这样修改
ext.kotlin_version = '1.1.2'//定义当前版本,方便后来引用
ext.anko_version = '0.10.0-beta-2'//定义当前版本,方便后来引用
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
maven { url "http://dl.bintray.com/kotlin/kotlin-dev" }
}
}
在app的build.gradle里修改如下几个地方
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
这就是基本的配置和使用了,欢迎大家使用kotlin大法,kotlin大法好。