简体   繁体   English

android Jetpack Room 导出模式失败

[英]android Jetpack Room Export schemas failed

I refer to setting Export schemas in the documentation, But it failed, the error is as follows:我在文档中参考了设置Export schemas,但是失败了,错误如下: 在此处输入图像描述 The following is my Gradle configuration:下面是我的Gradle配置:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'

    // hilt
    id 'kotlin-kapt'
    id 'com.google.dagger.hilt.android'

}

android {
    namespace 'com.freedom.android'
    compileSdk 32

    defaultConfig {
        applicationId "com.freedom.android"
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }

        // Room
        javaCompileOptions {
            annotationProcessorOptions {
                compilerArgumentProviders(
                        new RoomSchemaArgProvider(new File(projectDir, "schemas"))
                )
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.1.1'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }

    applicationVariants.all { variant ->
        variant.getRuntimeConfiguration().exclude group: 'com.google.code.findbugs', module: 'jsr305'
    }
}

dependencies {
    /**
     * Room
     */
    def room_version = "2.4.3"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    // To use Kotlin annotation processing tool (kapt)
    kapt "androidx.room:room-compiler:$room_version"
    // To use Coroutine features, you must add `ktx` artifact from Room as a dependency. androidx.room:room-ktx:<version>
    implementation "androidx.room:room-ktx:$room_version"



   
    // Could not find method ksp() for arguments [androidx.room:room-compiler:2.4.3] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
    // To use Kotlin Symbol Processing (KSP)
    // ksp "androidx.room:room-compiler:$room_version"

    // hilt
    implementation("com.google.dagger:hilt-android:2.44")
    kapt("com.google.dagger:hilt-android-compiler:2.44")

   
    def lifecycle_viewmodel_version = "2.5.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_viewmodel_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_viewmodel_version"

   
    // private val articleViewModel: ArticleViewModel by viewModels()
    // java.lang.IllegalArgumentException: CreationExtras must have a value by `SAVED_STATE_REGISTRY_OWNER_KEY`
    implementation "androidx.fragment:fragment-ktx:1.5.1"

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.1.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
}


// hilt
// Allow references to generated code
kapt {
    correctErrorTypes = true
}

// Room
class RoomSchemaArgProvider implements CommandLineArgumentProvider {

    @InputDirectory
    @PathSensitive(PathSensitivity.RELATIVE)
    File schemaDir

    RoomSchemaArgProvider(File schemaDir) {
        this.schemaDir = schemaDir
    }

    @Override
    Iterable<String> asArguments() {
        // Note: If you're using KSP, you should change the line below to return
        // ["room.schemaLocation=${schemaDir.path}"]
        return ["-Aroom.schemaLocation=${schemaDir.path}"]
    }
}

I am basically sure that it is an error caused by setting Export schemas.我基本确定是设置Export schemas导致的错误。 When I set exportSchema = false , the program can run normally.当我设置exportSchema = false时,程序可以正常运行。 Sorry my settings file is a bit messy, but like I said, it works.抱歉,我的设置文件有点乱,但就像我说的,它有效。

Convert string like this:像这样转换字符串:

 return ["-Aroom.schemaLocation=${schemaDir.path}".toString()]

About bug here:关于这里的错误:

https://github.com/gradle/gradle/issues/4730# https://github.com/gradle/gradle/issues/4730#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM