首页 > 解决方案 > Gradle:如何获取项目的所有模块依赖项

问题描述

我正在尝试编写一个 Gradle 插件,该插件在特定 Android 应用程序的所有源代码上运行 Checkstyle,包括该应用程序正在使用的所有变体和所有库模块,并生成一个没有重复错误的报告。目前我能够成功地通过所有变体运行它并生成没有重复的报告,但我不知道如何获取应用程序正在使用的模块列表。

所以,基本上,如果我有一个具有这种结构的项目:

MyProject
+-MyAppFoo
+-MyAppBar
+-FeatureA
+-FeatureB
+-FeatureC
+-Core

并且该应用程序MyAppFoo依赖于模块FeatureAFeatureC并且FeatureA依赖于Core我希望能够访问的项目实例FeatureACoreFeatureC获取它们的源和类路径。

我当前的代码如下所示,我将其直接应用于项目的一个应用程序模块(例如。MyAppFoo):

apply plugin: 'checkstyle'

afterEvaluate {
    def variants
    if (project.plugins.hasPlugin('com.android.application')) {
        variants = android.applicationVariants
    } else if (project.plugins.hasPlugin('com.android.library')) {
        variants = android.libraryVariants
    } else {
        return
    }

    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
}

我想要的是一个project仅包含我MyAppFoo正在使用的模块的 s 列表,当我运行时,我gradle :MyAppFoo:checkstyle想在模块MyAppFooFeatureA和.CoreFeatureC

标签: androidgradlecheckstylegradle-plugin

解决方案


推荐阅读