android studio3.0 kotlin配置Anko

1.在project的build gradle中引入 ext.anko_version = ‘0.10.0-beta-2’

buildscript {
    ext.kotlin_version = '1.1.2'
    ext.anko_version = '0.10.0-beta-2'

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.4.0-alpha7'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "http://dl.bintray.com/kotlin/kotlin-dev" }
    }
}

2.在module的build gradle中引入anko包

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "org.example.ankodemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }

    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'])
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.anko:anko-sdk25:$anko_version"
    compile "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
    compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
}

kotlin {
    experimental {
        coroutines "enable"
    }
}

详见:https://github.com/Kotlin/anko-example
注意:
配置完后出现的错误:Error:Failed to resolve: com.google.android:android:2.3.1
目前发现在Android studio 3.0版本使用 gradle:3.0.0版本时会出现这个问题,貌似只能等待Anko给一个alpha版本解决这个问题。或者降低gradle版本号(我并没有测试)。
详见:https://github.com/Kotlin/anko/issues/381
https://youtrack.jetbrains.com/issue/KT-18103

你可能感兴趣的:(Anko,Kotlin)