Gradle buildSrc produces unexpected results when implementation
as a project
hi, I use buildSrc to make a gradle plugin to generate some resource files at compile time for the project that uses it.
- This Plugin uses some java classes in buildSrc, and I hope that other projects can also access these classes at runtime.
- This Plugin uses some resource files in buildSrc.
|-- app
|-- build.gradle
|--buildSrc
|--src/main
|-- java /*My java class is in this dir*/
|-- groovy /*My plugin is in this dir*/
|-- resources /*My resources is in this dir*/
|-- custom
|-- build.gradle
custom/build.gradle
/* Some plugin declarations, DSLs about android that are useless for analysis are omitted.*/
// “my plugin” refers to the gradle plugin I coded
apply plugin: “my plugin”
dependencies {
implementation project(":buildSrc")
}
buildSrc/build.gradle
apply plugin: "java"
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
afterEvaluate { project ->
def canProcessResources = false
project.tasks.each{
if (it.name == "compileGroovy"){
canProcessResources = true
}
}
project.tasks.processResources.enabled = canProcessResources
}
I found out that buildSrc includes the ‘compileGroovy’ task when it is automatically executed. When I use ''implementation project(":buildSrc")", because I declare “apply plugin: “java”” in “buildSrc/build.gradle”, there is no ‘compileGroovy’ task to execute. So I want to according to this phenomenon ti controls the “enabled” of the “processResources” task.
My question is:
When I generated the debug apk package, I observed the result of task execution and found that the processResources task was skipped, but the apk package still contains all the content under buildSrc/src/main/resources.
> Configure project :buildSrc
> Task :buildSrc:compileJava UP-TO-DATE
> Task :buildSrc:compileGroovy UP-TO-DATE
> Task :buildSrc:processResources UP-TO-DATE
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:jar UP-TO-DATE
> Task :buildSrc:assemble UP-TO-DATE
> Task :buildSrc:compileTestJava NO-SOURCE
> Task :buildSrc:compileTestGroovy NO-SOURCE
> Task :buildSrc:processTestResources NO-SOURCE
> Task :buildSrc:testClasses UP-TO-DATE
> Task :buildSrc:test NO-SOURCE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE
> Configure project :app
> Configure project :buildSrc
> Configure project :uicommon
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :custom:preBuild UP-TO-DATE
> Task :custom:preDebugBuild UP-TO-DATE
> Task :custom:compileDebugAidl NO-SOURCE
> Task :app:compileDebugAidl NO-SOURCE
> Task :custom:packageDebugRenderscript NO-SOURCE
> Task :app:compileDebugRenderscript NO-SOURCE
> Task :app:checkDebugManifest
> Task :app:generateDebugBuildConfig
> Task :app:mainApkListPersistenceDebug
> Task :app:generateDebugResValues
> Task :app:generateDebugResources
> Task :custom:compileDebugRenderscript NO-SOURCE
> Task :custom:generateDebugResValues
> Task :custom:generateDebugResources
> Task :custom:packageDebugResources
> Task :app:createDebugCompatibleScreenManifests
> Task :custom:checkDebugManifest
> Task :buildSrc:compileJava UP-TO-DATE
> Task :buildSrc:processResources SKIPPED
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:jar
> Task :custom:generateDebugBuildConfig
> Task :app:mergeDebugShaders
> Task :app:compileDebugShaders
> Task :app:generateDebugAssets
> Task :custom:mergeDebugShaders
> Task :custom:compileDebugShaders
> Task :custom:generateDebugAssets
> Task :custom:packageDebugAssets
> Task :app:mergeDebugAssets
> Task :app:processDebugJavaRes NO-SOURCE
> Task :custom:processDebugJavaRes NO-SOURCE
> Task :app:checkDebugDuplicateClasses
> Task :app:desugarDebugFileDependencies
> Task :custom:parseDebugLibraryResources
> Task :app:validateSigningDebug
> Task :app:signingConfigWriterDebug
> Task :app:mergeDebugJniLibFolders
> Task :custom:mergeDebugJniLibFolders
> Task :custom:processDebugManifest
> Task :app:processDebugManifest
> Task :app:mergeDebugResources
> Task :custom:mergeDebugNativeLibs
> Task :custom:stripDebugDebugSymbols
> Task :custom:transformNativeLibsWithIntermediateJniLibsForDebug
> Task :custom:generateDebugRFile
> Task :app:processDebugResources
> Task :custom:compileDebugKotlin
> Task :app:mergeExtDexDebug
> Task :custom:javaPreCompileDebug
> Task :custom:compileDebugJavaWithJavac
> Task :custom:bundleLibResDebug
> Task :custom:bundleLibRuntimeDebug
> Task :custom:bundleLibCompileDebug
> Task :app:compileDebugKotlin
> Task :app:mergeDebugNativeLibs
> Task :app:stripDebugDebugSymbols
> Task :app:mergeLibDexDebug
> Task :app:javaPreCompileDebug
> Task :app:compileDebugJavaWithJavac
> Task :app:compileDebugSources
> Task :app:transformClassesWithDexBuilderForDebug
> Task :app:mergeProjectDexDebug
> Task :app:mergeDebugJavaResource
> Task :app:packageDebug
> Task :app:assembleDebug
myResourses is a file under buildSrc/src/main/resources which should not be included in the apk because of “processResources skipped”.
Additional note: I’ve seen some answers saying that using “implementation project(”:buildSrc")" in a custom project will throw an error. But in my practice, after using “apply plugin: “java”” in buildSrc/build.gradle, it works and accesses the java class at runtime, it’s just that the resources file doesn’t behave correctly.
I don’t want to put the files in buildSrc/src/main/resources into the apk package, which may introduce extra security issues.
My gradle version is 5.6.4