欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kotlen_DataBinding_配置 kotlindatabindingandroid 

程序员文章站 2022-03-08 20:49:15
...
新增的Kotlin项目需要使用DataBinding的时候要在module的build.gradle中添加如下配置

1,启用DataBinding, 跟使用java语言时一样的配置
android {
...
    dataBinding {
        enabled = true
    }
}


2,dependencies 添加插件
dependencies {
...
    //kotlin使用DataBinding需要用到, 版本跟gradle一致
    kapt "com.android.databinding:compiler:$gradle_version"
}


3,文件末尾添加插件配置
kapt {
    generateStubs = true
}




module的build.gradle举例如下

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

android {
    buildToolsVersion androidBuildTools
    compileSdkVersion androidTargetSdk

    defaultConfig {
        applicationId "com.xxx.xxx"
        minSdkVersion 19
        targetSdkVersion androidTargetSdk
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            //minifyEnabled false
            //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        incremental true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    //kotlin使用DataBinding需要用到, 版本跟gradle一致
    kapt "com.android.databinding:compiler:$gradle_version"
    //design
    compile 'com.android.support:design:25.3.1'
    //retrofit,OKHttp3
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    //Retrofit2使用gson序列化
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    //okhttp3拦截器
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    //GSON
    compile 'com.google.code.gson:gson:2.8.0'
}

//kotlin使用DataBinding需要用到
kapt {
    generateStubs = true
}



project的build.gradle举例如下
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.51'
    ext.gradle_version = '3.0.1'
    repositories {
        google()
        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
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
    ext.androidBuildTools = '27.0.3'
    ext.androidTargetSdk = 25
}

task clean(type: Delete) {
    delete rootProject.buildDir
}