日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android开发:Kotlin下配置DataBinding

發(fā)布時間:2025/6/15 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开发:Kotlin下配置DataBinding 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

近日,隨著Google召開了Google I/O 2017,Kotlin大火一把。因為Google宣布Kotlin為First-class開發(fā)語言作 為一名Kotlin忠實粉絲,高興地很呀。雖然短
時間內不太可能替代Java,但這次官宣意味承認了Kotlin在Android開發(fā)中的合法地位,讓想嘗試Kotlin卻有顧率的開發(fā)者可以放心地使用Kotlin(比如說我)。
有人說沒必要嘗試Ktolin,Kotlin沒有什么吸引人的地方,相比java沒簡潔多少,只不是多一些語法糖而已。對我而言,我就是喜歡這些語法糖。當然了,此時也
應該回想回想Eclipse。Kotlin有諸如kotlin-android-extensions 以及Anko這種優(yōu)秀的插件或者庫,但是我也很偏愛Databinding。下面就講如何讓kotlin與databinding合諧并存

開發(fā)環(huán)境

AndroidStudio 2.3.2 Kotlin:1.1.2-3

安裝Kotlin插件

打開settings去plugin里面搜索kotlin,然后install就可以了。以在tools里面我們就可以查看kotlin了,在這里可以進行檢查更新什么的。
安裝完之后,可以雙擊shift然后彈出了一個對話框,在里面輸入

configure kotlin in project

然后可以選擇配置整個project還是單個Module。

gradle配置

經歷過以上步驟,你會發(fā)現你的Project下面的build.gradle變成這樣的了

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

而module的build.gradle是這樣的

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android {....dataBinding {enabled true} } dependencies {compile fileTree(include: ['*.jar'], dir: 'libs')androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {exclude group: 'com.android.support', module: 'support-annotations'})compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:kotlin_version"testCompile 'junit:junit:4.12' }

正常來說,經過以上步驟我們就可以正常使用kotlin了,但是等等!!!今天的主角好像不是怎么配置kotlin,我們的目的是讓kotlin與databinding共存。所以在你的Module的build.gradle還要加上一句:

apply plugin: 'kotlin-kapt' ... dependencies {compile fileTree(include: ['*.jar'], dir: 'libs')androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {exclude group: 'com.android.support', module: 'support-annotations'})compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:kotlin_version"kapt 'com.android.databinding:compiler:2.3.2'testCompile 'junit:junit:4.12' }

其中databinding complier的版本也就是2.3.2其實是和Project下的gradle版本是一樣的,當然了可以不一樣,如用2.3.1。
為了方便管理實際工作中我是這樣配置的

buildscript {ext.kotlin_version = '1.1.2-3'ext.gradle_version = '2.3.2'repositories {jcenter()}dependencies {classpath "com.android.tools.build:gradle:$gradle_version"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} } dependencies {compile fileTree(include: ['*.jar'], dir: 'libs')androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {exclude group: 'com.android.support', module: 'support-annotations'})compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:kotlin_version"kapt "com.android.databinding:compiler:$rootProject.ext.gradle_version"testCompile 'junit:junit:4.12' }

AndroidStudio3.0

AndroidStudio3.0默認集成了kotlin,我們只需要configure一下就好了。但是3.0用的kotlin用的應該是1.1.2-4,這個版本和as3.0可能有沖突,可能會出一個circular dependencies的錯誤。這個時候你需要在```gradle.properties或者 local.properties加入:kotlin.incremental=false

如果依然不好用可能需要降低kotlin或者databinding compiler的版本了。

總結

以上是生活随笔為你收集整理的Android开发:Kotlin下配置DataBinding的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。