Kotlin开发环境的配置

来源:互联网 发布:淘宝定制产品 编辑:程序博客网 时间:2024/06/11 05:32

新的产品开发中Android端已经完全使用Kotlin作为开发语言了,目前为止还没有发现什么不适的地方。所以今天我们将讲解一下如何在android Studio中配置Kotlin开发环境,算是为Kotlin讲解一下入门知识,后续会慢慢更新kotlin相关知识。

  • 什么是Kotlin?

这里暂时引用一下百度百科中对Kotlin的讲解:

Kotlin 是一个基于 JVM 的新的编程语言,由 JetBrains 开发。

Kotlin可以编译成Java字节码,也可以编译成JavaScript,方便在没有JVM的设备上运行。

JetBrains,作为目前广受欢迎的Java IDE IntelliJ 的提供商,在 Apache 许可下已经开源其Kotlin 编程语言。

  • Kotlin的特性

Kotlin是一种兼容Java的语言;

Kotlin比Java更安全,能够静态检测常见的陷阱。如:引用空指针;

Kotlin比Java更简洁,通过支持variable type inference,higher-order functions (closures),extension functions,mixins and first-class delegation等实现;

Kotlin可与Java语言无缝通信。这意味着我们可以在Kotlin代码中使用任何已有的Java库;同样的Kotlin代码还可以为Java代码所用;

Kotlin在代码中很少需要在代码中指定类型,因为编译器可以在绝大多数情况下推断出变量或是函数返回值的类型。这样就能获得两个好处:简洁与安全;

  • Kotlin相关网址

Kotlin官网 
http://kotlinlang.org

l Kotlin-github地址 
https://github.com/JetBrains/kotlin

  • 使用Android Studio下载Kotlin相关插件

第一步、执行 Settings -> plugins -> BrowseRepositories中搜索“Kotlin”


Kotlin插件安装成功后,执行new操作,会发现弹窗多出了两个Kotlin相关选项:

1)“KotlinFile/Class”:一般Kotlin类

2)“Kotlin Activity”:Activity类


此时证明安装成功。

第二步、为项目配置Kotlin


点击以上两个选项后会弹出如下:


此时可以选择Kotlin的版本,正常情况下以上流程就可以了

配置完成之后在项目的跟build.gradle中:

buildscript {    ext.kotlin_version = '1.0.4'    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.2.2'        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    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

会多出这样两样代码,而在module的build.gradle中:

apply plugin: 'com.android.application'apply plugin: 'kotlin-android'android {    ...}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    ...    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这样表示当前的module已经支持kotlin语言了


但是如果配置失败。可以尝试降低Kotlin的版本,然后重新sync一下。如果还不行请按照下面的配置来执行:

app 下的build.gradle

apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'buildscript {    repositories {        jcenter()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"    }}android {    compileSdkVersion 25    buildToolsVersion "26.0.1"    defaultConfig {        applicationId "com.ca.demo1"        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'        }    }    sourceSets {        main.java.srcDirs += 'src/main/kotlin'    }}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'    })    compile 'com.android.support:appcompat-v7:25.3.1'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    testCompile 'junit:junit:4.12'    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}repositories {    mavenCentral()}


a 下的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    ext.kotlin_version = '1.0.3'//此处,不要把Kotlin的版本选的太高,否则会失败    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.3.0'        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 {        jcenter()    }}task clean(type: Delete) {    delete rootProject.buildDir}

请对比此时你的项目下的和上面的配置的不同之处修改过来,一般百分之九十都是可疑的

如果不对欢迎指出谢谢