Kotlin + Room:java.lang.IllegalArgumentException:void 无法转换为元素
Kotlin + Room : java.lang.IllegalArgumentException: void cannot be converted to an Element
我正在尝试在我的 Java + Kotlin 项目中添加 Room,但是当我尝试编译该项目时,它在 :app:kaptDebugKotlin
上失败并出现以下错误:
e: java.lang.IllegalStateException: failed to analyze: java.lang.IllegalArgumentException: void cannot be converted to an Element
at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:386)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda.invoke(CompileServiceImpl.kt:889)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:916)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:888)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385)
at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run[=11=](TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: void cannot be converted to an Element
at com.google.auto.common.MoreTypes$AsElementVisitor.defaultAction(MoreTypes.java:532)
at com.google.auto.common.MoreTypes$AsElementVisitor.defaultAction(MoreTypes.java:527)
at javax.lang.model.util.SimpleTypeVisitor6.visitNoType(SimpleTypeVisitor6.java:226)
at com.sun.tools.javac.code.Type$JCVoidType.accept(Type.java:1736)
at com.google.auto.common.MoreTypes.asElement(MoreTypes.java:524)
at com.google.auto.common.MoreTypes.asTypeElement(MoreTypes.java:553)
以下是我使用的版本:
kotlin_version = '1.1.51'
room library version = '1.0.0'
android build tool = '3.0.1'
我在 gradle 房间中添加的内容如下:
kapt "com.android.databinding:compiler:3.0.1"
//Room
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
implementation "android.arch.persistence.room:rxjava2:1.0.0"
我在 build.gradle 中也添加了 apply plugin: 'kotlin-kapt'
。
以下是我实现房间库的方式:
RoomDatabase.kt
@Database(entities = arrayOf(ProductTest::class), version = CartDatabase.DATABASE_VERSION)
abstract class CartDatabase : RoomDatabase() {
companion object {
const val DATABASE_VERSION = 1
const val DATABASE_NAME = "sall_customer_db"
}
abstract var cartRepositoryImpl: CartRepositoryImpl
}
ProductTest.kt
@Entity
data class ProductTest(
@PrimaryKey
@ColumnInfo(name = "id")
var productId: Int,
@ColumnInfo(name = "name")
var productName: String
)
CartRepositoryImpl.kt
@Dao
interface CartRepositoryImpl : CartRepository {
@Insert(onConflict = OnConflictStrategy.REPLACE)
override fun addToCart(product: ProductTest): Long
}
我刚刚编写了这个代码,但没有在任何地方使用它,并试图编译,但它失败了。如果我从 gradle & 代码中注释掉 Room 的东西,它编译得很好,但仅上面的代码就失败了。
已经尝试清理项目,但没有帮助。我在这里错过了什么吗?请帮帮我。
编辑 我创建了新的演示项目来检查,我也面临同样的编译错误。 Room 相关代码与上面相同,这里我从我的演示中添加完整的 build.gradle:
项目级别gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
// Versions
ext {
support_library_version = '27.0.0'
play_services_version = '11.6.2'
retrofit_version = '2.3.0'
arch_components_version = '1.0.0'
}
task clean(type: Delete) {
delete rootProject.buildDir
}
应用模块的gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.abc.kotlinroomdemo"
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
// RxJava2
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
//Room
implementation "android.arch.persistence.room:runtime:$arch_components_version"
implementation "android.arch.persistence.room:rxjava2:$arch_components_version"
kapt "android.arch.persistence.room:compiler:$arch_components_version"
//Dagger 2
//compile 'com.google.dagger:dagger-android:2.10'
//kapt 'com.google.dagger:dagger-android-processor:2.10'
//compileOnly 'com.google.dagger:dagger:2.10'
//implementation 'com.google.dagger:dagger:2.13'
//kapt 'com.google.dagger:dagger-compiler:2.13'
//provided 'javax.annotation:jsr250-api:1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
妈呀!多么愚蠢的错误!在 abstract RoomDatabase
class 中,我们需要声明 abstract fun 返回 Dao 对象,但我已经定义了 abstract var那里。
如此变化
abstract var cartRepositoryImpl: CartRepositoryImpl
至:
abstract fun cartRepositoryImpl(): CartRepositoryImpl
修复了编译错误!我在关注我承认的博客中的内容时忽略了这一点,但更好的编译时错误仍然不会受到伤害!
当我的数据库定义实现 Closeable
接口时,我 运行 陷入同样的异常。
它需要空覆盖才能传递:
override fun close() {
super.close()
}
在我的例子中,我在 Dao 层中添加了一个函数,其中没有 Table。
错误:
@Dao
interface ProductDao {
@Insert
fun insert(product: Int) // Here Int can't save in Room DB
}
这对我有用:
@Dao
interface ProductDao {
@Insert
fun insert(product: Product) // Here I added table instead Int
}
在我的例子中,在数据库摘要中 class 我将 var 更改为 val。如下所示:
abstract var noteDao: NoteDao
对此:
abstract val noteDao: NoteDao
并为我工作。
我可能会迟到回答,但在尝试了很多次并遇到不同的错误之后,这对我有用。
implementation 'androidx.room:room-runtime:2.2.2'
kapt 'androidx.room:room-compiler:2.2.2'
但在我的旧项目中我使用了这个
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
你可以试试
在我的例子中,我为我的数据库继承了 Parcelable
class。所以我改变了:
abstract class AppDatabase : Parcelable, RoomDatabase()
至:
abstract class AppDatabase : RoomDatabase()
这对我来说也是一个愚蠢的错误。
我用了
@Embedded(prefix = "id") val fooId: Long = 0L
而不是
@ColumnInfo(name = "id") val fooId: Long = 0L
检查您的 DAO 中的 functions/Methods,可能是您在本应传递实体的地方传递了一个长值。例子:
</p>
<pre><code>@Delete
public void remove(long id){
}
代替
<pre><code> @Delete
public void remove(IndividualEntity in)
{
}
我正在尝试在我的 Java + Kotlin 项目中添加 Room,但是当我尝试编译该项目时,它在 :app:kaptDebugKotlin
上失败并出现以下错误:
e: java.lang.IllegalStateException: failed to analyze: java.lang.IllegalArgumentException: void cannot be converted to an Element
at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:386)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda.invoke(CompileServiceImpl.kt:889)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:916)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:888)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385)
at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run[=11=](TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: void cannot be converted to an Element
at com.google.auto.common.MoreTypes$AsElementVisitor.defaultAction(MoreTypes.java:532)
at com.google.auto.common.MoreTypes$AsElementVisitor.defaultAction(MoreTypes.java:527)
at javax.lang.model.util.SimpleTypeVisitor6.visitNoType(SimpleTypeVisitor6.java:226)
at com.sun.tools.javac.code.Type$JCVoidType.accept(Type.java:1736)
at com.google.auto.common.MoreTypes.asElement(MoreTypes.java:524)
at com.google.auto.common.MoreTypes.asTypeElement(MoreTypes.java:553)
以下是我使用的版本:
kotlin_version = '1.1.51'
room library version = '1.0.0'
android build tool = '3.0.1'
我在 gradle 房间中添加的内容如下:
kapt "com.android.databinding:compiler:3.0.1"
//Room
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
implementation "android.arch.persistence.room:rxjava2:1.0.0"
我在 build.gradle 中也添加了 apply plugin: 'kotlin-kapt'
。
以下是我实现房间库的方式:
RoomDatabase.kt
@Database(entities = arrayOf(ProductTest::class), version = CartDatabase.DATABASE_VERSION)
abstract class CartDatabase : RoomDatabase() {
companion object {
const val DATABASE_VERSION = 1
const val DATABASE_NAME = "sall_customer_db"
}
abstract var cartRepositoryImpl: CartRepositoryImpl
}
ProductTest.kt
@Entity
data class ProductTest(
@PrimaryKey
@ColumnInfo(name = "id")
var productId: Int,
@ColumnInfo(name = "name")
var productName: String
)
CartRepositoryImpl.kt
@Dao
interface CartRepositoryImpl : CartRepository {
@Insert(onConflict = OnConflictStrategy.REPLACE)
override fun addToCart(product: ProductTest): Long
}
我刚刚编写了这个代码,但没有在任何地方使用它,并试图编译,但它失败了。如果我从 gradle & 代码中注释掉 Room 的东西,它编译得很好,但仅上面的代码就失败了。
已经尝试清理项目,但没有帮助。我在这里错过了什么吗?请帮帮我。
编辑 我创建了新的演示项目来检查,我也面临同样的编译错误。 Room 相关代码与上面相同,这里我从我的演示中添加完整的 build.gradle:
项目级别gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
// Versions
ext {
support_library_version = '27.0.0'
play_services_version = '11.6.2'
retrofit_version = '2.3.0'
arch_components_version = '1.0.0'
}
task clean(type: Delete) {
delete rootProject.buildDir
}
应用模块的gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.abc.kotlinroomdemo"
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
// RxJava2
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
//Room
implementation "android.arch.persistence.room:runtime:$arch_components_version"
implementation "android.arch.persistence.room:rxjava2:$arch_components_version"
kapt "android.arch.persistence.room:compiler:$arch_components_version"
//Dagger 2
//compile 'com.google.dagger:dagger-android:2.10'
//kapt 'com.google.dagger:dagger-android-processor:2.10'
//compileOnly 'com.google.dagger:dagger:2.10'
//implementation 'com.google.dagger:dagger:2.13'
//kapt 'com.google.dagger:dagger-compiler:2.13'
//provided 'javax.annotation:jsr250-api:1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
妈呀!多么愚蠢的错误!在 abstract RoomDatabase
class 中,我们需要声明 abstract fun 返回 Dao 对象,但我已经定义了 abstract var那里。
如此变化
abstract var cartRepositoryImpl: CartRepositoryImpl
至:
abstract fun cartRepositoryImpl(): CartRepositoryImpl
修复了编译错误!我在关注我承认的博客中的内容时忽略了这一点,但更好的编译时错误仍然不会受到伤害!
当我的数据库定义实现 Closeable
接口时,我 运行 陷入同样的异常。
它需要空覆盖才能传递:
override fun close() {
super.close()
}
在我的例子中,我在 Dao 层中添加了一个函数,其中没有 Table。
错误:
@Dao
interface ProductDao {
@Insert
fun insert(product: Int) // Here Int can't save in Room DB
}
这对我有用:
@Dao
interface ProductDao {
@Insert
fun insert(product: Product) // Here I added table instead Int
}
在我的例子中,在数据库摘要中 class 我将 var 更改为 val。如下所示:
abstract var noteDao: NoteDao
对此:
abstract val noteDao: NoteDao
并为我工作。
我可能会迟到回答,但在尝试了很多次并遇到不同的错误之后,这对我有用。
implementation 'androidx.room:room-runtime:2.2.2'
kapt 'androidx.room:room-compiler:2.2.2'
但在我的旧项目中我使用了这个
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
你可以试试
在我的例子中,我为我的数据库继承了 Parcelable
class。所以我改变了:
abstract class AppDatabase : Parcelable, RoomDatabase()
至:
abstract class AppDatabase : RoomDatabase()
这对我来说也是一个愚蠢的错误。
我用了
@Embedded(prefix = "id") val fooId: Long = 0L
而不是
@ColumnInfo(name = "id") val fooId: Long = 0L
检查您的 DAO 中的 functions/Methods,可能是您在本应传递实体的地方传递了一个长值。例子:
</p>
<pre><code>@Delete
public void remove(long id){
}
代替
<pre><code> @Delete
public void remove(IndividualEntity in)
{
}