Android Gradle配置:只编译修改过的模块
{"title":"Android Gradle配置:只编译修改过的模块","description":"本文介绍如何在Android项目的根目录build.gradle文件中配置gradle,让每次运行时只编译修改过的模块,提高开发效率。","keywords":"Android, Gradle, 编译, 优化, 模块","content":""gradle.projectsEvaluated {\n def modules = subprojects.findAll { it.hasProperty('android') }\n\n tasks.withType(JavaCompile) {\n options.compilerArgs << "-Xmaxerrs" << "1"\n }\n\n tasks.matching {\n it.name.startsWith('compile') && it.name.endsWith('JavaWithJavac')\n }.all { task ->\n task.inputs.property("moduleName", task.name.replaceAll('compile|JavaWithJavac', '').toLowerCase())\n task.outputs.upToDateWhen { false }\n }\n\n gradle.taskGraph.whenReady { taskGraph ->\n modules.each { module ->\n def taskName = ":${module.name}:compile${module.name.capitalize()}JavaWithJavac"\n def task = gradle.taskGraph.find { it.path == taskName }\n if (task) {\n task.inputs.files = files(task.inputs.files.files.findAll {\n file -> file.lastModified() > module.file('build/outputs').lastModified()\n })\n }\n }\n }\n}"\n这段代码会在每次运行时检查每个子模块的修改时间,如果某个模块的修改时间晚于上次编译的时间,则会重新编译该模块。\n\n请注意,这段代码需要放在项目根目录的build.gradle文件中,而不是模块的build.gradle文件中。\n\n希望对你有帮助!"}
原文地址: https://www.cveoy.top/t/topic/puI0 著作权归作者所有。请勿转载和采集!