我在测试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 …Run Code Online (Sandbox Code Playgroud) 按照设置Room余辉库的教程时,我在Android设备上测试时遇到此错误.
java.lang.RuntimeException:找不到PackageName .AppDatabase的实现.AppDatabase_Impl不存在
我知道有一个类似的问题,但问题是由于kotlin gradle问题.可能重复
测试类:
@RunWith(AndroidJUnit4.class)
public class LocalDatabaseTest {
private PhotoDao mPhotoDao;
private AppDatabase mDb;
@Before
public void createDb() {
Context context = InstrumentationRegistry.getTargetContext();
mDb = Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class).build();
mPhotoDao = mDb.photoDao();
}
@After
public void closeDb() throws IOException {
//mDb.close();
}
@Test
public void testPreConditions() {
assertNotNull(mDb);
}
Run Code Online (Sandbox Code Playgroud)
道:
@Dao
public interface PhotoDao {
@Delete()
public void delete(Photo... photos);
@Update
public void update(Photo ... photos);
@Insert
public void insert(Photo ... photos);
}
Run Code Online (Sandbox Code Playgroud)
数据库:
@Database(entities = {Photo.class}, …Run Code Online (Sandbox Code Playgroud) 我正在kotlin中实现Room persistence lib来实现我的数据库.
以下是我的Entity,Dao和Database类:
Food.kt
@Entity
class Food(@ColumnInfo(name = "food_name") var foodName: String,
@ColumnInfo(name = "food_desc") var foodDesc: String,
@ColumnInfo(name = "protein") var protein: Double,
@ColumnInfo(name = "carbs") var carbs: Double,
@ColumnInfo(name = "fat") var fat: Double)
{
@ColumnInfo(name = "id")
@PrimaryKey(autoGenerate = true)
var id: Long = 0
@ColumnInfo(name = "calories")
var calories: Double = 0.toDouble()
}
Run Code Online (Sandbox Code Playgroud)
PersonalizedFood.kt
@Entity(primaryKeys = arrayOf("food_id","date"))
class PersonalizedFood(@ColumnInfo(name = "quantity") var quantity: Int,
@ColumnInfo(name = "unit") var unit: …Run Code Online (Sandbox Code Playgroud) 因此,自从添加新的Room android架构库以来,这已经开始发生.我遇到了AppDatabase_Impl不存在的问题,我通过将kapt添加到注释中来修复:
我有其他错误,我怀疑是由于AS,Kotlin和Java 8,所以我尝试更新到AS 3.0
我在尝试构建时遇到以下错误:
Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources, :cryptocurrency-icons:generateDebugSources, :cryptocurrency-icons:mockableAndroidJar, :cryptocurrency-icons:generateDebugAndroidTestSources, :cryptocurrency-icons:compileDebugSources, :cryptocurrency-icons:compileDebugUnitTestSources, :cryptocurrency-icons:compileDebugAndroidTestSources]
Error:Circular dependency between the following tasks:
:app:compileDebugKotlin
+--- :app:dataBindingExportBuildInfoDebug
| \--- :app:compileDebugKotlin (*)
\--- :app:kaptDebugKotlin
\--- :app:dataBindingExportBuildInfoDebug (*)
(*) - details omitted (listed previously)
Information:BUILD FAILED in 1s
Information:1 error
Information:0 warnings
Information:See complete output in console
Run Code Online (Sandbox Code Playgroud)
我的gradle文件看起来像:
项目gradle
// Top-level build file where you can add configuration options common to …Run Code Online (Sandbox Code Playgroud) 我正在kotlin中实现Room persistence lib来实现我的数据库.在Android Room Persistences库和Kotlin线程中询问相同的问题,应用这些解决方案会导致不同的gradle错误:
以下是我的Entity,Dao和Database类:
Food.kt
@Entity
class Food(@ColumnInfo(name = "food_name") var foodName: String,
@ColumnInfo(name = "food_desc") var foodDesc: String,
@ColumnInfo(name = "protein") var protein: Double,
@ColumnInfo(name = "carbs") var carbs: Double,
@ColumnInfo(name = "fat") var fat: Double)
{
@ColumnInfo(name = "id")
@PrimaryKey(autoGenerate = true)
var id: Long = 0
@ColumnInfo(name = "calories")
var calories: Double = 0.toDouble()
}
Run Code Online (Sandbox Code Playgroud)
PersonalizedFood.kt
@Entity(primaryKeys = arrayOf("food_id","date"))
class PersonalizedFood(@ColumnInfo(name = "quantity") var quantity: Int,
@ColumnInfo(name …Run Code Online (Sandbox Code Playgroud)