#java #android #sqlite #android-room #android-room-prepackageddatabase
#java #Android #sqlite #android-комната #android-room-prepackageddatabase
Вопрос:
я пытался предварительно заполнить свою базу данных Room, но Android Studio сообщает, что он «не может разрешить метод ‘createFromAsset ()’ в ‘Builder’. Я использую версию room 2.2.6, а функция была реализована в версии 2.2.0.
package com.example.elementsfoodapp;
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;
@Database(entities = {Food.class}, version = 2, exportSchema = false)
public abstract class FoodRoomDatabase extends RoomDatabase {
public abstract FoodDao foodDao();
private static FoodRoomDatabase INSTANCE;
public static FoodRoomDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (FoodRoomDatabase.class) {
if (INSTANCE == null) {
// Create database here
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
FoodRoomDatabase.class, "food_database")
.createFromAsset("databases/food_database")
.build();
}
}
}
return INSTANCE;
}
}
зависимости:
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment:2.3.3'
implementation 'androidx.navigation:navigation-ui:2.3.3'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// Room components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"
//Lifecycle components
implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"
}
build.gradle (проект)
ext {
roomVersion = '2.2.6'
archLifecycleVersion = '2.2.0'
}
Ответ №1:
Вы используете android.arch
зависимости из эпохи до AndroidX. Они не включают никаких новых функций — они навсегда застряли в версиях 2.0, несмотря на то, что вы предоставляете более новую версию. Вам необходимо переключиться на androidx
версии этих артефактов в соответствии с зависимостями Room и зависимостями жизненного цикла, чтобы фактически получить более новые версии.
// Room components
implementation "androidx.room:runtime:$rootProject.roomVersion"
kapt "androidx.room:compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:testing:$rootProject.roomVersion"
//Lifecycle components
implementation "androidx.lifecycle:runtime-ktx:$rootProject.archLifecycleVersion"
kapt "androidx.lifecycle:compiler:$rootProject.archLifecycleVersion"
Комментарии:
1. Большое вам спасибо. Это решило мою проблему