Android Kotlin的使用和第一个项目HelloKotlin

Google在今天发布将Kotlin设为Android开发的第一语言,紧接着又推出了AndroidStudio3.0来支持Kotlin。我们可以看到,在未来Kotlin的崛起应该是必然的了,今天自己研究了一点,先发个最基础的:Kotlin的使用和第一个项目HelloKotlin。


1.如果你使用了AndroidStudio3.0那么这一步就不用看了。使用其他版本AndroidStudio的首先要在设置里下载Kotlin的Plugin。


2.下载之后重启AS,然后我们来创建一个新的工程,就叫HelloKotlin吧(和以前的创建工程一样)。

打开MainActivity.java文件。然后调用action(Ctrl+Shift+A)在action中输入"convert java File to Kotlin File"将Java文件转换为Kotlin文件,就像下面这样


转换后,就是这个样子的了


然后在代码中随便编辑一下,会在右上角出现一个提示Kotlin not configured,然后点击Configure

然后提示选择Kotlin版本和需要在哪些module中使用,版本一般都是最新的,不需要改的话直接按确定就好


这时项目的build.gradle会变成这样

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        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()
    }
}

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



主程序的build.gradle会成这样


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

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.wzh.hellokotlin"
        minSdkVersion 21
        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 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
    mavenCentral()
}
然后点击同步按钮更新

更新之后代码就可以运行了。


kotlin中文帮助文档下载:http://download.csdn.net/detail/mouseking1993/9845895

本篇代码下载:http://download.csdn.net/detail/mouseking1993/9845914


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值