4

我正在尝试在我的 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'

以下是我在我的 gradle 中为 Room 添加的内容:

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"

我还添加apply plugin: 'kotlin-kapt'了我的 build.gradle。

这是我实现房间库的方式:

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 & code 中注释掉 Room 的东西,它编译得很好,但只用上面的代码就失败了。

已经尝试清理项目,但没有帮助。我在这里错过了什么吗?请帮帮我。

编辑我创建了新的演示项目来检查&我在那里也面临同样的编译错误。Room 相关代码与上面相同,这里我从我的演示中添加完整的 build.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'
}
4

8 回答 8

18

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

如此变化

abstract var cartRepositoryImpl: CartRepositoryImpl

至:

abstract fun cartRepositoryImpl(): CartRepositoryImpl

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

于 2017-12-07T12:20:20.040 回答
2

在我的情况下,在 Database Abstract 类中,我将 var 更改为 val。如下所示:

 abstract var noteDao: NoteDao

对此:

abstract val noteDao: NoteDao

并为我工作。

于 2019-11-23T17:04:11.550 回答
1

就我而言,我在 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
}
于 2019-01-13T18:41:51.680 回答
1

就我而言,我继承了Parcelable我的数据库的类。所以我改变了:

abstract class AppDatabase : Parcelable, RoomDatabase()

至:

abstract class AppDatabase : RoomDatabase()
于 2020-02-10T16:02:11.930 回答
0

当我的数据库定义实现Closeable接口时,我遇到了同样的异常。它需要空覆盖才能传递:

override fun close() {
    super.close()
}
于 2018-04-24T18:59:05.133 回答
0

我可能会迟到回答,但在尝试了这么多次并得到不同的错误之后,这对我有用。

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'

你可以试一试

于 2019-12-04T10:45:25.523 回答
0

这对我来说也是一个愚蠢的错误。

我用了

@Embedded(prefix = "id") val fooId: Long = 0L

代替

@ColumnInfo(name = "id") val fooId: Long = 0L
于 2020-05-26T16:51:19.823 回答
0

检查您的 DAO 中的函数/方法,可能是您传递了一个长值,您应该在其中传递一个实体。例子:

@Delete
public void remove(long id){

}
代替
  @Delete 
    public void remove(IndividualEntity in)
    {
    
    
    }
于 2020-09-24T15:35:05.227 回答