android studio 3.0中配置Kotlin

来源:互联网 发布:mac上不了网 编辑:程序博客网 时间:2024/05/22 07:50

一、创建工程:

Android Studio 3.0中绑定了Kotlin插件,打开android studio 3.0在新建工程的时候勾选“Include Kotlin support”选项。之后就按照创建java语言的安卓工程一样一直到finish并等待工程创建成功。

 

在新建完成的时,如下图:划红线处可能报红。此处很可能是您的sdk中的platfrorm-tools没有升级到新版本。

 

二、工程相关配置:

需要在您的模块中的build.gradle中加入如下红线依赖:

apply plugin'kotlin-android-extensions'

启用Android Extensions Gradle插件。(加入该依赖后控件ID就代表控件对象,具体相关原理这里不作解释)



 

三、完整的gradle配置如下:

app gradle:

apply plugin'com.android.application'
apply plugin'kotlin-android'
apply plugin'kotlin-android-extensions'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.xhx.kotlin.myapplication"
        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'
        }
    }
}

dependencies {
    implementation fileTree(dir'libs'include: ['*.jar'])
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group'com.android.support'module'support-annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:25.4.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}


MyApplication gradle:

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
        mavenCentral()
    }
}

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


四、Android Studio 3.0链接。

链接: http://pan.baidu.com/s/1eRI5EoE
密码:7xbd
温馨提示:复制链接进入可以下载!

 

原创粉丝点击