具有Koti ln通用类型的Android Gradle插件3.5.1数据绑定错误

问题描述 投票:3回答:1

我正在尝试对库进行模块化,因此此处可能会发生任何事情。从Android Gradle插件3.5.0更新到3.5.1之后,我现在在使用Resource.kt类的布局上收到数据绑定错误。我认为该类是直接从Google Github Browser示例中提出的(出于某种原因,我无法获得该类的最新提交)。该错误似乎与通用数据字段T有关。

Resource.kt

data class Resource<out T>(val status: Status, val data: T?, val message: String?, val throwable: Throwable? = null) {
    companion object {
        fun <T> success(data: T?): Resource<T> {
            return Resource(SUCCESS, data, null)
        }

        fun <T> error(data: T?, msg: String, throwable: Throwable?): Resource<T> {
            return Resource(ERROR, data, msg, throwable)
        }

        fun <T> loading(data: T?): Resource<T> {
            return Resource(LOADING, data, null)
        }
    }
}

布局xml:

<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="core.sdk.data.remote.response.Resource" />

        <import type="core.sdk.data.remote.response.Status" />

        <variable
            name="resource"
            type="Resource" />

        <variable
            name="progressText"
            type="String" />
    </data>

    <LinearLayout
        android:id="@+id/circular_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical"
        app:visibleGone="@{resource.data == null}">

        <androidx.core.widget.ContentLoadingProgressBar
            android:id="@+id/progress_bar"
            style="@style/Widget.AppCompat.ProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateTint="@color/ll_secondary"
            android:indeterminateTintMode="src_in"
            app:visibleGone="@{resource.status == Status.LOADING}"
            tools:ignore="UnusedAttribute" />

        <TextView
            android:id="@+id/progress_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@{progressText}"
            android:textAppearance="?attr/textAppearanceHeadline5"
            android:textColor="@color/ll_secondary"
            android:textStyle="italic"
            app:visibleGone="@{resource.status == Status.LOADING}"
            tools:text="loading..." />
    </LinearLayout>

</layout>

错误:

LoadingStateBindingImpl.java:106:错误:';'预期? resourceData = null;enter image description here

我具有增量数据绑定并启用了kapt:

android.databinding.incremental=true
kapt.incremental.apt=true

[项目是使用Kotlin 1.3.50的完全Kotlin,jvm目标为1.8:]

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
    jvmTarget = "1.8"
}

此错误在AGP 3.5.0中不出现。这是具有3.5.0且没有错误的相同文件:

enter image description here

android kotlin android-gradle-plugin android-databinding android-studio-3.5.1
1个回答
0
投票

不幸的是,这是对数据绑定工作方式的更改,从问题跟踪器的讨论来看,这是永久的更改。

数据绑定引用现在更加严格,因此使用通用类(在这种情况下为Resource)将失败。同样使用类似List的东西:

<variable name="list" type="java.util.List"/>

也会失败。因为数据绑定没有足够的信息来确定泛型是什么-具有泛型知识的人也许可以解释为什么会这样!

为了处理泛型,数据绑定需要更多信息。有两种方法可以更新数据绑定以与新的gradle插件一起使用:

  1. 直接定义您的通用名称。 “&lt”是与“
<variable name="list" type="java.util.List&lt;MyClass>"/>
  1. 将泛型包装在包含类中。
class MyGenericWrapper : List<Object>  

<variable name="myVariable" type="MyGenericWrapper"/>
© www.soinside.com 2019 - 2024. All rights reserved.