Android Room 无法处理我的类,因为它无法解决它们的所有依赖项

Posted

技术标签:

【中文标题】Android Room 无法处理我的类,因为它无法解决它们的所有依赖项【英文标题】:Android Room can not process my classes because it can not resolve all their dependencies 【发布时间】:2019-03-20 16:07:36 【问题描述】:

我正在尝试将 Room 集成到我的 kotlin 项目中,该项目也使用数据绑定。我已经实现了所有必需的类(我想是的!)但是当我尝试构建项目时,我收到以下错误消息并且无法找到是什么导致了这个错误或者我错过了什么

这是构建日志

e: [PATH_TO_PROJECT]\dataaccess\local\AppDatabase.java:7: error: android.arch.persistence.room.RoomProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract class AppDatabase extends android.arch.persistence.room.RoomDatabase 
                ^
e: [PATH_TO_PROJECT]\dataaccess\local\BusinessLocalDataSource.java:7: error: android.arch.persistence.room.RoomProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract class BusinessLocalDataSource implements [PACKAGE].dataaccess.local.BaseDao<[PACKAGE].dataaccess.local.model.Business>, [PACKAGE].dataaccess.BusinessDataSource 
                ^
e: [PATH_TO_PROJECT]\dataaccess\local\model\Business.java:7: error: android.arch.persistence.room.RoomProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public final class Business 
             ^
:app:kaptAppDebugKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptAppDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptAppDebugKotlin'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:103)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:73)
    at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:59)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ResolveBuildCacheKeyExecuter.execute(ResolveBuildCacheKeyExecuter.java:66)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
    at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter.java:44)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:91)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:59)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:256)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:249)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:238)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:663)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:597)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: org.gradle.api.GradleException: Compilation error. See log for more details
    at org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:16)
    at org.jetbrains.kotlin.gradle.internal.KaptWithKotlincTask.compile(KaptWithKotlincTask.kt:79)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.doExecute(StandardTaskAction.java:46)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:788)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:755)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:124)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:113)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:95)
    ... 31 more


* Get more help at https://help.gradle.org

BUILD FAILED in 9s

这是我的 AppDatabase 类

@Database(version = 1, entities = [(Business::class)])
abstract class AppDatabase : RoomDatabase() 

    abstract fun businessDao(): BusinessLocalDataSource

    companion object 
        private var INSTANCE: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase 
            if (INSTANCE == null) 
                synchronized(AppDatabase::class) 
                    INSTANCE = Room.databaseBuilder(context,
                            AppDatabase::class.java,
                            "app.db")
                            .build()
                
            
            return INSTANCE!!
        

        fun destroyInstance() 
            INSTANCE = null
        
    

我的企业实体

@Entity(tableName = "business")
data class Business(

        @PrimaryKey
        @ColumnInfo(name = "uuid")
        var uuid: String = "",

        @ColumnInfo(name = "title")
        var title: String = "",

        @ColumnInfo(name = "address")
        var address: String = "",

        @ColumnInfo(name = "votesCount")
        var votesCount: Int = 0,

        @ColumnInfo(name = "isSpecial")
        var isSpecial: Boolean = false,

        @ColumnInfo(name = "lat")
        var lat: Double? = 0.0,

        @ColumnInfo(name = "lon")
        var lon: Double? = null,

        @ColumnInfo(name = "imageUrl")
        var imageUrl: String = "",

        @ColumnInfo(name = "dateSeen")
        var dateSeen: Long = 0
)

我的 BaseDao 界面

interface BaseDao<T> 

    @Delete
    fun delete(t: List<T>): Int

    @Delete
    fun delete(t: T): Int

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(t: List<T>): List<Long>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(t: T): Long

    @Update
    fun update(t: List<T>): Int

    @Update
    fun update(t: T): Int

这是实现 BaseDao 的 BusinessLocalDataSource

@Dao
abstract class BusinessLocalDataSource : BaseDao<Business>, BusinessDataSource 

    override fun getTitleSuggestions(request: TitleSuggesterRequest): Maybe<TitleSuggesterResponse> 
        TODO("Currently local data source does not support getting title suggestions")
    

    override fun getPreSearch(): Maybe<PreSearchResponse> 
        TODO("Currently local data source does not support getting pre-search data")
    

    @Query("SELECT * from business")
    abstract override fun getLastSeen(): Flowable<List<Business>>

    override fun saveLastSeen(business: Business) 
        insert(business)
    

这是我的 build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android-extensions'

android 
    compileSdkVersion 26
    buildToolsVersion '28.0.3'

    // ...

    configurations.all 
        resolutionStrategy 
            force 'com.android.support:appcompat-v7:26.1.0'
            force 'com.android.support:design:26.1.0'
            force 'com.android.support:support-v4:26.1.0'
            force 'com.android.support:cardview-v7:26.1.0'
            force 'com.android.support:palette-v7:26.1.0'
        
    
    lintOptions 
        disable 'MissingTranslation'
    
    defaultConfig 
        multiDexEnabled true
        applicationId '[PACKAGE]'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        minSdkVersion 16
        targetSdkVersion 26

        versionCode 1
        versionName "1.0.0"

        ndk 
            abiFilters "armeabi-v7a", "x86"
        

        packagingOptions 
            exclude "lib/arm64-v8a/librealm-jni.so"
        

        vectorDrawables.useSupportLibrary = true
    
    sourceSets 
        main.java.srcDirs += 'src/main/kotlin'
    
    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    
    dexOptions 
        jumboMode true
        javaMaxHeapSize "4g"
    
    buildTypes 
        debug 
            versionNameSuffix '.DEBUG'
        
        release 
            minifyEnabled true
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    
    packagingOptions 
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/rxjava.properties'
        exclude 'META-INF/rxkotlin.properties'
        exclude 'META-INF/app_debug.kotlin_module'
        exclude 'META-INF/proguard/androidx-annotations.pro'
    
    dataBinding 
        enabled = true
    

    // ...


configurations 
    implementation.exclude group: 'org.jetbrains', module: 'annotations'
    implementation.exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jre7'


dependencies 

    // ...

    implementation "android.arch.persistence.room:runtime:1.1.1"
    kapt "android.arch.persistence.room:compiler:1.1.1"
    implementation "android.arch.persistence.room:rxjava2:1.1.1"

我真的不知道该检查什么了。如果有人帮忙就好了。 提前致谢。

【问题讨论】:

你的 gradle 版本是什么?有时会发生在 5.1.1 等更新版本中。 当时我最终使用了另一个库,因为我找不到导致此错误的原因。但是在将项目升级到AndroidX并使用最新版本的Room后,它可以正常工作,没有任何错误。 【参考方案1】:

也许你忘记在 build.gradle[app] 中添加 apply plugin: "kotlin-kapt"

【讨论】:

【参考方案2】:

请单独运行失败的脚本以获取更多详细信息:

./gradlew kaptAppDebugKotlin

PS。这不能解决您的问题,但您可以通过这种方式简化您的 getInstance() 函数:

fun getInstance(context: Context) = INSTANCE
                ?: Room.databaseBuilder(context, AppDatabase::class.java, "app.db")
                        .build()

【讨论】:

【参考方案3】:

我在构建文件中更改了序列,发生同样的错误,我的序列是:

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.fabric'

【讨论】:

【参考方案4】:

Room 生成的类需要 jetbrains 注释类才能正常工作。当您排除注释模块时,您会收到错误消息。删除以下代码进行修复。

configurations 
    implementation.exclude group: 'org.jetbrains', module: 'annotations'
    implementation.exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jre7'

【讨论】:

【参考方案5】:

您的问题看起来像this 问题,检查一下,希望您可以使用那里的解决方案来解决您的问题。

【讨论】:

以上是关于Android Room 无法处理我的类,因为它无法解决它们的所有依赖项的主要内容,如果未能解决你的问题,请参考以下文章

在 Room 中创建通用 DAO 接口时,“参数的类型必须是带有 @Entity 注释的类”

Android Room持久性库@Update无效

如何为 Android Room 请求制作通用 AsyncTask

无法导入 android.arch.persistence.room.testing.MigrationTestHelper

使用带有 Android 的 Room 使用 UUID 作为主键

在使用 Android 架构组件和 Room 时将数据插入外部源/数据库