gpt4 book ai didi

android - Kotlin + Room : java. lang.IllegalArgumentException:void 无法转换为 Element

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:48 25 4
gpt4 key购买 nike

我正在尝试在我的 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$1$2.invoke(CompileServiceImpl.kt:386)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:889)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.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$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.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$0(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'

以下是我在 Room 的 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'

以下是我实现 Room Library 的方式:

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'
}

最佳答案

妈安!多么愚蠢的错误!在抽象 RoomDatabase 类中,我们需要声明返回 Dao 对象的抽象 fun 但我已经在那里定义了抽象 var

变化多端

abstract var cartRepositoryImpl: CartRepositoryImpl

到:

abstract fun cartRepositoryImpl(): CartRepositoryImpl

修复了编译错误!我在关注我承认的博客中的内容时忽略了这一点,但更好的编译时错误仍然不会造成伤害!

关于android - Kotlin + Room : java. lang.IllegalArgumentException:void 无法转换为 Element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47676905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com