我的app数据库类
@Database(entities = {Detail.class}, version = Constant.DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract FavoritesDao favoritesDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();
//Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
Run Code Online (Sandbox Code Playgroud)
Gradle lib:
compile "android.arch.persistence.room:runtime:+"
annotationProcessor "android.arch.persistence.room:compiler:+"
Run Code Online (Sandbox Code Playgroud)
当我要求例如它会给出这个错误时,我的应用程序类中不存在AppDatabase_Impl
public class APp extends Application {
private boolean appRunning = false;
@Override
public void onCreate() {
super.onCreate();
AppDatabase.getAppDatabase(this); //--AppDatabase_Impl does …Run Code Online (Sandbox Code Playgroud) 我有谷歌这个问题,但结果对我不起作用。
详情如下。
public final class App extends com.zhixin.wedeep.common.BaseApplication implements androidx.lifecycle.LifecycleOwner {
^
// Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
Run Code Online (Sandbox Code Playgroud)
应用程序代码。
@HiltAndroidApp
class App : BaseApplication(), LifecycleOwner {
@Inject
lateinit var service: EventService
private val mLifecycleRegistry = LifecycleRegistry(this)
}
Run Code Online (Sandbox Code Playgroud)
此模块 gradle 文件。
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-allopen'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'dagger.hilt.android.plugin'
dependencies {
implementation rootProject.ext.dependencies["hilt-android"]
implementation rootProject.ext.dependencies["hilt-lifecycle-viewmodel"]
kapt rootProject.ext.kapt["hilt-compiler"]
kapt rootProject.ext.kapt["hilt-android-compiler"]
}
Run Code Online (Sandbox Code Playgroud)
谁有想法?谢谢!
我正在使用带有kotlin的android持久性库房间.
Dao看起来像这样
@Dao
interface CountryDao {
@Query("SELECT * FROM countries")
fun loadAllCountried() : LiveData<List<CountryEntity>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(products: List<CountryEntity>)
@Query("SELECT * FROM countries WHERE id = :countryId")
fun loadCountry(countryId: Int): LiveData<CountryEntity>
@Query("SELECT * FROM countries WHERE id = :countryId")
fun loadCountrySync(countryId: Int): CountryEntity
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很好,但我收到了这个错误
Error: Each bind variable in the query must have a matching method parameter. Cannot find method parameters for :countryId.
我可以看到参数被命名为countryId,那么可能是什么问题?
仅供参考:这是CountryDao_Impl.java中的生成代码
@Override
public CountryEntity loadCountrySync(int arg0) {
final String _sql = "SELECT …Run Code Online (Sandbox Code Playgroud) kapt当我尝试使用to迁移 Android 项目时收到错误消息KSP。
错误信息
Unable to find method ''void org.jetbrains.kotlin.gradle.tasks.KotlinCompile.<init>(org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions)''
'void org.jetbrains.kotlin.gradle.tasks.KotlinCompile.<init>(org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions)'
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project …Run Code Online (Sandbox Code Playgroud) 我试图实现一个简单的自动迁移,仅将一个可为空的字符串字段添加到我唯一的表中,但由于某种原因,我在行中收到以下错误 autoMigrations = [AutoMigration(from = 1, to = 2)]:
类型不匹配:推断类型为 AutoMigration,但预期为 KClass<*>
我说这是一个奇怪的错误,因为即使文档也是这样的。
完整代码如下:
@Database(
version = 2,
entities = [Note::class],
autoMigrations = [AutoMigration(from = 1, to = 2)]
)
@TypeConverters(Converters::class)
abstract class NotesDB : RoomDatabase() {
abstract fun noteDao(): NoteDao
companion object {
@Volatile
private var INSTANCE: NotesDB? = null
fun getDB(context: Context): NotesDB {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
NotesDB::class.java,
"notesDB"
).build()
INSTANCE = instance
instance
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我使用的是房间2.4.0-alpha01
但它告诉我这个错误
在路径:\app\schemascom.pigeon.mangaer.AppDB/2.json 中找不到迁移所需的模式。无法生成自动迁移。
这是我的代码:
@Database(
exportSchema = true,
version = 3,
entities = [Pigeon::class,PairEntity::class],
autoMigrations = [
AutoMigration(from = 2,to = 3)
]
)
abstract class AppDB:RoomDatabase() {
abstract fun pigeonDao():PigeonDao
abstract fun pairDao():PairDao
}
Run Code Online (Sandbox Code Playgroud) 我的Android应用程序中有很多未使用的图像.这些图像放在项目根目录中的单独文件夹中.它们不会在项目的任何地方使用,但我需要保留它们.
我担心的是这些未使用的图像会包含在apk文件中吗?由于它们中有很多并且增加了apk文件的大小.
我已将 kotlin 插件更新到 1.5.20 并发生了此问题。如果我返回 1.5.10 一切正常。
Schema export directory is not provided to the annotation processor so we cannot import the schema. To generate auto migrations, you must provide `room.schemaLocation` annotation processor argument AND set exportSchema to true.
public abstract class BatteryInfoDatabase extends androidx.room.RoomDatabase {
Run Code Online (Sandbox Code Playgroud)
我正在使用最新版本的 Room 持久性库 alpha 3,因为它提供自动迁移
def room_version = "2.4.0-alpha03"
implementation("androidx.room:room-ktx:$room_version")
kapt("androidx.room:room-compiler:$room_version")
Run Code Online (Sandbox Code Playgroud)
javaCompileOptions {
annotationProcessorOptions {
arguments += [
"room.schemaLocation":"$projectDir/schemas".toString()]
}
}
Run Code Online (Sandbox Code Playgroud)