在构建阶段找不到房间编译器错误

Could not find room-compiler error during build phase

我正在尝试在我的 android 项目中添加 Room 支持。我已经将所有依赖项添加到 gradle 文件中,并且它成功构建了……一次。每个后续构建都会导致 Could not find androidx.room:room-compiler:compiler

这是我的模块 gradle 文件:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
    google()
}

buildscript{
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
        google()
    }
}

android {
    //...
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.4@aar') {
        transitive = true
    }
    implementation 'com.airbnb.android:lottie:2.2.0'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'

    def room_version = "2.1.0-alpha03"

    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:compiler:$room_version"
}

这是我的项目 gradle 文件:

apply plugin: 'kotlin-android-extensions'
apply plugin: 'announce'
buildscript {
    ext.kotlin_version = '1.3.10'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}



allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
        maven { url "https://maven.google.com" }
    }
}

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

task info {
    doLast {
        announce.announce "Running $it.name", 'local'
        println gradle.gradleVersion
    }
}

最后,这里是完整的错误:

Could not find androidx.room:room-compiler:compiler.
Searched in the following locations:
  - file:/home/miku/Android/Sdk/extras/m2repository/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - file:/home/miku/Android/Sdk/extras/m2repository/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - file:/home/miku/Android/Sdk/extras/google/m2repository/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - file:/home/miku/Android/Sdk/extras/google/m2repository/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - file:/home/miku/Android/Sdk/extras/android/m2repository/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - file:/home/miku/Android/Sdk/extras/android/m2repository/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - https://dl.google.com/dl/android/maven2/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - https://dl.google.com/dl/android/maven2/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - https://jcenter.bintray.com/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - https://jcenter.bintray.com/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - https://jitpack.io/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - https://jitpack.io/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - https://maven.google.com/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - https://maven.google.com/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - https://repo.maven.apache.org/maven2/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - https://repo.maven.apache.org/maven2/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - https://maven.fabric.io/public/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - https://maven.fabric.io/public/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
  - https://dl.google.com/dl/android/maven2/androidx/room/room-compiler/compiler/room-compiler-compiler.pom
  - https://dl.google.com/dl/android/maven2/androidx/room/room-compiler/compiler/room-compiler-compiler-2.1.0-alpha03.jar
Required by:
    project :fpcommonlib

Gradle 同步很好,所以它会像找到所需的库一样接缝。但是,构建应用程序会使 Android Studio 显示该错误消息,而不是实际构建应用程序。所以看起来 Gradle 和 Build 出于某种原因正在使用不同的存储库。 除此之外,Kotlin 代码也没有显示任何错误。

好的,经过一番努力,我确实找到了解决方案

我创建了一个新项目并添加了原始项目中使用的所有附加依赖项。然后我将项目 Gradle 从新项目复制到旧项目中并且它起作用了。我不知道如何也不知道为什么两个 Gradle 文件看起来一样。

但为了保存(我可能遗漏了一些明显的东西)这里有两个 Gradle 个文件

工作之一:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
    google()
}

buildscript{
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
        google()
    }
}

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    //implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.4@aar') {
        transitive = true
    }

    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
    implementation 'com.airbnb.android:lottie:2.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'androidx.core:core-ktx:1.0.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    def room_version = "2.1.0-alpha04"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
}

损坏的一个:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
    google()
}

buildscript{
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
        google()
    }
}

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

       testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.0.2'
    //    implementation 'com.android.support:support-v4:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.4@aar') {
        transitive = true
    }
    implementation 'com.airbnb.android:lottie:2.8.0'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'
    def room_version = "2.1.0-alpha04"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:compiler:$room_version"
}

也许有点晚了,但对我来说,删除项目文件夹中的 .idea / 和 .gradle / 目录就足够了。

此致!