1. 程式人生 > >Kotlin 入門環境搭建

Kotlin 入門環境搭建

dex 好的 .html 分享 fin google 介紹 mod help

這次的Google I/O大會 ,一大熱點:Kotlin 被列為Android一級開發語言。

下面介紹如何在Android Studio中是用Kotlin.

1.下載Kotlin 3.0

Google 下載地址:https://developer.android.com/studio/preview/index.html

騰訊微雲下載地址:https://share.weiyun.com/542279b824eebfbcdff7ea3b3753ed9f

waiting.

waiting...

2.下載安裝完成,新建一個項目:

  技術分享

3.將Android代碼轉為Kotlin:

  在help 裏面選擇find action 或者 ctrl + shift + a快捷鍵,在搜索框中輸入 convert java file to kotlin

技術分享

轉換後的代碼:

技術分享

4.在項目中配置Kotlin

技術分享

選擇將所有module轉成kotlin

技術分享

app.gralde 變成如下:

技術分享

註意上圖中:apply plugin: ‘kotlin-android-extensions‘ 是手動添加上去的,這是幹什麽用的呢?我們知道Kotlin中一點很好的功能就是我們再也不用寫findviewbyid了,增加這個就是為了能直接使用view的id去做相關的操作:

技術分享 技術分享

如上圖所示,我們並沒有使用findviewbyid,但是能直接使用,是不是很歡喜? 如果不在gradle 中增加 apply plugin: ‘kotlin-android-extensions‘ 直接使用是會報錯的,在需要使用該功能的class下記得導入對應包:

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

以上就是 android stuido 3.0下面Kotlin的環境搭建了。

Kotlin 入門環境搭建