迁移到AndroidX后无法实现Room库

时间:2019-06-26 08:51:36

标签: android kotlin android-room androidx

整个gradle文件在这里:

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

buildscript {
    dependencies {
        classpath 'io.fabric.tools:gradle:1.28.1'
    }

    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
}

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion compile_sdk_version

    defaultConfig {
        applicationId "..."
        minSdkVersion min_sdk_version
        targetSdkVersion target_sdk_version
        versionCode ...
        versionName "..."
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    flavorDimensions "..."

    productFlavors {
        x {
            dimension "..."
            applicationId "..."
            versionCode ..
            versionName "..."
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'manifest/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            resources.srcDirs = ['src/main/java']
            aidl.srcDirs = ['src/main/java']
            renderscript.srcDirs = ['src/main/java']
            res.srcDirs = ['res/main']
            assets.srcDirs = ['assets']
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    androidExtensions {
        experimental = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(path: ':...')
    implementation project(path: ':...'

    implementation(
            "com.google.android.gms:play-services-maps:$google_play_services_version",
            "com.google.android.gms:play-services-location:$google_play_services_version",
            "com.google.android.gms:play-services-gcm:$google_play_services_version",
            "com.google.android.gms:play-services-vision:17.0.2",
            "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version",
            "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version",
            "com.google.android.material:material:1.0.0",
            "androidx.appcompat:appcompat:1.1.0-alpha01",
            "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1",
            "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1",
            "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2",
            "androidx.constraintlayout:constraintlayout:1.1.3",
            "androidx.lifecycle:lifecycle-extensions:2.0.0",
            "androidx.navigation:navigation-ui-ktx:2.0.0-rc02",
            "androidx.navigation:navigation-fragment:2.0.0-rc02",
            ...
    )

    androidTestImplementation(
            "androidx.test:runner:1.1.0",
            "androidx.test.espresso:espresso-core:3.1.0"
    )

    testImplementation(
            "junit:junit:4.12",
            "org.mockito:mockito-core:1.10.19"
    )

    implementation('com.google.maps:google-maps-services:0.1.17') {
        exclude module: 'joda-time'
    }

    implementation('com.crashlytics.sdk.android:crashlytics:2.9.5@aar') {
        transitive = true
    }

    implementation 'androidx.room:room-runtime:2.1.0'
    kapt 'androidx.room:room-compiler:2.1.0'
}

Gradle同步成功并且也安装了应用程序,但是由于以下错误而崩溃:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to create application ...MyApp:java.lang.RuntimeException: cannot find implementation for com.x.logs.XDatabase. XDatabase_Impl does not exist at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5743)

..和XDatabase文件导入为:

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import android.content.Context
import android.util.Log

这是在我将整个项目及其子模块迁移到android-x之后立即发生的。知道缺少什么吗? 有什么我可以做的,以得到更精确的错误信息?

1 个答案:

答案 0 :(得分:0)

尝试在gradle.properties(项目属性)中添加以下行

android.useAndroidX=true
android.enableJetifier=true