突然间,我开始收到此错误,并且我不知道为什么如果有人让我知道此错误在哪里,将足够有帮助。由于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
答案 0 :(得分:185)
Android Studio的用户界面只是隐藏错误...
发生错误时,它会在列表视图中突出显示该项目,并在终端视图中显示无用的消息。
要找到真正的错误,请在列表视图中选择根项,以便Android Studio在终端视图中显示整个构建输出,然后滚动以查找错误。
答案 1 :(得分:56)
也许我的答案有点晚了,但是无论如何,我遇到了同样的错误。构建失败可能是由Android Studio无法指出的一些错误引起的,可能是由于项目规模很大。
这是指出确切错误的简便方法。
答案 2 :(得分:33)
找出根本问题的方法是运行以下命令:
./gradlew assembleDebug --stacktrace
答案 3 :(得分:17)
我遇到了同样的问题,所以我试图通过这样做来获取更多信息
gradle-> app-> Tasks-> Build-> assemble
此后,我得到确切的错误,说“注释处理时出错”。 我检查了最近调整过的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
答案 4 :(得分:14)
一段时间以来,我有同样的错误,然后我开始检查其他软件包,我才知道我在数据库代码中犯了错字错误。所以, “遍历数据库和其他活动类文件,您可能会在这里发现一些错误。”
答案 5 :(得分:13)
更改
implementation "android.arch.persistence.room:runtime:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
收件人
implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
答案 6 :(得分:8)
对我来说,大量参考错误和带有DataBinding
的XML表达式中的错误导致了此错误。
我已经删除了<variable/>
文件中的layout
,因为我认为我已经不需要了。我忘记了在layout
文件中引用了变量。
构建项目后,这将产生一个错误,无法导入BindingImpl
类,因为该类不存在,并且仅与上述{{1} }错误。
搜索了一段时间后,我发现了此错误并解决了。然后,显示了一系列参考错误,因为我重命名了某些内容,但没有在KaptExecution
文件中将其重命名。在解决了这些错误之后,构建对我来说也没有错误或警告。
答案 7 :(得分:8)
如果您已升级到类路径'com.android.tools.build:gradle:4.0.0' 替换为以前的版本
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
}
然后更改gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4- all.zip`
答案 8 :(得分:6)
我这里也有同样的问题。就我而言,原因是我在我的一个 dagger 模块中忘记了 @Module 注释。
要找到这种 AS 警报的真正问题,有必要深入了解:标题中的消息它只是最后一个,显示错误的 1 个或多个原因选择左侧的根元素,即您的确切问题,请检查下面的打印屏幕:
答案 9 :(得分:5)
就我而言,我忘记在实体类中添加 @PrimaryKey。
答案 10 :(得分:4)
尝试使用构建您的项目
kapt.use.worker.api=false
在gradle.properties设置文件中
答案 11 :(得分:4)
就我而言,我使用的是房间,而我的databasDao方法之一具有未使用的参数,不幸的是android studio无法正确警告我
答案 12 :(得分:3)
我有同样的问题。就我而言,问题在于数据库。我不得不更改这行代码
@Database(entities = [SearchedLocation::class, FavoriteLocation::class], version = 1)
我在数据库中添加了另一个表,但是忘记在上一行中添加表。
答案 13 :(得分:3)
就我而言,从 jcenter() 移动到 mavenCentral() 时出现此错误,我的一个依赖项仅在 jcenter() 上可用,而在 mavenCentral() 上不可用。 所以我在 github 上向所有者提交了相同的问题,并将等待删除 jcenter() 直到它被移动。
答案 14 :(得分:2)
在经历了许多痛苦之后,我决定尝试使用annotationProcessor
而不是kapt
,希望它至少可以显示错误消息或可以帮助我找到源的任何内容。但是幸运的是(或不幸的是,由于浪费了时间),它已成功构建,没有任何错误。
这主要是kapt
本身的错误。因此,尝试此解决方案可能会有所帮助。
答案 15 :(得分:2)
如果您安装了新的kotlin插件(1.4.20-release-Studio4.1-1
)并带有匕首(kapt 'com.google.dagger:dagger-compiler:2.30'
),也会发生此问题。在这种情况下,一种解决方案可能是使用视图绑定(https://developer.android.com/topic/libraries/view-binding)替换不推荐使用的plugin: 'kotlin-android-extensions'
答案 16 :(得分:2)
就我而言,我使用的是协程,但我忘记为 Room 添加 Kotlin 扩展和协程支持
def room_version = "2.2.6" 实现“androidx.room:room-ktx:$room_version”
答案 17 :(得分:2)
通过将gradle版本增加到最新来解决-
它在项目级别的 gradle 文件中,在依赖项块下。
classpath "com.android.tools.build:gradle:4.x.x"
答案 18 :(得分:2)
我有同样的问题。让我向您介绍有关如何解决该问题以及解决问题的方式的示例,也许您可以了解更大的情况。
解决之前
@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
进行注释
但是,如果您仍然不满意,可以发布代码。
我希望这会有所帮助
答案 19 :(得分:1)
就我而言,降级 kotlin 版本(从 1.4.21 到 1.3.21)解决了我的问题。
buildscript {
ext.kotlin_version = '1.3.21'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
}
答案 20 :(得分:1)
如果在布局中的绑定表达式中使用了非英文字符,就会发生这种情况。例如:
<TextView ... android:text="@{ viewModel.letterTitle, default=`Важное письмо` }" />
如果您执行“Run with --stacktrace option”,您可以检查这种情况。如果堆栈跟踪包含消息 “MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.” 那么可能是
答案 21 :(得分:1)
当我将 ROOM 添加到我的应用程序 (Kotlin) 时,我遇到了同样的问题。 我很难找出问题所在,最后发现我正在使用以下内容将 ROOM 添加到我的应用中。
//room
implementation 'android.arch.persistence.room:runtime:1.1.1'
kapt "android.arch.persistence.room:compiler:1.1.1"
所以我改变了上面的两行,下面的代码摆脱了痛苦。
def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
如果对您有帮助,请点击向上按钮:D
答案 22 :(得分:1)
我有同样的错误。 我有两个问题。
您可能需要添加
实现'androidx.room:room-ktx:2.2.5'
我已经删除了一个在activity_xml文件之一中被引用为成员的文件。直到我将其更改为annotationProcessor而不是kapt为止,该错误才给我提供任何线索,然后它指出了该错误,我找到了该文件,并确定我已经引用了一个不再使用并被删除的文件。我从xml中删除了此数据引用,并将其全部清除。然后,我将其放回kapt。
答案 23 :(得分:1)
当我尝试使用 Hilt 在活动中注入私有字段时发生了这种情况。解决方案是删除私有关键字,因此更改
@Inject
private lateinit var appAnalytics: AppAnalytics
到
@Inject
lateinit var appAnalytics: AppAnalytics
答案 24 :(得分:1)
什么都没做,我尝试了一切,最后发现了一个小错误,这是一个大问题。
返回到每个新创建的数据库文件,并仔细逐行检查每个文件的代码。
检查数据库类,并检查是否声明了Dao,例如
abstract val commentDatabaseDao: CommentDatabaseDao
声明为 val 而不是 var ,在我看来,这终于解决了。
答案 25 :(得分:1)
这种方法对我来说是Room
数据库和Coroutines
所存在的所有问题,甚至拼写错误。最后是尝试通过插入列之后的Flow返回单个值的方法,方法是:Flow<Long>
来自DAO类。
它应该是suspend
函数,并且只有Long
类型才能在插入列之后返回。
这些问题有时是模棱两可的,因此请尝试阅读所有的Build Output消息,对我有帮助的消息是:error: Not sure how to handle insert method's return type.
答案 26 :(得分:1)
我刚发生这种情况。另一个开发人员未正确完成合并,因此该行在代码中
<<<<<<< HEAD
我收到了KaptExecution错误,而不是编译错误。
答案 27 :(得分:1)
对我来说,问题在于在模型上定义了2个主键。
// before
@field:ColumnInfo(name = "id") @field:PrimaryKey(autoGenerate = true) var id: Long = 0,
@field:ColumnInfo(name = "name") @field:PrimaryKey var name: String,
//after
@field:ColumnInfo(name = "id") @field:PrimaryKey(autoGenerate = true) var id: Long = 0,
@field:ColumnInfo(name = "name") @field:NotNull var name: String,
我不得不重建项目并稍微更改Dao类以触发有关此问题的消息。
答案 28 :(得分:1)
我也遇到了相同的问题,因此,只需尝试删除项目的.idea文件夹和.gradle文件夹,然后再删除app文件夹中的build文件夹,然后重新启动android studio IDE,它对我有用
答案 29 :(得分:1)
在我的情况下,我已将一个变量UserManager全局更改为NetWorkManager,并且在所有有UserManager类的地方都将其更改为NetworkManager。
因为我使用的是Hilt,所以我不得不再次构建该项目。
我清理了项目,然后Kotlin展示了错误所在。
答案 30 :(得分:1)
对@Rene Spies的答案大喊大叫,在使用数据绑定时我也遇到此错误。事实证明,当您在Kotlin中将@Bindable
批注放在data class
的主构造函数中的字段上时,构建引擎不喜欢它。
所以永远不要做以下事情,
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
}
}
奖金:如果您尝试像我这样使用双向绑定来防止设置和获取的无限循环,请记住在将值设置为字段之前检查相同的值。
答案 31 :(得分:1)
我在房间上遇到了同样的问题,并且我没有使用 viewBinding 。
我通过在数据库类中使用 exportSchema 设置为false修复了该问题。
@Database(entities = [ModelClass::class], version = 1, exportSchema = false)
abstract class ModelDatabase: RoomDatabase() {}
记住:exportScehma会根据您的用例而有所不同,通常它保持为false,所以我将其设置为false。
答案 32 :(得分:1)
有趣的是,由于添加了改造的描述,因此我收到了此错误。注意不要混淆“房间和装修”的描述。
答案 33 :(得分:1)
就我而言:问题已解决
步骤:
<variable
name="viewModel"
type="com.xx.AppViewModel" / >
android:text="@{viewModel.simName}"
binding.viewModel = viewModel
清理项目并重新编译。
添加viewModel变量-在XML和构建项目中。
< variable
name="viewModel"
type="com.xx.AppViewModel" / >
binding.viewModel = viewModel
android:text="@{viewModel.simName}"
-我希望它也对您有用。
答案 34 :(得分:1)
在我的情况下,这是因为我没有在ViewModel中实现Observable。我使用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
}
}
AddProductViewModel
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
答案 35 :(得分:0)
执行以下操作后出现此错误:
1.5.21
16
使用 assembleDebug
运行 --stacktrace
,我在日志中看到了这个异常:Caused by: java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext
。四处搜索,我发现这个 link 讨论了 kapt
和 JDK16+
之间的兼容性问题。
我通过在我的 gradle.properties
文件中进行此更改解决了该问题:
org.gradle.jvmargs=-Xmx1536m --illegal-access=permit
答案 36 :(得分:0)
我遇到了同样的错误,对我来说问题是我需要在 Android Studio > Preferences... > Build, Execution 中从 Gradle JDK JDK version 16.0.1
切换到 Android Studio java home version 11.0.10
,部署 > 构建工具 > Gradle
答案 37 :(得分:0)
我在使用 ROOM 的实体属性中有一个 enum。长时间搜索导致以下故障。
<块引用>任务“:app:kaptDebugKotlin”执行失败。
<块引用>执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障 java.lang.reflect.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() {
答案 38 :(得分:0)
升级到 kotlin_version = '1.4.31'(从 1.3.71 开始)时出现完全相同的错误
以下 moshi 升级解决了我的问题:
implementation("com.squareup.moshi:moshi:1.11.0")
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.11.0")
答案 39 :(得分:0)
如果您使用带有 Hilt 的 Kotlin
确保您使用
注释您的模块类@Module
@InstallIn(SingletonComponent::class)
class AppModule {
//...
}
答案 40 :(得分:0)
在某些情况下可能会有所帮助 gradle.properties
kapt.include.compile.classpath=true
答案 41 :(得分:0)
就我而言,我已经添加了这一行
android {
.
.
.
kapt.includeCompileClasspath = false
}
答案 42 :(得分:0)
是的,我也遇到了这个错误,它也是一个与Room
相关的问题。
我已经定义了TypeConverters
,但是从不用Room database
注释了@TypeConverters(TypeConverter.class)
。
答案 43 :(得分:0)
我刚刚将Android Studio IDE更新到4.1.1版本,并且遇到了类似的问题。
我意识到的是……
...在我没有一些*_Impl
类(我在Kotlin的知识中知道它们)之前,甚至还没有一些新的*Tests
类。
因此,在“运行世界”解决方案之后,我做出了一个直观而公平的选择:我删除了Android Studio更新之前不存在的“手工构建”程序包中的所有那些文件< / strong>。
那你猜怎么着?
有效。没问题,甚至没有关于kapt
的问题。
我并不是说这是最终解决方案。但是它可以为您工作。
有一个好的。