寫在前面的話
- 好久都沒有在簡書上寫文章了杈女,前段時(shí)間一直沒有空閑,最近才有時(shí)間整理這段時(shí)間的所得吊圾。
- 自從 Google I/O 大會(huì)上宣布 Kotlin 成為 Android 開發(fā)一級語言达椰,Kotlin 這門語言也越來越火了,理所當(dāng)然的项乒,筆者的項(xiàng)目也要開始向 Kotlin 轉(zhuǎn)移了啰劲,接下來呢,還是以項(xiàng)目 KotlinTest Github: 為例板丽,一邊學(xué)習(xí) Kotlin 語言呈枉,一邊搭建項(xiàng)目框架。
開發(fā)環(huán)境
-
要想使用 Kotlin 開發(fā)埃碱,首先當(dāng)然是準(zhǔn)備好開發(fā)工具了猖辫,你可以選擇使用你當(dāng)前使用的正式版 Android Studio,只是需要在 File -> Settings... -> Plugins -> Install JetBrains plugin... 中搜索 kotlin砚殿,安裝重啟即可啃憎。
image.pngimage.png 你也可以下載使用 Android Studio 3.0 測試版, 其中已經(jīng)默認(rèn)支持 Kotlin 開發(fā)似炎,不需要再單獨(dú)下載 Kotlin 插件辛萍。當(dāng)然了,如果你還在使用 Eclipse 進(jìn)行開發(fā)羡藐,你也可以找到支持的 kotlin 插件蘸嘶,這里筆者就不詳細(xì)說明了。
項(xiàng)目中使用Kotlin配置
-
欲練神功躯砰、练链、、不對,要想在項(xiàng)目中使用Kotlin谆甜,那首先肯定得在gradle文件中進(jìn)行相關(guān)配置啦垃僚。當(dāng)然,如果你是用的是 Android Studio 3.0 那就比較簡單了规辱,只需要在創(chuàng)建項(xiàng)目時(shí)勾選上 Include Kotlin support 選項(xiàng)就可以了谆棺,項(xiàng)目生成時(shí)會(huì)自動(dòng)添加上 Kotlin 支持。像這樣:
創(chuàng)建時(shí)支持Kotlin 如果你使用的是正式版的罕袋,你就需要在項(xiàng)目創(chuàng)建后改淑,手動(dòng)添加上相關(guān)支持了。像這樣:
-
project.gradle
buildscript { ext.kotlin_version = '1.1.51' // Kotlin 插件版本號炫贤,設(shè)置中查看 repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-rc1' 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 { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
插件版本號 這樣:
-
module.gradle
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { applicationId "com.wj.kotlintest" 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 { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation "com.android.support:appcompat-v7:26.1.0" implementation 'com.android.support:recyclerview-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" }
這樣溅固,Kotiln 的配置就完成了,如果使用的 AS 3.0兰珍,那么你會(huì)發(fā)現(xiàn)新建的項(xiàng)目中侍郭,MainActivity 默認(rèn)使用的就是 Kotlin 語言。
-
MainActivity.kt
class Main2Activity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) } }
當(dāng)然了掠河,如果你是用的是正式版亮元,那么在安裝完 Kotlin 插件后,你可以使用 Code -> Convert Java File to Kotlin File 將 Java 代碼轉(zhuǎn)換成 Kotlin 代碼唠摹。
最后
- 就這樣爆捞,Kotlin 的配置就這么說完了,接下來我還會(huì)繼續(xù)更新 Kotlin 中的相關(guān)語法勾拉、新特性以及 MVP 框架實(shí)現(xiàn)煮甥,歡迎關(guān)注~