数据绑定未获得“标识符必须具有XML文件中的用户定义类型”错误

时间:2018-09-21 07:23:49

标签: android data-binding

我正在学习android数据绑定。我面临的问题是,在与当前设置的数据绑定相关的错误的情况下,我会收到非常笼统的错误消息,这些消息无法准确说明发生了什么事情和发生在哪里,而不是某些主题中提到的非常具体的消息关于数据绑定(for instance)。

考虑以下布局:

<?xml version="1.0" encoding="utf-8"?>
<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>
        <!--<import type="android.view.View"/>-->
        <!--<import type="android.text.TextUtils"/>-->
        <variable
            name="movie"
            type="us.kostenko.architecturecomponentstmdb.details.model.Movie" />
    </data>
    <TextView
        <!-- ... -->
        android:text="@{TextUtils.isEmpty(movie.release_date) ? @string/empty_date : movie.release_date}"/>
</layout>

请注意,导入被故意注释掉以触发错误。

我期望得到类似Identifiers must have user defined types from the XML file. TextUtils is missing it的错误。但是我得到的错误是非常普遍的错误:

    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin'.
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.gradle.api.GradleException: Compilation error. See log for more details
    at org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:16)
    at org.jetbrains.kotlin.gradle.internal.KaptWithKotlincTask.compile(KaptWithKotlincTask.kt:79)

我怀疑我的设置有问题。我正在使用Android Studio 3.1.4。

这是我的build.gradle

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-kapt'
    apply plugin: 'kotlin-android-extensions'
    android {
    //...
        dataBinding { enabled = true }
    }

    dependencies {
        // ...
        kapt "com.android.databinding:compiler:3.1.4"
    }

项目一级:

buildscript {
ext.kotlin_version = '1.2.70'
dependencies {
    classpath 'com.android.tools.build:gradle:3.1.4'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

有什么主意我想念的是什么吗?

1 个答案:

答案 0 :(得分:0)

这是我的,它工作正常,给出适当的错误,差异不大。试试这个...

应用程序级build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 28
    }
    dataBinding {
        enabled = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
    mavenCentral()
}

项目级build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

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