Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin + Room : java.lang.IllegalArgumentException: void cannot be converted to an Element

I am trying to add Room in my project which is Java + Kotlin, but when I try to compile the project, it fails on :app:kaptDebugKotlin with following error:

    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)

Here are the versions I am using:

kotlin_version = '1.1.51'
room library version = '1.0.0'
android build tool = '3.0.1'

below things I have added in my gradle for 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"

I have also added apply plugin: 'kotlin-kapt' in my build.gradle.

Here's how I have implemented 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
}

I have just coded this without using it anywhere & tried to compile, but it fails. It compiles fine if I comment out the Room stuff from gradle & code but fails with just the above code.

Have already tried cleaning the project but it didn't help. Am I missing something here? Please help me out here.

Edit I created the new demo project to check & I am also facing the same compilation error there. The Room related code is same as above, here I am adding the full build.gradle from my demo:

Project level 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
}

app module's 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'
}
like image 864
Sandip Fichadiya Avatar asked Nov 28 '22 22:11

Sandip Fichadiya


1 Answers

Maaan! What a silly mistake! In abstract RoomDatabase class, we need to declare the abstract fun returning the Dao object but I had defined abstract var there.

So changing

abstract var cartRepositoryImpl: CartRepositoryImpl

to:

abstract fun cartRepositoryImpl(): CartRepositoryImpl

fixed the compilation error! I overlooked this while following the stuff from a blog I admit, but still the better compile time error wouldn't have hurt!

like image 196
Sandip Fichadiya Avatar answered Dec 04 '22 01:12

Sandip Fichadiya