在 Android 中使用 GreenDao,由于 GreenDao 现在不维护,所以更新到新版本的 Gradle 经常出问题,在这记录一些升级遇到的问题,并且记录解决方案。
博主博客
- https://blog.uso6.com
- https://blog.csdn.net/dxk539687357
一、‘:app:greendao’ without declaring an explicit or implicit dependency.
1.1 Gradle 8.0.2
从 Gradle 7.x 升级到 Gradle 8.x 会遇到以下错误
org.gradle.internal.execution.WorkValidationException: Some problems were found with the configuration of task ':app:greendao' (type 'DefaultTask').
- Gradle detected a problem with the following location: '/Users/nukix/AndroidStudioProjects/Demo/app/src/main/java'.
Reason: Task ':app:compileOfficialDebugKotlin' uses this output of task ':app: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 ':app:greendao' as an input of ':app:compileOfficialDebugKotlin'.
2. Declare an explicit dependency on ':app:greendao' from ':app:compileOfficialDebugKotlin' using Task#dependsOn.
3. Declare an explicit dependency on ':app:greendao' from ':app:compileOfficialDebugKotlin' using Task#mustRunAfter.
Please refer to https://docs.gradle.org/8.0.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
groovy
修改项目根目录 build.gradle
为以下内容
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.0.2'
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.1'
}
}
修改 app 模块下的 build.grade
为以下内容
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'org.greenrobot.greendao'
}
greendao {
schemaVersion 1
daoPackage 'com.uso6.greendao'
targetGenDir 'src/main/java'
}
tasks.whenTaskAdded { task ->
if (task.name.matches("compile\w*Kotlin")) {
task.dependsOn('greendao')
}
}
dependencies {
implementation 'org.greenrobot:greendao:3.3.0'
}
kotlin
修改项目根目录 build.gradle.kts
为以下内容
buildscript {
val agp_version: String by extra("8.1.2")
repositories {
gradlePluginPortal()
}
dependencies {
...
classpath("org.greenrobot:greendao-gradle-plugin:3.3.1")
}
}
plugins {
id("com.android.application") version("8.0.2") apply false
id("com.android.library") version("7.4.1") apply false
id("org.jetbrains.kotlin.android") version("1.8.10") apply false
}
修改 app 模块下的 build.grade.kts
为以下内容
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
id("org.greenrobot.greendao")
}
android {
...
greendao {
schemaVersion = 1
daoPackage = "com.uso6.greendao"
targetGenDir = file("src/main/java")
}
tasks.configureEach {
if (this.name.matches(Regex("compile\\w*Kotlin"))) {
this.dependsOn("greendao")
}
}
}
dependencies {
implementation("org.greenrobot:greendao:3.3.0")
}
1.2 Gradle 8.12.1
升级到 8.12.1 会遇到以下错误(不一定是 8.12.1 版本,只是我在这个版本遇到了)
org.gradle.internal.execution.WorkValidationException: A problem was found with the configuration of task ':app:kaptGenerateStubsDebugKotlin' (type 'KaptGenerateStubsTask').
- Gradle detected a problem with the following location: '/Users/nukix/AndroidStudioProjects/Demo/app/src/main/java'.
Reason: Task ':app:kaptGenerateStubsDebugKotlin' uses this output of task ':app: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 ':app:greendao' as an input of ':app:kaptGenerateStubsDebugKotlin'.
2. Declare an explicit dependency on ':app:greendao' from ':app:kaptGenerateStubsDebugKotlin' using Task#dependsOn.
3. Declare an explicit dependency on ':app:greendao' from ':app:kaptGenerateStubsDebugKotlin' using Task#mustRunAfter.
For more information, please refer to https://docs.gradle.org/8.12.1/userguide/validation_problems.html#implicit_dependency in the Gradle documentation.
这个问题跟上面的几乎一样,所以增加一个判断条件即可。
groovy
修改 app 模块下的 build.grade
为以下内容
tasks.whenTaskAdded { task ->
if (task.name.matches("compile\w*Kotlin")
|| task.name.matches("kapt\\w*Kotlin")) {
task.dependsOn('greendao')
}
}
kotlin
修改 app 模块下的 build.grade.kts
为以下内容
tasks.configureEach {
if (this.name.matches(Regex("compile\\w*Kotlin")) || this.name.matches(Regex("kapt\\w*Kotlin"))) {
this.dependsOn("greendao")
}
}
1.3 在 GitHub issues 里面有个老哥给出来的答案更完整,可以直接使用。
groovy
tasks.whenTaskAdded { task ->
if (task.name.matches("\\w*compile\\w*Kotlin")
|| task.name.matches("\\w*kaptGenerateStubs\\w*Kotlin")
|| task.name.matches("\\w*kapt\\w*Kotlin")) {
task.dependsOn('greendao')
}
}
kotlin
tasks.configureEach {
if (this.name.matches(Regex("\\w*compile\\w*Kotlin")) || this.name.matches(Regex("\\w*kaptGenerateStubs\\w*Kotlin")) || this.name.matches(Regex("\\w*kapt\\w*Kotlin"))) {
this.dependsOn("greendao")
}
}
参考文献
- The project encountered errors after updating Gradle Version 7.6.1 to Gradle Version 8.0+