Room无法找到实现
Room无法找到实现
我在测试Room数据库时遇到了问题:
当我运行测试时,出现了以下异常:
java.lang.RuntimeException: 无法找到database.TicketDatabase的实现。TicketDatabase_Impl不存在 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)
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"来找到解决方案,但是gradle不识别"kapt"。