Reputation: 49
01-17 14:25:21.567 5084-5084/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.harv.swat, PID: 5084 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harv.swat/com.example.harv.swat.HomeActivity}: java.lang.RuntimeException: cannot find implementation for com.example.harv.swat.model.AppDatabase. AppDatabase_Impl does not exist at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2696) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2757) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6237) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.RuntimeException: cannot find implementation for com.example.harv.swat.model.AppDatabase. AppDatabase_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.example.harv.swat.model.Db.(Db.java:16) at com.example.harv.swat.model.Db.getInstance(Db.java:20) at com.example.harv.swat.HomeActivity.onCreate(HomeActivity.java:28) at android.app.Activity.performCreate(Activity.java:6847) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2649) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2757) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6237) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
public class Db {
AppDatabase adb;
private static Db db;
private Db(Context context){
adb = Room.databaseBuilder(context,
AppDatabase.class, "swat").allowMainThreadQueries().build();
}
public static Db getInstance(Context context){
if(db==null){
db = new Db(context);
}
return db;
}
public MyDAO getDao(){
return adb.userDao();
}}
DOA class
@Dao
public interface MyDAO {
@Query("SELECT * FROM content")
List<Content> getAll();
@Query("SELECT * FROM content WHERE id IN (:userIds)")
List<Content> loadAllByIds(int[] userIds);
/*@Query("SELECT * FROM user WHERE first_name LIKE :first AND "
+ "last_name LIKE :last LIMIT 1")
Content findByName(String first, String last);*/
@Insert
void insertAll(Content... users);
@Delete
void delete(Content user);}
Gradle file
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.0.0"
implementation "android.arch.lifecycle:common-java8:1.0.0"
//annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
// Room
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
}
Upvotes: 3
Views: 11113
Reputation: 1090
The question was asked a long time ago, but in 2024, it has become relevant again with the use of Kotlin Multiplatform. The following line in the Gradle file of the Compose project was a stumbling block for me.
dependencies {
add("kspAndroid", libs.androidx.room.compiler) // Add this line
}
Where
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomVer" }
This block needs to be placed at the root of the Gradle file, on the same level as the plugins or kotlin blocks.
Currently, projects using KMP can have different Gradle file and directory structures. This solution is applicable to projects with the following structure:
Upvotes: 1
Reputation: 8102
Had the same error and the reason for this is missing this line:
@Database(entities = [YourEntityClass::class], version = 1)
You need to annotate your ROOM database class with @Database annotation and also pass in your tables and version.
@Database(entities = [YourEntityClass::class], version = 1)
abstract class YourDatabase : RoomDatabase(){}
Upvotes: 1
Reputation: 1049
It could be possible that the file wasn't regenerated when changes were made. Perhaps try rebuilding the project: Build -> Rebuild
Upvotes: 1