Ale*_*lex 63 android kotlin android-room kotlin-coroutines
Android studio 报错:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
Run Code Online (Sandbox Code Playgroud)
我想在我的项目 Kotlin Coroutines 中添加并将其与 Room 数据库一起使用。但是在添加了所有库之后,我收到了这个错误。这是来自编译器的所有信息。
我已经确定,这是因为注释@Database。如果我删除此注释,则不会出现错误,但 Room 也无法正常工作。
我的gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
//apply plugin: 'androidx.navigation.safeargs'
kotlin {
experimental {
coroutines 'enable'
}
}
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.bestcred.coursetthree"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
//vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// Enables data binding.
buildFeatures {
dataBinding true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Support libraries
implementation "androidx.appcompat:appcompat:1.2.0"
implementation 'com.google.android.material:material:1.2.0'
implementation "androidx.fragment:fragment:1.2.5"
implementation "androidx.constraintlayout:constraintlayout:2.0.0"
// Android KTX
implementation 'androidx.core:core-ktx:1.3.1'
// Room and Lifecycle dependencies
implementation "androidx.room:room-runtime:$room_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
kapt "android.arch.persistence.room:compiler:$room_version"
// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"
}
kotlin_version = "1.4.0"
room_version = "2.2.5"
coroutine_version = '1.3.9'
Run Code Online (Sandbox Code Playgroud)
Eri*_*ric 165
Android Studio 的 UI 只是隐藏了错误...
当错误发生时,它会在列表视图中突出显示该项目,并在终端视图中显示一条无用的消息。

要找到真正的错误,请在列表视图中选择根项目,以便 Android Studio 在终端视图中显示整个构建输出,然后滚动以查找错误。

小智 33
我在 Apple Silicon Macbook M1 上开发。
使用 room_version 2.2.4,在 2.2.5 中失败
def room_version = "2.2.4"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
Run Code Online (Sandbox Code Playgroud)
Aru*_*tya 12
当您在Room 数据库或Doa或实体类上犯错时会发生此异常 ,例如我在实体类中犯了错误
因此,如果您导入了正确的依赖项,我建议仔细检查实体、dao 或数据库类。
@Entity(tableName = "user_table")
data class User(
val firstName: String,
val lastName: String,
val age: Int
) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0 //**do not made it val**
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex 10
你需要改变:
kapt "android.arch.persistence.room:compiler:$room_version"
Run Code Online (Sandbox Code Playgroud)
到
kapt "androidx.room:room-compiler:$room_version"
Run Code Online (Sandbox Code Playgroud)
我可能会迟到,但只有以下解决方案对我有用。
在 Android studio 中打开gradle.properties文件。
添加以下行:
kapt.use.worker.api=false
kapt.incremental.apt=false
Run Code Online (Sandbox Code Playgroud)
清理并重新构建应用程序。
完毕
尝试将其添加到您的app/build.gradle文件中并再次运行该程序。它帮助我找到了问题的真正原因。
kapt {
correctErrorTypes true
}
Run Code Online (Sandbox Code Playgroud)
今天我遇到了这个错误。就我而言,我的笔记本电脑中同时安装了 java 11 和 java 16,java 16 是默认的。但是我项目中的 gradle 使用 java 11 来构建它。所以当我调用
$ gradle build
Run Code Online (Sandbox Code Playgroud)
从我的终端,它将使用 java 16,因此构建失败。我知道,正确的解决方案是使我的代码与 java 16 兼容,但是现在,我使用以下命令明确告诉 gradle 使用 java 11:
$ gradle build -Dorg.gradle.java.home=/usr/lib/jvm/java-11-openjdk-amd64 # warning: use te path to your java 11
Run Code Online (Sandbox Code Playgroud)
由于它是一个大命令,我将它保存在一个脚本文件中,因此只需运行该脚本即可为我解决问题。
| 归档时间: |
|
| 查看次数: |
28151 次 |
| 最近记录: |