Fed*_*hin 37 implementation android android-room
我在测试Room数据库时遇到问题:当我运行测试时,我得到以下异常:
java.lang.RuntimeException: cannot find implementation for database.TicketDatabase. TicketDatabase_Impl does not exist
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92)
at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:454)
at com.sw.ing.gestionescontrini.DatabaseTest.setDatabase(DatabaseTest.java:36)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)
Run Code Online (Sandbox Code Playgroud)
类DatabaseTest:
public class DatabaseTest {
TicketDatabase database;
DAO ticketDAO;
@Before
public void setDatabase() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Context context = InstrumentationRegistry.getTargetContext();
database = Room.inMemoryDatabaseBuilder(context, TicketDatabase.class).build();
Method method = TicketDatabase.class.getDeclaredMethod("ticketDao()");
method.setAccessible(true);
ticketDAO = (DAO) method.invoke(database, null);
}
}
Run Code Online (Sandbox Code Playgroud)
gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.sw.ing.gestionescontrini"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
defaultConfig {
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'
compile 'android.arch.persistence.room:rxjava2:1.0.0-rc1'
testCompile'android.arch.persistence.room:testing:1.0.0-rc1'
}
Run Code Online (Sandbox Code Playgroud)
我真的不知道这个实现应该是什么,我搜索了一个解决方案,但我发现的所有内容对我都不起作用.例如,许多人发现了添加kapt"android.arch.persistence.room ..."的解决方案,但gradle不承认"kapt".
Com*_*are 58
kapt
是为了Kotlin.
首先,添加:
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
Run Code Online (Sandbox Code Playgroud)
到你的dependencies
关闭.
然后,升级android.arch.persistence.room:rxjava2
和android.arch.persistence.room:testing
对1.0.0
替代1.0.0-rc1
.
Man*_*ahi 18
对于Kotlin,在gradle文件中将'annotationProcessor'关键字更改为'kapt'.
kapt "android.arch.persistence.room:compiler:1.1.1"
Run Code Online (Sandbox Code Playgroud)
并在依赖文件的顶部添加
apply plugin: 'kotlin-kapt'
Ike*_*bal 10
java.lang.RuntimeException:找不到 com.example.kermovie.data.repository.local.MoviesDatabase 的实现。MoviesDatabase_Impl 不存在
您必须将以下内容添加到您的build.gradle:
apply plugin: 'kotlin-kapt'
dependencies {
implementation 'androidx.room:room-runtime:2.3.0'
implementation "androidx.room:room-ktx:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
}
Run Code Online (Sandbox Code Playgroud)
当有新版本可用时,您可能需要更改版本。
如果您使用Kotlin,请确保您的in档文件中存在此代码。
apply plugin: "kotlin-kapt"
// Room
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
Run Code Online (Sandbox Code Playgroud)
文档:https : //developer.android.com/jetpack/androidx/releases/room
将其添加到您的应用程序模块的buld.gradle
文件中
//The Room persistence library provides an abstraction layer over SQLite
def room_version = "2.3.0"
implementation("androidx.room:room-runtime:$room_version")
annotationProcessor "androidx.room:room-compiler:$room_version"
Run Code Online (Sandbox Code Playgroud)
在您的数据访问对象文件中添加以下注释
@Database(entities = [EntityClassName::class], version = 1)
Run Code Online (Sandbox Code Playgroud)
根据您的项目需要替换实体类名称和数据库版本代码。
如果您使用 kotlin,请将 kapt 插件应用到注释处理器,如下行:
apply plugin: 'kotlin-kapt'
Run Code Online (Sandbox Code Playgroud)
然后替换annotationProcessor "androidx.room:room-compiler:$room_version"
为以下行:
kapt "androidx.room:room-compiler:$room_version"
Run Code Online (Sandbox Code Playgroud)
我看到这里的一些人(Vishu Bhardwaj 和其他类似 StackOverflow 问题的人)抱怨接受的答案对他们不起作用。我认为值得一提的是,我在 MULTI 模块项目中遇到了类似的问题,需要围绕已接受的答案进行技巧。
在我的多模块项目中,房间数据库依赖项主要包含在一个基本模块中,并使用适当的 api 符号导出,但房间代码分为 2 个不同的模块。项目编译时没有警告,但java.lang.RuntimeException: cannot find implementation for database...在运行时引发。
这是通过添加修复的
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
Run Code Online (Sandbox Code Playgroud)
在包含 Room 相关代码的所有模块中。
归档时间: |
|
查看次数: |
17820 次 |
最近记录: |