Android Kotlin初体验

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z2464342708m/article/details/74033788

在今年的google大会上,Kotlin被google推为官方语言了,但是一直没去体验koltin的优势,这几天正好有空,于是乎,就动手搭建了一个小demo。在编写demo的过程中,看了一些博客,但是,觉得写得不是很清晰,所以,自己打算写一篇笔记来留下自己的脚印。
创建项目和创建Android项目一样一样的:
1、设置项目名称:
这里写图片描述
2、选择Android版本:
这里写图片描述
3、为项目选择一个activity:
这里写图片描述
4、设置activity的名字:
这里写图片描述
到这里,Android项目已经创建完成了,代码大概是这样的:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

接下来就是转换Koltin项目了,使用快捷键ctrl+shift+alt+G,转换java代码,转换之后的代码就成这样了:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

但是好像和官网上说的有点不一样,官网上说转换代码之后会有一个Configure配置Koltin:
这里写图片描述
但是我没看到啊,是不是感觉被google坑了?别理我,我先默默地哭一会……..
别着急啊,先build一下看看,咦,好像,貌似有这个Configure,看来我们错怪google了。
配置完成之后,在项目的build.gradle中配置已经自动修改了:

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

app下的build.gradle自动修改成:

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

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.morse.koltintest"
        minSdkVersion 15
        targetSdkVersion 26
        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:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
    mavenCentral()
}

项目是构建好了,但是kotlin compile下载也太慢了吧。
等Kotlin compile下载完成之后,给textview设置一个文本,直接使用textview的id作为控件名称,设置文本就ok。但是在设置文本的过程中,怎么设置都会报错,提示没有textview这个资源,而且

kotlinx.android.synthetic.main.activity_main.*

这行代码页报错了,咋整呢?

Every Android developer knows well the findViewById() function. It is, without a doubt, a source of potential bugs and nasty code which is hard to read and support. While there are several libraries available that provide solutions to this problem, being libraries dependent on runtime, they require annotating fields for each View.

The Kotlin Android Extensions plugin allows us to obtain the same experience we have with some of these libraries, without having to add any extra code or shipping any additional runtime.

这是官网的解释,在使用kotlin开发android项目时,每一个控件都是作为activity的一个属性展现在用户面前的,但是这个属性不能直接去获取,需要借助kotlin的一个拓展插件才能正常使用,在app目录下的build.gradle中添加kotlin插件的应用就OK了:

apply plugin: 'kotlin-android-extensions'

修改后的源码:
MainActivity:

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tv.setText("hello kotlin")
    }
}

项目下的build.gradle:

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

app目录下的build.gradle:

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

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.morse.koltintest"
        minSdkVersion 15
        targetSdkVersion 26
        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:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
    mavenCentral()
}

Kotlin傻瓜式入门教程到这里就完了,希望对各位同学有所帮助。

猜你喜欢

转载自blog.csdn.net/z2464342708m/article/details/74033788