关于安卓greendao打包时报错问题修复
背景
项目在使用greendao的时候,debug安装没有问题,一到打包签名就报了。
环境
win10
jdk17
gradle8+
项目依赖情况
博主的greendao是一个独立的module项目,项目目前只适配了java,不支持Kotlin。然后被外部集成。greendao版本3.0.0。主项目和其他module都是Kotlin+Java混合环境。
问题集合
问题一
> Could not create task ':libpersistence:greendaoPrepare'.
> Cannot use @TaskAction annotation on method DetectEntityCandidatesTask.execute() because interface org.gradle.api.tasks.incremental.IncrementalTaskInputs is not a valid parameter to an action method.
在编译的过程,gradle报错,提示task错误。
解决方法:
在项目根部build.gradle文件,添加一下代码
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.1'
具体添加位置如下图:
这样就能正常进行gradle编译了。
问题二
FAILURE: Build completed with 2 failures.
1: Task failed with an exception.
-----------
* What went wrong:
A problem was found with the configuration of task ':libpersistence:greendao' (type 'DefaultTask').
- Gradle detected a problem with the following location: 'E:\workstation\android\clawclaw\clawclaw\libpersistence\src\main\java'.
Reason: Task ':libpersistence:kaptGenerateStubsReleaseKotlin' uses this output of task ':libpersistence:greendao' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
Possible solutions:
1. Declare task ':libpersistence:greendao' as an input of ':libpersistence:kaptGenerateStubsReleaseKotlin'.
2. Declare an explicit dependency on ':libpersistence:greendao' from ':libpersistence:kaptGenerateStubsReleaseKotlin' using Task#dependsOn.
3. Declare an explicit dependency on ':libpersistence:greendao' from ':libpersistence:kaptGenerateStubsReleaseKotlin' using Task#mustRunAfter.
For more information, please refer to https://docs.gradle.org/8.9/userguide/validation_problems.html#implicit_dependency in the Gradle documentation.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
==============================================================================
这个错误很诡异,按照官方issus,是很难解决的,如果你的greendao模块,原本只支持Java编译。得按以下的步骤进行解决。
(一)对module进行kotlin支持
这个无可厚非的,很简单的配置一下就好了,我目前的kotlin版本是1.9.25,gradle是8.5,配置一下就好了。选中Module中的build.gradle文件,先apply一下Kotlin安卓支持,代码截图如下:
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
id 'org.greenrobot.greendao'
}
然后引入依赖:
"androidx.core:core-ktx:1.7.0"
"org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.10"
上述版本与你们实际版本可能存在差异,但是博主这个版本是实测过的,可以正常打包,具体的版本,应该配合gradle来决定,不能一概而论。
最后,在模块builde.gradle底部增加如下命令的配置:
//适用于greendao打包报错
tasks.configureEach { task ->
if (task.name.matches("\\w*compile\\w*Kotlin")) {
task.dependsOn('greendao')
}
if (task.name.matches("\\w*kaptGenerateStubs\\w*Kotlin")) {
task.dependsOn('greendao')
}
if (task.name.matches("\\w*kapt\\w*Kotlin")) {
task.dependsOn('greendao')
}
}
上述配置的思路,其实就是为了适配kotlin与java混合打包过程中,一些Kotlin编译与Java差异化的一种兼容手段。
配置完上面代码后,就可以正常运行了。
最后,放出greendao模块中,build.gradle文件的全部代码
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
id 'org.greenrobot.greendao'
}
android {
namespace 'com.north.light.libpersistence'
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
}
buildTypes {
release {
minifyEnabled false
consumerProguardFiles 'proguard-rules-lib-greendao.pro'
}
pre {
minifyEnabled false
consumerProguardFiles 'proguard-rules-lib-greendao.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
buildFeatures{
buildConfig = true
}
}
greendao {
// 指定数据库schema版本号,迁移等操作会用到
schemaVersion 1
// 设置生成数据库文件的目录,默认是在build中,可以将生成的文件放到我们的java目录中
targetGenDir 'src/main/java'
// 设置生成的数据库相关文件的包名,默认为entity所在的包名
daoPackage 'com.north.light.libpersistence.gen'
}
dependencies {
api rootProject.ext.dependencies["kotlin"]
api rootProject.ext.dependencies["kotlin-stdlib"]
api project(":libcommonresp")
implementation 'org.greenrobot:greendao:3.3.0'
}
//适用于greendao打包报错
tasks.configureEach { task ->
if (task.name.matches("\\w*compile\\w*Kotlin")) {
task.dependsOn('greendao')
}
if (task.name.matches("\\w*kaptGenerateStubs\\w*Kotlin")) {
task.dependsOn('greendao')
}
if (task.name.matches("\\w*kapt\\w*Kotlin")) {
task.dependsOn('greendao')
}
}
that’s all-----------------------------------------------------------------------------------