Android Studio 配置Kotlin详解

作者: ChangQin | 来源:发表于2017-05-18 23:46 被阅读1029次

    插件安装

    安装这个kotlin插件


    Paste_Image.png
    1. project的build.gradle配置
    1. buildscript新增ext.kotlin_version = '1.1.2-4'
    2. dependencies新增classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    3. allprojects新增mavenCentral()
    1. module的build.gradle配置
    1. 新增插件支持
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    1. android新增
     sourceSets {
            main.java.srcDirs += 'src/main/kotlin'
        }
    
    1. dependencies新增
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    

    总配置

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
        ext.kotlin_version = '1.1.2-4'
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.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 {
            jcenter()
            mavenCentral()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        defaultConfig {
            applicationId "com.happy.kotlindemo"
            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"
    }
    

    所有都配置好了之后,选择一个Activity文件,选择code->convet java file...

    Paste_Image.png

    大功告成

    Paste_Image.png

    相关文章

      网友评论

      • 0b4a8a69f146:安静的等AS-3.0
        0b4a8a69f146:@ChangQin canary(金丝雀),预览版
        https://developer.android.com/studio/preview/index.html?hl=zh-cn
        ChangQin:@Herve_Lee 好像已经有一个3.0什么版本来着,,,忘了
      • pianoboyfans:安装好插件sync一下就自动配好了
        pianoboyfans: @ChangQin 我也是
        ChangQin:@pianoboyfans 嘿嘿,不知道哎,刚上手这门语言,正在学习那:joy:

      本文标题:Android Studio 配置Kotlin详解

      本文链接:https://www.haomeiwen.com/subject/pgyexxtx.html