突然我开始得到这个错误,我不知道为什么如果有人告诉我这个错误在哪里,就足够有帮助了。正如我所能得到的,这是因为android studio的新更新。 我得到的错误的详细总结。

Task :app:kaptDebugKotlin
    ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1C:\Users\shubh\Downloads\MarginCalculator\app\build\generated\source\kapt\debug\com\kotlin_developer\margincalculator\DataBinderMapperImpl.java:10: error: cannot find symbol
    import com.kotlin_developer.margincalculator.databinding.FragmentCalculatorScreenBindingImpl;

    symbol:   class FragmentCalculatorScreenBindingImpl

    Task :app:kaptDebugKotlin FAILED
    location: package com.kotlin_developer.margincalculator.databinding
    FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 17s
29 actionable tasks: 27 executed, 2 up-to-date

当前回答

在我的例子中,这是因为我没有在我的ViewModel中实现Observable。我添加了一个EditText约束布局与android:text="@={addProductViewModel.inputProductName}"

一旦我在ViewModel类中实现了Observable,错误就消失了

ViewModel

class AddProductViewModel (
    private val repository: ProductRepository,
    private val context: Context
): ViewModel(), Observable {

    @Bindable
    val inputProductName = MutableLiveData<String>()


    fun addProduct() {
        //inputProductName.value
    }

    override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
        TODO("Not yet implemented")
    }

    override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
        TODO("Not yet implemented")
    }
}

使用片段的MVVM数据绑定的完整示例

布局- add_product.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <data class=".AddProductBinding">
        <variable
            name="addProductViewModel"
            type="com.rao.iremind.AddProductViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <EditText
            android:id="@+id/editTextTextProductName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Product name"
            android:inputType="textPersonName"
            android:text="@={addProductViewModel.inputProductName}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />



    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

AddProductFragment

class AddProductFragment: Fragment() {
    private lateinit var binding: AddProductBinding
    private lateinit var addProductViewModel: AddProductViewModel
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding =  DataBindingUtil.inflate(inflater, R.layout.add_product, container, false)
        val dao = SubscriberDatabase.getInstance(requireActivity().applicationContext).productDAO
        val repository = ProductRepository(dao)
        val factory = AddProductViewModelFactory(repository, requireActivity().applicationContext)
        addProductViewModel = ViewModelProvider(this, factory).get(AddProductViewModel::class.java)
        binding.addProductViewModel = addProductViewModel
        binding.lifecycleOwner = this
        val view = binding.root

        return view
    }
}

添加产品视图模型

class AddProductViewModel (
    private val repository: ProductRepository,
    private val context: Context
): ViewModel(), Observable {

    @Bindable
    val inputProductName = MutableLiveData<String>()


    fun addProduct() {
        //inputProductName.value
    }

    override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
        TODO("Not yet implemented")
    }

    override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
        TODO("Not yet implemented")
    }
}

希望这能有所帮助 R

其他回答

我也遇到了同样的问题,所以我试图通过做来获得更多的信息

gradle - >应用程序- > - > Build - >组装任务

在这之后,我得到确切的错误说“错误,而注释处理”。 我检查了最近调整的DAO类,发现其中一个方法返回类型没有定义。

//Before
@Query("SELECT countryName FROM country_table WHERE countryCode= :code")
    fun getCountryNameForCode(code: String)

//After
@Query("SELECT countryName FROM country_table WHERE countryCode= :code")
    fun getCountryNameForCode(code: String): String

以我为例:问题解决了

步骤:

删除viewModel变量-在XML中。

<variable
    name="viewModel"
    type="com.xx.AppViewModel" / >

删除了所有viewModel绑定引用-在XML中。

android:text="@{viewModel.simName}"

删除了对绑定映射- In活动的viewModel实例引用

binding.viewModel = viewModel

清理项目并重新编译。 在XML & Build项目中添加viewModel变量。

< variable
    name="viewModel"
    type="com.xx.AppViewModel" / >

将viewModel实例引用添加到绑定映射-在活动和构建项目中

binding.viewModel = viewModel

添加所有viewModel绑定引用-在XML & Build项目中..

    android:text="@{viewModel.simName}"

它现在可以工作了。

——我希望这对你也有用。

我有一个枚举在我的实体属性使用房间。经过长时间的搜索,导致如下失败。

“:app:kaptDebugKotlin”任务执行失败。 执行org.jetbrains.kotlin.gradle.internal.KaptExecution时发生失败 invocationtargetexception(没有错误消息)

enum class Color{RED,BLACK,BLUE,GREEN,WHITE}
@Entity(tableName = "flower_table")
data class Flower(
        @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "name") val name: String,
        @ColumnInfo(name = "color") val color: Color)

val flower = Flower(2, "rose", Color.RED)

我的解决方案是在实体属性中使用枚举的索引。

@Entity(tableName = "flower_table")
data class Flower(
        @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "name") val name: String,
        @ColumnInfo(name = "color") val color: Int)

val flower = Flower(4, "tulip", Color.BLUE.ordinal)

希望能对你有所帮助! PS,不要忘记更新数据库类中的版本号。

@Database(entities = [Flower::class], version = 2)
abstract class FlowerRoomDatabase : RoomDatabase() {

我得到了同样的错误,对我来说,问题是我需要从Gradle JDK版本16.0.1切换到Android Studio java家庭版本11.0.10在Android Studio >首选项…>构建,执行,部署>构建工具> Gradle

如果你已经升级到类路径'com.android.tools.build:gradle:4.0.0' 替换之前的版本

dependencies {
    classpath 'com.android.tools.build:gradle:3.6.3'
}

和Change gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-   all.zip`