Kotlin has been out for a long time. As an Android development party, I have been using java before. When I came into contact with kotlin, I was immediately amazed by it. It turns out that the code can be so concise! If you want to do a good job, you must first sharpen your tools. Let’s not talk nonsense. Next, I will introduce to you how to configure and support kotlin in AndroidStudio:
First of all, I am using AndroidStudio version 3.4.1 (you can upgrade the old version if you can, but pay attention to the compatibility between Gradle and the project), AS3.0 and above versions have built-in support for kotlin, if you are still For versions before 3.0, please see below
Open File->Settings->Plugins , search for "kotlin", find the plugin, and then Install
After restarting, File -> New sees Kotlin File/Class , indicating that the plugin is installed successfully
When the preparations are ready, create a new project. When you go to the next step, you can choose the language of the project. Selecting kotlin will help you configure things about kotlin. At this time, you can already use kotlin to develop; if you choose java, enter the project and come back Configuration is also available. (There will be some differences in different versions of AS, this tutorial is based on 3.4.1)
If the language you choose for your new project is java or your existing old project uses java development, then the next configuration is as follows:
1. Enter the project, select Tool->Kotlin->Configure Kotlin in Project
2. Choose whether to configure all Modules together or to configure a single one. Below is the version compiled by kotlin
3. Then the following configuration will be generated in build.gradle:
buildscript {
ext.kotlin_version = '1.3.41' //kotlin版本号
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" //kotlin编译插件
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
4. Create a new kotlin file, and you can happily write kotlin code
5. For Java files, you can use the following operations to convert java to kotlin:
Precautions:
If Gradle cannot download kotlin dependencies, switch the network or adjust the version of Gradle and kotlin. A relatively stable version I have used is configured as follows:
gradle.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.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
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 28
defaultConfig {
applicationId "cn.com.minstone.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"//kotlin
}
repositories {
mavenCentral()
}