Android and Kotlin

本文介绍了如何在Android Studio中安装Kotlin插件,并详细解释了如何将Java代码转换为Kotlin代码。此外,还提供了关于如何配置项目以支持Kotlin的指导,包括构建文件的更改。特别地,本文深入探讨了Kotlin Android Extensions插件的使用方法及其背后的原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://kotlinlang.org/docs/tutorials/kotlin-android.html

1,Installing the Kotlin plugin

AndroidStudio3.0以后自带Kotlin插件。3.0以前的需要自己安装插件。
安装方法:File | Settings | Plugins | Install JetBrains plugin
搜索kotlin,安装后重启as。 我的as目前版本:2.3.3

这里写图片描述

然后就可以创建项目,创建kotlin Activity或者Kotlin File/Class,创建完需要配置kotlin,看第三点。

这里写图片描述

2,Converting Java code to Kotlin
将java文件转化为kotlin文件
1,快捷键:
1-1 ctrl+shift+a,然后输入Convert Java File to Kotlin File
1-2 ctrl+alt+shift+k

这里写图片描述
2,menu菜单 code|Convert Java File to Kotlin File
这里写图片描述

3,Configuring Kotlin in the project
当你使用了kotlin之后,as就会告诉你需要配置kotlin。
1,Meun菜单 Tools | Kotlin | Configure Kotlin
2,
这里写图片描述

进一步选择是所有的modules还是单个module

这里写图片描述

当你配置完之后,build文件会发生改变
Now you can see that apply plugin: ‘kotlin-android’ and the kotlin-stdlib dependency were added.

创建了Kotlin Activity之后,会让你配置project,整体项目变化:
1,Module的build文件:

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

android {
}
dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
    mavenCentral()
}

2,整个project的build文件

buildscript {
    ext.kotlin_version = '1.1.3-2'

    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    }
}

这两个jar下载有点慢
这里写图片描述

最后:jar包的大小大概859KB(1.1.3-2版本)
Kotlin has a rather small runtime file size: the library is approximately 859KB (as of 1.1.3-2). This means Kotlin adds just a little to .apk file size.


Kotlin Android Extensions

kotlin扩展插件,高效率开发Android项目

背景:findViewById(),每个Activity都有该方法。一些第三方的库,可以通过注解的方式来寻找每一个view。

kotlin插件也可以达到同样的效果,不需要额外的代码。

来看一个例子:
textView就是activity_main.xml中控件的id

// Using R.layout.activity_main from the main source set
import kotlinx.android.synthetic.main.activity_main.*

class MyActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView.setText("Hello, world!")
        // Instead of findViewById(R.id.textView) as TextView
    }
}

Using Kotlin Android Extensions

1,配置依赖
在本module的build中配置:apply plugin: ‘kotlin-android-extensions’

apply plugin: 'kotlin-android-extensions'

需要说明Android extensions 是 kotlin插件的一部分,你不需要做额外的操作。

2,导入(activity中使用和adapter中使用)

针对在activity中使用的导入

import kotlinx.android.synthetic.main.activity_main.*

如果是在adapter中使用

If we want to call the synthetic properties on View (useful in adapter classes), we should also import 
kotlinx.android.synthetic.main.activity_main.view.*.

原理:构造方法传入Activity实例,然后再去调用

eg:
Activity中的控件

 <TextView
        android:id="@+id/hello"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, MyActivity"
        />

adapter中调用(activity为构造方法传入的对象)

activity.hello.setText(“Hi!”)
activity!!.tv_hello.setText(“name”);


Android Flavors中使用

android {
    productFlavors {
        free {
            versionName "1.0-free"
        }
    }
}

这样导入即可:

import kotlinx.android.synthetic.free.activity_free.*

Under the hood,底层原理

Kotlin Android Extensions是kotlin编译器的一个插件,她只做两件事情
1,Adds a hidden caching function and a field inside each Kotlin Activity. The method is pretty small so it doesn’t increase the size of APK much.

2,Replaces each synthetic property call with a function call.

当我们去调用一个合成属性的时候,他的接受者activity或者fragment就回去调用该缓存方法(改方法是隐藏的)。

class MyActivity : Activity()
fun MyActivity.a() { 
    this.textView.setText(“”)
}

但是,However in the following case:

fun Activity.b() { 
    this.textView.setText(“”)
}

We wouldn’t know if this function would be invoked on only Activities from our sources or on plain Java Activities also. As such, we don’t use caching there, even if MyActivity instance from the previous example is the receiver.

但是如果一个方法被调用,我们不知道该方法来自哪个明确的activity,我们是不调用缓存。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值