首页 > 解决方案 > 从 Room 开始,遇到这个错误:不兼容的类型:NonExistentClass 无法转换为 Annotation

问题描述

我已经查看了每个链接中提供的解决方案。 NonExistentClass 无法转换为注释 - app:kaptDebugAndroidTestKotlin - 不相关,因为我没有使用 JUnit5。 错误:不兼容的类型:NonExistentClass 无法转换为 Annotation @error.NonExistentClass() -- 两个问题,一个是我不知道将 kapt { } 放在哪里,我尝试将其保留为全局,但没有这样做任何东西并将其置于依赖关系中都会导致错误。 NonExistentClass 无法转换为 Annotation -- 不确定在我的 build.gradle 文件中将 generateStubs 设置为 true 的位置以及将 correctErrorTypes 设置为 true 的位置。我不知道我是否可以自己使用 annotationProcessor。

在我意识到它已被弃用之前,我自己尝试实现 android.arch。

我尝试使用 ksp,但返回一个错误,指出编译器不知道 ksp() 方法在哪里。使用 implementation("com.google.devtools.ksp:symbol-processing-api:1.5.0-1.0.0-alpha10") 并没有解决 ksp 的错误。

确切的错误消息:“错误:不兼容的类型:NonExistentClass 无法转换为注释”@error.NonExistentClass()

这是我的模块 build.gradle

plugins {
    id 'com.android.application'
 //   id 'com.android.feature'
    id 'kotlin-android'
    id 'kotlin-kapt'
    //id 'dagger.hilt.android.plugin'
    //id 'kotlin-ksp'

}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
//apply plugin: 'dagger.hilt.android.plugin'
//apply plugin: 'com.android.feature'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.testingdatabases"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        kapt {
            correctErrorTypes = true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    // ROOM   - -- - --- - - - >
    def room_version = "2.3.0"

    implementation("androidx.room:room-runtime:$room_version")
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // To use Kotlin annotation processing tool (kapt)
    kapt("androidx.room:room-compiler:$room_version")
    // To use Kotlin Symbolic Processing (KSP)
   // ksp("androidx.room:room-compiler:$room_version")

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation("androidx.room:room-ktx:$room_version")

    api("io.grpc:grpc-kotlin-stub:1.0.0")
   // implementation("com.google.devtools.ksp:symbol-processing-api:1.5.0-1.0.0-alpha10")
    //  < -- - - - End of Room stuff
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

这是项目的 build.gradle 文件,

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.5.0"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        def nav_version = "2.3.0-alpha01"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }

}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是应用程序代码,

package com.example.testingdatabases

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.room.RoomDatabase;

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

@Entity
data class User(
    @PrimaryKey val uid: Int,
    @ColumnInfo(name = "first_name") val firstName: String?,
    @ColumnInfo(name = "last_name") val lastName: String?
)

@Dao
interface UserDao {
    @Query("SELECT * FROM user")
    fun getAll(): List<User>

    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
    fun loadAllByIds(userIds: IntArray): List<User>

    @Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
            "last_name LIKE :last LIMIT 1")
    fun findByName(first: String, last: String): User

    @Insert
    fun insertAll(vararg users: User)

    @Delete
    fun delete(user: User)
}

@Database(entities = arrayOf(User::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}

标签: androidkotlin

解决方案


  1. 在 gradleimplementation "a-b-x"中使用,在 kotlin 中使用 dsl implemntation("a-b-x"),不要混用
  2. 您的数据库未正确构建(根据我的说法)这里

推荐阅读