当前位置: 首页 > article >正文

Android GreenDAO 适配 AGP 8.0+

在 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+

http://www.kler.cn/a/554849.html

相关文章:

  • web网络安全:SQL 注入攻击
  • 第150场双周赛:好数字之和、分割正方形 Ⅰ、分割正方形 Ⅱ、最短匹配字符串
  • HBase的安全性考量:保护你的数据不受威胁
  • 蓝桥杯学习大纲
  • DeepSeek部署到本地(解决ollama模型下载失败问题)
  • 【2025最新版】Chrome谷歌浏览器如何能恢复到之前的旧版本
  • 基于单片机的智能网控风扇设计
  • C++:类与对象,定义类和构造函数
  • 【ISO 14229-1:2023 UDS诊断(ECU复位0x11服务)测试用例CAPL代码全解析⑩】
  • CSS盒模
  • 学习 `@PreDestroy`:Java EE/Jakarta EE 生命周期回调
  • Linux top 命令
  • jsherp importItemExcel接口存在SQL注入
  • 【Leetcode】二叉树的最大深度
  • 2025最新面试自用题库---面试使用
  • Redis 持久化:从零到掌握
  • 微信小程序客服消息接收不到微信的回调
  • 【OpenCV】OpenCV 中各模块及其算子的详细分类
  • 多任务(20250210)
  • 如何在在 CentOS 中配置 rsync 实现高效文件同步