为什么要学习kotlin
1、想使用ViewPager2,但是在github上下载ViewPager2的例子的时候,居然是kotlin的。
2、想学习okhttp的源码,但是从github上下载okhttp的源码是kotlin写的。
基于上面的原因,必须要进行kotlin的学习。
kotlin开发环境配置
第一种办法:老牛拉车式的创建
1、添加kotlin的编译器插件
项目的build.gradle - buildscript - dependencies中添加如下内容,注意他们的位置
ext.kotlin_version = "1.4.10"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2、然后引入插件
app的build.gradle中
apply plugin: 'kotlin-android'
3、添加标准库
app的build.gradle中添加
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
第二种办法:方便快捷的创建
1、AS中创建项目,选择kotlin,项目会自动的创建一个项目
2、然后将项目中,以及app中里面的build.gradle中关于kotlin的相关配置拷贝过来就好。
kotlin语法
学习语法就上官网上进行学习