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

时间:2018-01-05 21:06:27

标签: java android kotlin android-room android-database

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

但是当我为Room Persistent库添加依赖项时,突然构建项目开始失败。

我收到的错误:

Error image

这是我的项目级别 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()
}

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

我们将不胜感激。

1 个答案:

答案 0 :(得分:10)

Android Gradle Plugin 3.0及更高版本为依赖项添加了新行为。您可以阅读有关此here的更多信息。简而言之,通过在子模块中使用implementation,您不会与使用者模块(您的app模块)共享依赖关系。因此,无法生成Room所需的代码。

基本上,您需要做的只是将数据模块中的implementation更改为api以获取Room的依赖关系。结果将是:

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