Kotlin安卓开发环境搭建

添加依赖

首先把需要的插件安装完毕

方式一
Ctrl+Alt+A可以帮助您快速找到所需的操作,而无需浏览菜单和工具栏,输入所需的操作 Config Kotlin,选择Config Kotlin in Project,
选择Android 、Gradle,选择app module,工程就配置完成。

Kotlin安卓开发环境搭建_第1张图片
Paste_Image.png

方式二(手动配置build.gradle)
因为用到了kotlin其他一些功能,所以配置比上面多一点点。
根build.gradle

buildscript {
    ext.support_version = '23.1.1'
    ext.kotlin_version = '1.1.2'
    ext.anko_version = '0.8.2'

    repositories {
        jcenter()
    }
    dependencies {

        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions: $kotlin_version"
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app的 build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.xx.test"
        minSdkVersion 14
        targetSdkVersion 14
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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:26.+'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support:support-vector-drawable:26.+'
    testCompile 'junit:junit:4.12'

    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.anko:anko-common:$anko_version"
}

转换kotlin 到Java代码

  1. Ctrl+Alt+A (Find Action),在查找框输入使用 Convert Java File to Kotlin File 命令。
  2. 打开需要转换的Java文件,然后Ctrl+Alt+Shift+K即可
  3. 把Java代码直接复制到kotlin文件中,会自动转换,只能从java转换到kotlin。IntelliJ具有自动从Java转换成Kotlin的功能,但是它经常会产生不正确的代码,故最好还是熟悉kotlin语法。

你可能感兴趣的:(Kotlin安卓开发环境搭建)