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

Moh*_*dva 4 java android kotlin android-database android-room

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

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

我收到的错误:

错误图片

这是我的项目级 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
}
Run Code Online (Sandbox Code Playgroud)

这是我的app模块 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")
}
Run Code Online (Sandbox Code Playgroud)

这是我的域模块 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"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的数据模块 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()
}
Run Code Online (Sandbox Code Playgroud)

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

我们将不胜感激.

and*_*cev 10

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

从本质上讲,你需要做的是简单地改变implementationapi了房间的依赖数据模块中.结果将是:

api "android.arch.persistence.room:runtime:$room_version"
api "android.arch.persistence.room:rxjava2:$room_version"
Run Code Online (Sandbox Code Playgroud)

  • @IgorGanapolsky这是`api`和`implementation`之间的本质区别。当在模块中使用“implementation”时,只有该模块才能访问此依赖项。当在模块 A 中使用 api 时,另一个模块可以访问此依赖项(如果它依赖于模块 A)。 (3认同)