Kotlin--Hello World

2017-07-30  本文已影响68人  OneBelowZero

放在前面的图

Kotlin吐槽

kotlin出来好早了 我记得15年还是16年 当时看过一阵子 入了个门 Hello World 简单的看了下语法 当时觉得和 C#好像啊 后来没怎么继续看只记得 好像不能弹吐司 最近这段时间特别火 特别是宣布成为android官方语言之后 尤其是Android Studio 3.0上默认支持Kotlin。我的是AS 是2.3版本 所以需要安装插件。

Android Studio环境搭建

执行 Settings -> plugins -> BrowseRepositories中搜索“Kotlin” 安装重启

project的build.gradle配置:

buildscript新增ext.kotlin_version = '1.1.2-4'
dependencies新增classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
allprojects新增mavenCentral()

module的build.gradle配置

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

为避免遗漏:
<code>

// 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.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        //Kotlin
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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

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

ext {
    buildToolsVersion = '25.0.3'
    supportLibVersion = '25.3.1'
    runnerVersion = '0.5'
    rulesVersion = '0.5'
    espressoVersion = '2.2.2'
    archLifecycleVersion = '1.0.0-alpha1'
    archRoomVersion = '1.0.0-alpha1'
}

</code>

<code>

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

android {
    compileSdkVersion 25
    buildToolsVersion rootProject.ext.buildToolsVersion
    defaultConfig {
        applicationId "com.kotlin.kotlindemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

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:${rootProject.ext.supportLibVersion}"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

</code>

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

感谢:
http://www.jianshu.com/p/a6f8dcd6a330
http://wuxiaolong.me/2017/05/21/kotlin1/
最近在写Kotlin的demo 还在学习ing

上一篇 下一篇

猜你喜欢

热点阅读