使用Room所遇到的 Execution failed for task ‘:app:kaptDebugKotlin‘

路径窄处,留一步让人走;
滋味浓时,减三分请人尝。


1. 踩雷

项目中使用Room数据库,需要新增操作方法然后就对相应的Dao进行了一些修改,结果是这样的:

Execution failed for task ':app:kaptReleaseKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

在这里插入图片描述

尼玛哦,no error message 显得辣么鲜艳夺目,光彩照人啊…


2. 排雷

StackOverflow的方案 这次是真指望不上啦。

毛主席教育我们: 自己动手,丰衣足食。那就捋起袖子加油干…

一般情况下,遇到build过程中出现的错误,我们的正确姿势是这样的。

  1. 打开Android Studio 右侧的Gradle;
  2. 找到对应模块下的build,进行build等相关操作;

如下图所示:
在这里插入图片描述

这里双击build之后,详细信息就出来啦,极其方便地帮助我们定位到问题。

错误: Not sure how to convert a Cursor to this method’s return type (void).

在这里插入图片描述


3. 总结

根据上面的错误定位,很容易就把问题给解决掉啦…

错误原因:

SELECT 查询是需要有返回值的。默认返回Cursor。

文档描述如下:

For SELECT queries, Room will infer the result contents from the method’s return type and generate the code that will automatically convert the query result into the method’s return type. For single result queries, the return type can be any data object (also known as POJOs). For queries that return multiple values, you can use List or Array. In addition to these, any query may return Cursor or any query result can be wrapped in a LiveData.

INSERT queries can return void or long. If it is a long, the value is the SQLite rowid of the row inserted by this query. Note that queries which insert multiple rows cannot return more than one rowid, so avoid such statements if returning long.

UPDATE or DELETE queries can return void or int. If it is an int, the value is the number of rows affected by this query.

简单总结一下:

  1. SELECT 查询:任何查询都可以返回Cursor 或者 LiveData包装后的结果。
    1. 单个结果查询:直接返回数据对象(POJOs)
    2. 多个结果查询: 可以使用List 或者 Array
  2. INSERT 查询: 无返回值(void)或者long(行号rowid)
  3. UPDATE 和 DELETE 查询: 无返回值(void) 或者int(受影响的行数)

详细文档地址:Query


4. Room相关知识

1. Room 持久性库
2. 使用 Room 将数据保存到本地数据库
3. Room API 文档


考虑抽出时间,专门来写一下Room

这个错误通常是由于dex方法数超出限制引起的。你可以尝试以下方法解决: 1. 在app的build.gradle文件中添加下面的代码: ```groovy android { defaultConfig { ... multiDexEnabled true } ... } dependencies { implementation 'com.android.support:multidex:1.0.3' } ``` 然后在你的Application类中添加下面的代码: ```java public class MyApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } ... } ``` 2. 如果你使用了很多第三方库,可以尝试使用ProGuard来优化代码。在app的build.gradle文件中添加下面的代码: ```groovy android { ... buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } ``` 然后在app的根目录下创建一个proguard-rules.pro文件,添加下面的代码: ``` -dontwarn okio.** -dontwarn javax.annotation.** -dontwarn com.squareup.okhttp.** -dontwarn retrofit2.Platform$Java8 -dontwarn retrofit2.Platform$Java8$.* -dontwarn retrofit2.Platform$Java9 -dontwarn retrofit2.Platform$Java9$.* -keep class com.google.gson.** { *; } -keep class org.apache.commons.io.** { *; } -keep class retrofit2.** { *; } -keepattributes Signature -keepattributes Exceptions -keepattributes InnerClasses ``` 注意:ProGuard可能会导致某些库出现问题,因此需要进行适当的测试和调整。 3. 如果以上方法都无法解决问题,可以尝试升级Gradle版本或者在项目中使用AndroidX。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值