I am trying to write a Gradle Plugin that runs Checkstyle over all the source code of a specific Android app, including all the variants and all the library modules that the app module is using and generate a single report without duplicate errors. Currently I am able to run it through all the variants successfully and generate the single file report, but I have no idea how to get the list of modules that the app is using.
So, basically, if I have a project with this structure:
MyProject
+-MyAppFoo
+-MyAppBar
+-FeatureA
+-FeatureB
+-FeatureC
+-Core
And the app MyAppFoo has dependencies on the modules FeatureA and FeatureC and FeatureA depends on Core I would like to be able to access the project instance of FeatureA, Core and FeatureC to get their sources and classpaths.
My current code looks like this and I am applying it to only one app module of the project (eg. MyAppFoo):
/**
* A plugin that lets you use the checkstyle plugin for Android projects.
*/
class AndroidCheckstylePlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.plugins.with {
apply CheckstylePlugin
}
project.afterEvaluate {
def variants
if (hasPlugin(project, AppPlugin)) {
variants = project.android.applicationVariants
} else if (hasPlugin(project, LibraryPlugin)) {
variants = project.android.libraryVariants
} else {
throw new StopExecutionException(
"Must be applied with 'android' or 'android-library' plugin.")
}
def dependsOn = []
def classpath
def source
variants.all { variant ->
dependsOn << variant.javaCompiler
if (!source) {
source = variant.javaCompiler.source.filter { p ->
return p.getPath().contains("${project.projectDir}/src/main/")
}
}
source += variant.javaCompiler.source.filter { p ->
return !p.getPath().contains("${project.projectDir}/src/main/")
}
if (!classpath) {
classpath = project.fileTree(variant.javaCompiler.destinationDir)
} else {
classpath += project.fileTree(variant.javaCompiler.destinationDir)
}
}
def checkstyle = project.tasks.create "checkstyle", Checkstyle
checkstyle.group = "Verification"
checkstyle.dependsOn dependsOn
checkstyle.source source
checkstyle.classpath = classpath
checkstyle.exclude('**/BuildConfig.java')
checkstyle.exclude('**/R.java')
checkstyle.exclude('**/BR.java')
checkstyle.showViolations true
project.tasks.getByName("check").dependsOn checkstyle
}
}
static def hasPlugin(Project project, Class<? extends Plugin> plugin) {
return project.plugins.hasPlugin(plugin)
}
}
What I would like to have is a list of projects containing only the modules that my MyAppFoo is using, this whay when I run gradle :MyAppFoo:checkstyle I want to run it on the modules MyAppFoo, FeatureA, Core and FeatureC.
The crucial part is that I don’t need just the name of the modules, but I need the list of source files of each single module.