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

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+

相关文章:

  • 使用 Redis 实现 RBAC 权限管理
  • Python毕业设计选题:基于python的酒店推荐系统_django+hadoop
  • 腾讯云ChatBI通过中国信通院大模型驱动的智能数据分析工具专项测试
  • Myplater项目
  • @RestController和@RequestBody注解含义
  • 出现 [ app.json 文件内容错误] app.json: 在项目根目录未找到 app.json (env: Windows,mp 解决方法
  • VSCode本地python包“无法解析导入”
  • 千峰React:脚手架准备+JSX基础
  • Python 文件操作利器:FileUtils 工具类深度剖析
  • 赛前启航 | Azure 应用开发实战指南:开启创意的无限可能
  • MySQL数据库入门到大蛇尚硅谷宋红康老师笔记 基础篇 part 15
  • 工业安全的智能哨兵:AI如何筑起生产线的“数字防火墙“
  • React实现自定义图表(线状+柱状)
  • Spring中Bean的四种实例化方法
  • 对称加密算法——IDEA加密算法
  • C# ConcurrentBag 使用详解
  • Spring Boot实战:拦截器
  • 高效执行自动化用例:分布式执行工具pytest-xdist实战!
  • oracle序列每天重置
  • Java 设计模式总结
  • “80后”计算机专家唐金辉已任南京林业大学副校长
  • 马新民卸任外交部条约法律司司长
  • 万达电影:股东杭州臻希拟减持不超1.3927%公司股份
  • 暴雨及强对流天气黄色预警已发布!南方进入本轮降雨最强时段
  • “救护车”转运病人半路加价,从宝鸡到西安往返都要多收钱
  • 美联储如期按兵不动,强调“失业率和通胀上升的风险均已上升”(声明全文)