2.Kotlin项目创建

1.通过 create new project 新建一个项目

直接next就可以

2.在项目的 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:1.5.0'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

通过,变量kotlin_version等进行控制kotlin的版本

3.在module 的build.gradle中配置

apply plugin: 'com.android.application'
// 使用Kotlin插件
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.xdf.wei.weatherapp"
        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 {
    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 "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.anko:anko-common:$anko_version"
    compile "com.android.support:appcompat-v7:$support_version"
    compile "com.android.support:recyclerview-v7:$support_version"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

其中的anko-common是部分的anko,而 anko是简化了android任务的的一个kotlin强大的库。

4.由于刚才新建的时候,MainActivity是java类,可以通过,Code->Convert java File to kotlin File,将MainActivity转为kotlin的代码。


转成功后的代码显示为


之后直接启动应用,就可以测试是否已经成功建立一个kotlin项目了。

猜你喜欢

转载自blog.csdn.net/shuang__zi/article/details/77967759