我在测试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)
Class 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);
}
}
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'
}
我真的不知道这个实现应该是什么,我搜索了一个解决方案,但我找到的并不适合我。例如,许多人找到了一个解决方案,添加了kapt" android.arch.persistence.room ..."但是gradle没有认识到" kapt"。
答案 0 :(得分:50)
kapt
适用于Kotlin。
首先,添加:
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
到dependencies
关闭。
然后,将android.arch.persistence.room:rxjava2
和android.arch.persistence.room:testing
升级为1.0.0
而不是1.0.0-rc1
。
答案 1 :(得分:12)
对于Kotlin,在gradle文件中将“ annotationProcessor”关键字更改为“ kapt”。
kapt "android.arch.persistence.room:compiler:1.1.1"
并在依赖文件的顶部添加
apply plugin: 'kotlin-kapt'
答案 2 :(得分:6)
如果您使用Kotlin,请确保您的in档文件中存在此代码。
apply plugin: "kotlin-kapt"
// Room
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
答案 3 :(得分:3)
我在这里看到一些人(Vishu Bhardwaj和其他人在类似的StackOverflow问题上)抱怨公认的答案对他们不起作用。我认为值得一提的是,我在MULTI模块项目中遇到了类似的问题,该问题需要技巧才能解决。
在我的多模块项目中,会议室数据库的依赖关系主要包含在一个基本模块中,并使用适当的api符号导出,但会议室代码沿2个不同的模块拆分。在没有警告的情况下编译的项目,但 java.lang.RuntimeException:找不到数据库的实现... 在运行时引发。
此问题已通过添加
修复annotationProcessor "android.arch.persistence.room:compiler:$room_version"
所有包含与房间相关的代码的模块。
答案 4 :(得分:2)
就我而言,在数据库类上方添加以下注释即可解决问题。
@Database(entities = {Entities.class}, version = 1, exportSchema = false)
public abstract class TheDatabase extends RoomDatabase {...}
感谢@nicklocicero。
答案 5 :(得分:1)
此gradle效果很好。请注意开始时的插件kotlin-kapt。至关重要。
apply plugin: 'kotlin-kapt'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "????"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'android.arch.persistence.room:runtime:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
}
答案 6 :(得分:1)
我按照上述答案中的每个人所做的仍然没有运气。
我的问题是,我正在主线程上插入。
roomDatabase = Room.databaseBuilder(DexApplication.getInstance(),MyRoomDatabase.class,"db_name")
.fallbackToDestructiveMigration() // this is only for testing,
//migration will be added for release
.allowMainThreadQueries() // this solved the issue but remember that its For testing purpose only
.build();
说明:
.allowMainThreadQueries()
允许您在主线程上运行查询。但是请记住要删除它并实现RxJava,Live Data或任何其他用于查询数据库的后台进程机制。 希望会有所帮助。
答案 7 :(得分:1)
java.lang.RuntimeException:无法找到实现 com.example.kermovie.data.repository.local.MoviesDatabase。 MoviesDatabase_Impl不存在
您必须在 build.gradel 上添加以下代码:
apply plugin: 'kotlin-kapt'
dependencies {
implementation 'androidx.room:room-runtime:2.2.5'
implementation "androidx.room:room-ktx:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
}
答案 8 :(得分:0)
在插件和依赖项下方添加
应用插件:“ kotlin-kapt”
kapt“ android.arch.persistence.room:compiler:1.1.1”
答案 9 :(得分:0)
You can see your database tables on DB browser for SQLITE.
In android studio
1. Click on View -> Tool Windows -> Device File Explorer
2. Select your project package name -> database -> select all file and save on desktop
Install **DB browser for SQLITE**
1. sudo apt-get install sqlitebrowser //write on your terminal for install DB browser
2. install DB browser for sqlite
3. click on Open database and select file where you had saved your data
4. click on Brower Data and see your tables
This is all where i have see my android room database
答案 10 :(得分:0)
我将添加解决问题的完整答案
首先添加android x的所有房间相关性 第二个添加Apply插件:“ kotlin-kapt”,并用keeper替换房间编译器的注解处理器
答案 11 :(得分:0)
我来晚了一点,但是我遇到了同样的问题,但是没有一个答案对我有帮助。因此,我希望我的经验能对其他人有所帮助。 Room在执行仪器测试时找不到实现,因此在gradle文件中添加以下行即可解决问题
kaptAndroidTest "androidx.room:room-compiler:$room_version"
如果您将注释处理器用于androidTest或测试源,则相应的kapt配置将分别命名为kaptAndroidTest和kaptTest
答案 12 :(得分:0)
这意味着您的房间运行时依赖项丢失.. 请在您的 build.gradle(module) 文件中添加以下依赖项。这将解决您的问题:)
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <Windows.h>
struct timeb timenow;
int main() {
ftime(&timenow);
printf("%ds %dms\n", timenow.time, timenow.millitm);
Sleep(1503);
ftime(&timenow);
printf("%ds %dms\n", timenow.time, timenow.millitm);
return 0;
}
答案 13 :(得分:0)
将此添加到您的应用模块的 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"
在您的数据访问对象文件中添加以下注释
@Database(entities = [EntityClassName::class], version = 1)
根据您的项目需要替换实体类名称和数据库版本代码。
如果您使用的是 kotlin,请将 kapt 插件应用到注释处理器,如下行:
apply plugin: 'kotlin-kapt'
然后将 annotationProcessor "androidx.room:room-compiler:$room_version"
替换为以下行:
kapt "androidx.room:room-compiler:$room_version"