Gradle构建报错:计算':app:compileDebugJavaWithJavac'任务属性失败
Gradle构建错误解决:compileDebugJavaWithJavac属性查询异常
错误核心信息
根因1:
org.gradle.api.internal.provider.AbstractProperty$PropertyQueryException:无法计算任务:app:compileDebugJavaWithJavac的属性options.generatedSourceOutputDirectory的值。
根因2:org.gradle.api.InvalidUserCodeException:不支持在任务:app:compileDebugJavaWithJavac完成前查询map(java.io.File task ':app:compileDebugJavaWithJavac' property 'annotationProcessorSourcesDirectory' org.gradle.api.internal.file.DefaultFilePropertyFactory$ToFileTransformer@57cd73ff)的映射值。
问题原因
这个异常是违反了Gradle的懒加载执行机制导致的——annotationProcessorSourcesDirectory是compileDebugJavaWithJavac任务的输出属性,只有该任务执行完成后,这个属性的实际值才会生成。如果在任务还未执行时就直接查询它的映射值,就会触发这个错误。
解决步骤
- 排查项目构建脚本(
build.gradle/build.gradle.kts)或自定义插件,看是否有代码直接调用compileDebugJavaWithJavac的annotationProcessorSourcesDirectory属性的get()/asFile方法,提前获取了未生成的路径值。 - 把直接获取属性值的代码改成懒加载方式,用Gradle的Provider API延迟取值:
错误示例:
正确示例:// 直接获取会触发异常 def generatedDir = tasks.compileDebugJavaWithJavac.options.annotationProcessorSourcesDirectory.asFile// 用Provider持有引用,在需要时才取值 def generatedDirProvider = tasks.compileDebugJavaWithJavac.options.annotationProcessorSourcesDirectory // 绑定到其他任务的输入,让Gradle自动处理依赖顺序 someCustomTask.inputs.dir(generatedDirProvider) - 如果是第三方插件导致的问题,检查插件与当前Gradle版本的兼容性,尝试升级插件到最新稳定版,或者调整Gradle版本到插件支持的范围。
- 清理构建缓存后重新构建,执行命令:
./gradlew clean build --no-build-cache
内容的提问来源于stack exchange,提问作者Amrul Izwan
相关产品推荐
相关产品推荐

