Android Room使用时,运行App启动Crash:cannot find implementation for......_Impl does not exist

起因

在接入Jetpack Room组件,按照Android给的官方文档(点击前往)添加依赖时,编译没问题,但是一运行App,就遇到Crash。堆栈大概长这个样子:

10-17 21:27:23.968 14407 14407 E AndroidRuntime: FATAL EXCEPTION: main
10-17 21:27:23.968 14407 14407 E AndroidRuntime: Process: a.b.c, PID: 14407
10-17 21:27:23.968 14407 14407 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{a.b.c/a.b.c.MainActivity}: java.lang.RuntimeException: cannot find implementation for a.b.c.data.BookRoomDatabase. BookRoomDatabase_Impl does not exist
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3530)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3690)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2126)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:233)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:8053)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: Caused by: java.lang.RuntimeException: cannot find implementation for a.b.c.data.BookRoomDatabase. BookRoomDatabase_Impl does not exist
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at androidx.room.Room.getGeneratedImplementation(Room.java:97)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1358)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.data.BookRoomDatabase$Companion.getDatabase(BookRoomDatabase.kt:31)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MyApplication$bookDb$2.invoke(MyApplication.kt:13)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MyApplication$bookDb$2.invoke(MyApplication.kt:12)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MyApplication.getBookDb(MyApplication.kt:12)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MyApplication$bookRepository$2.invoke(MyApplication.kt:17)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MyApplication$bookRepository$2.invoke(MyApplication.kt:16)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MyApplication.getBookRepository(MyApplication.kt:16)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MainActivity$mBookViewModel$2.invoke(MainActivity.kt:21)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MainActivity$mBookViewModel$2.invoke(MainActivity.kt:20)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:52)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MainActivity.getMBookViewModel(MainActivity.kt:20)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at a.b.c.MainActivity.onCreate(MainActivity.kt:30)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:8159)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:8131)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1308)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3503)
10-17 21:27:23.968 14407 14407 E AndroidRuntime: 	... 11 more

分析

从堆栈信息看,可以得出是没有找到BookRoomDatabase_Impl这类导致的。简单了解了下Room的原理,这个文件应该是Room自动生成的才对。难道说没生成?那为什么能编译通过并正常安装呢?

解决

谷歌一番后,发现是在build.gradle中添加依赖出了问题。
需要将

annotationProcessor "androidx.room:room-compiler:$version_room"

改为

kapt "androidx.room:room-compiler:$version_room"

即把annotationProcessor替换成kapt

下面来说说简要原因。

官方给的参考是这样添加依赖:
Android Room使用时,运行App启动Crash:cannot find implementation for......_Impl does not exist_第1张图片
在项目中也是这么添加的。但是,注意到annotationProcessor这个关键字没有。这个是用于处理Java文件中的注解,而项目的语言采用的是Kotlin,需要使用kapt关键字。又了解到,Room最早是采用Java作为主要语言进行开发的。所以,这里其实是官方文档给的有问题,当然,这也是自身的技术储备不足。

你可能感兴趣的:(Android/安卓,android,java,kotlin,jetpack)