Android – 以下类的超types无法解析 – (Room Persistant Library,Android Library Module)

在我的Android项目(Kotlin)中,我想为我的DATA LAYER使用Room持久性库。

但是当我添加房间持久性库的依赖关系突然生成项目开始失败。

我正在收到的错误:

错误图像

这是我的项目级别 build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { globalCompileSdkVersion = 26 globalMinSdkVersion = 19 globalTargetSdkVersion = 26 kotlin_version = '1.2.10' support_version = "27.0.2" constraint_layout_version = "1.0.2" view_animator_version = "1.0.5" junit_version = "4.12" runner_version = "1.0.1" espresso_core_version = "3.0.1" room_version = "1.0.0" dagger_version = "2.13" rxJavaVersion = "2.1.7" rxAndroidVersion = "2.0.1" libs = [ appCompatV7 : "com.android.support:appcompat-v7:$support_version", rxJava : "io.reactivex.rxjava2:rxjava:$rxJavaVersion", rxAndroid : "io.reactivex.rxjava2:rxandroid:$rxAndroidVersion", junit : "junit:junit:$junit_version", runner : "com.android.support.test:runner:$runner_version", espressoCore: "com.android.support.test.espresso:espresso-core:$espresso_core_version" ] } repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' 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() } } task clean(type: Delete) { delete rootProject.buildDir } 

这是我的应用程序模块 build.gradle

 apply plugin: 'com.android.application' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion project.globalCompileSdkVersion defaultConfig { applicationId "com.keepsafe.app" minSdkVersion project.globalMinSdkVersion targetSdkVersion project.globalTargetSdkVersion versionCode 1 versionName "1.0" vectorDrawables.useSupportLibrary = true 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 libs.appCompatV7 implementation "com.android.support:design:$support_version" implementation "com.android.support.constraint:constraint-layout:$constraint_layout_version" implementation "com.github.florent37:viewanimator:$view_animator_version" implementation "com.google.dagger:dagger:$dagger_version" kapt "com.google.dagger:dagger-compiler:$dagger_version" implementation libs.rxJava implementation libs.rxAndroid testImplementation libs.junit androidTestImplementation libs.runner androidTestImplementation libs.espressoCore implementation project(":data") implementation project(":domain") } 

这是我的域模块 build.gradle

 apply plugin: 'java-library' apply plugin: 'kotlin' dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation libs.rxJava implementation libs.rxAndroid implementation libs.junit } sourceCompatibility = "1.7" targetCompatibility = "1.7" buildscript { repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } compileKotlin { kotlinOptions { jvmTarget = "1.8" } } compileTestKotlin { kotlinOptions { jvmTarget = "1.8" } } 

这是我的数据模块 build.gradle

 apply plugin: 'com.android.library' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android' android { compileSdkVersion project.globalCompileSdkVersion defaultConfig { minSdkVersion project.globalMinSdkVersion targetSdkVersion project.globalTargetSdkVersion 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 "android.arch.persistence.room:runtime:$room_version" implementation "android.arch.persistence.room:rxjava2:$room_version" kapt "android.arch.persistence.room:compiler:$room_version" testImplementation libs.junit implementation project(":domain") } repositories { mavenCentral() } 

我不知道如何解决这个问题。

您的帮助将不胜感激。

Android Gradle Plugin 3.0及更高版本为依赖关系添加了新的行为。 你可以在这里阅读更多。 长话短说,通过在子模块中使用implementation ,您不会与消费者模块(您的app模块)共享依赖关系。 因此,无法生成Room所需的代码。

从本质上讲,你需要做的只是把你的数据模块中的implementation更改为api的房间的依赖关系。 结果将是:

 api "android.arch.persistence.room:runtime:$room_version" api "android.arch.persistence.room:rxjava2:$room_version"