突然我开始得到这个错误,我不知道为什么如果有人告诉我这个错误在哪里,就足够有帮助了。正如我所能得到的,这是因为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

当前回答

对我来说,一堆引用错误和DataBinding XML表达式中的一个错误产生了这个错误。

我在布局文件中删除了<variable/>,因为我想,我不再需要它了。我忘了在布局文件中引用了变量。

在构建项目之后,这会产生一个错误,其中不可能导入BindingImpl类,因为它不存在,并且此错误仅显示为与上述KaptExecution错误并行的警告。

搜索了一段时间后,我发现了这个错误并解决了它。然后,一堆引用错误显示,因为我重命名了一些东西,它没有重命名它在片段文件。在解决了这些错误之后,我完成了构建,没有出现错误或警告。

其他回答

大声喊出上面@Rene Spies的回答,我在使用数据绑定时也得到了这个错误。当您将@Bindable注释放在Kotlin中数据类的主构造函数的字段上时,构建引擎不喜欢这样做。

所以不要做下面的事情,

data class MyAwesomePojo(
    @Bindable
    var firstname: String,
    var lastname: String
)

相反,你需要做的是

data class MyCorrectAwesomePojo(
    var lastname: String
):{
    @get:Bindable
    var firstname: String
        set(value){
            field = value
        }
}

好处:如果你像我一样试图使用双向绑定来防止设置和获取的无限循环,请记得在设置值到字段之前检查相同的值。

好吧,如果你使用Android Studio 4.1.2,这个问题也会发生。

所以,你要做的是:

下载jdk 11(搜索下载) 在Android Studio中,点击“文件->项目结构-> Jdk位置->更改到刚刚下载的Jdk的路径。

解决了只需增加gradle版本到最新

它在项目级别的gradle文件中,在dependencies块下。

classpath "com.android.tools.build:gradle:4.x.x"

我也有同样的问题。让我带你们看一下我是如何解决这个问题的,以及我是如何解决它的,也许你们会有更大的了解。

之前解决

@Entity(tableName = "modules")
data class Module
(
    @PrimaryKey val id: Int,
    val name: String
)

@Entity(tableName = "sessions")
data class Session
(
    @PrimaryKey(autoGenerate = true) var id: Int,
    @ColumnInfo(name = "module_id") val moduleId: Int,
    @ColumnInfo(name = "start_time") val startTime: String,
    @ColumnInfo(name = "end_time") val endTime: String
)

data class ModuleSession
(
    @Embedded val module: Module,
    @Relation(
        parentColumn = "id",
        entityColumn = "module_id"
    )
    val sessions: List<Session>,
    @ColumnInfo(name = "is_updated") val isUpdated: Boolean = false // The problem
)

在DAO中

@Transaction
@Query("SELECT * FROM modules")
abstract suspend fun getModuleSession(): List<ModuleSession>

我得到的错误是

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

所以我深入挖掘,找到了下面的信息

The columns returned by the query does not have the fields [isUpdated] in com.gmanix.oncampusprototype.Persistence.ModuleSession even though they are annotated as non-null or primitive. Columns returned by the query: [id,name]
    public abstract java.lang.Object getModuleSession(@org.jetbrains.annotations.NotNull()

我从POJO ModuleSession中删除了字段IsUpdated,并将其添加到会话表中

后的变化

@Entity(tableName = "sessions")
data class Session
(
    @PrimaryKey(autoGenerate = true) var id: Int,
    @ColumnInfo(name = "module_id") val moduleId: Int,
    @ColumnInfo(name = "start_time") val startTime: String,
    @ColumnInfo(name = "end_time") val endTime: String,
    @ColumnInfo(name = "is_updated") val isUpdated: Boolean = false
)

data class ModuleSession
(
    @Embedded val module: Module,
    @Relation(
        parentColumn = "id",
        entityColumn = "module_id"
    )
    val sessions: List<Session>
)

另一方面,交叉检查SELECT语句中是否有可能导致问题的字段,或者您可以使用@Ignore对其进行注释

但是,如果你仍然觉得不舒服,你可以发布你的代码。

我希望这能有所帮助

也许我回答的有点晚了,但是不管怎样,我得到了同样的错误。构建失败可能是由Android Studio未能指出的一些错误引起的,可能是由于项目规模非常大。

下面是指出确切错误的简单方法。

在Android studio的分析菜单,点击检查代码;检查整个项目,单击“确定”,等待检查完成。 现在你会看到一个标签,将指出所有的错误,警告等,你现在可以解决真正的问题。