简体   繁体   中英

DataBinding in a Kotlin project

Addenda :

I realize problem is related to :

app:visibleGone="@{isLoaded}"

in the following layout :

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="isLoaded"
            type="boolean" />
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.MainActivity">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:visibleGone="@{isLoaded}" />

        </android.support.v4.widget.SwipeRefreshLayout>

        <include
            layout="@layout/network_state_item"
            app:visibleGone="@{!isLoaded}" />

    </FrameLayout>

</layout>

I have following branch in Bitbucket : https://bitbucket.org/ali-rezaei/tmdb/src/dataBinding/

I get following Kotlin compiler error when I build the project :

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.

I appreciate if you can help me out.

Here is my project build.gradle :

buildscript {
    ext.kotlin_version = '1.2.60'
    ext.gradle_version = '3.1.4'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

And here is my app build.gradle :

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

androidExtensions {
    experimental = true
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    defaultConfig {
        applicationId "com.sample.android.tmdb"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        dataBinding {
            enabled = true
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // Support libraries
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:design:$rootProject.supportLibraryVersion"
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"

    // Architecture components
    implementation "android.arch.paging:runtime:$rootProject.paging"
    implementation "android.arch.lifecycle:runtime:$rootProject.lifecycle"
    implementation "android.arch.lifecycle:extensions:$rootProject.lifecycle"

    //DataBinding
    kapt "com.android.databinding:compiler:$gradle_version"

    //Gson
    implementation 'com.google.code.gson:gson:2.8.2'

    //Retrofit
    implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"

    //Dagger
    implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
    compileOnly 'org.glassfish:javax.annotation:10.0-b28'
    implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"

    // Network
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation "com.github.bumptech.glide:glide:$rootProject.glideVersion"
    kapt "com.github.bumptech.glide:compiler:$rootProject.glideVersion"

    //Butter knife
    implementation "com.jakewharton:butterknife:$rootProject.butterknifeVersion"
    kapt "com.jakewharton:butterknife-compiler:$rootProject.butterknifeVersion"

    //Timber
    implementation 'com.jakewharton.timber:timber:4.5.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

As a sample you can look at : https://www.moveoapps.com/blog/how-to-use-data-binding-library-with-kotlin-a-step-by-step-guide/

Problem will be solved by creating following method as a Kotlin file in the project :

  @BindingAdapter("visibleGone")
  fun View.visibleGone(visible: Boolean) {
      visibility = if (visible) View.VISIBLE else View.GONE
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM