AndroidStudio配置kotlin

Kotlin出来有很长一段时间了,作为一个安卓开发党之前一直在使用java,当我接触到kotlin时,顿时就被它惊艳到了,原来代码可以如此简洁!工欲善其事必先利其器,废话少说,接下来我就给大家介绍一下如何在AndroidStudio配置支持kotlin:

首先,我使用的是AndroidStudio版本是3.4.1(老版本玩家能升级就升级吧,但要注意Gradle与项目的兼容性),AS3.0及以上版本内置了对kotlin的支持,如果你还在用3.0之前的版本,请看下面

打开File->Settings->Plugins,搜索"kotlin",找到插件,然后Install

重启后,File -> New 看到有Kotlin File/Class,说明插件安装成功

准备工作就绪,新建项目,到下面一步时,可以选择项目的语言,选择kotlin就会帮你配置好关于kotlin东西,这个时候你就已经可以使用kotlin来开发了;如果选java,进去项目后再来配置也可。(不同版本AS会有些差异,本教程基于3.4.1)

如果你新建项目选的语言是java或者你已有的旧项目使用的是java开发,那么接下来配置如下:

1.进入项目,选择Tool->Kotlin->Configure Kotlin in Project

2.选择所有Module一起配置还是单独某一个进行配置,下方是kotlin编译的版本

3.然后就会在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.新建一个kotlin文件,即可愉快地写kotlin的代码啦

5.对于Java文件,可以使用下面操作将java转成kotlin:

注意事项:

如果遇到Gradle无法下载kotlin依赖的,切换下网络或者调整下Gradle和kotlin的版本,我用过的一个比较稳定的版本配置如下:

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()
}

猜你喜欢

转载自blog.csdn.net/gs12software/article/details/96430598