Android Gradle:如何获取项目的所有模块依赖项
我正在尝试编写一个Gradle插件,在特定Android应用程序的所有源代码上运行Checkstyle,包括应用程序正在使用的所有变体和所有库模块,并生成一个没有重复错误的报告。目前,我能够成功地运行所有变体并生成一份没有重复的报告,但我不知道如何获取应用程序正在使用的模块列表 因此,基本上,如果我有一个具有这种结构的项目:Android Gradle:如何获取项目的所有模块依赖项,android,gradle,checkstyle,gradle-plugin,Android,Gradle,Checkstyle,Gradle Plugin,我正在尝试编写一个Gradle插件,在特定Android应用程序的所有源代码上运行Checkstyle,包括应用程序正在使用的所有变体和所有库模块,并生成一个没有重复错误的报告。目前,我能够成功地运行所有变体并生成一份没有重复的报告,但我不知道如何获取应用程序正在使用的模块列表 因此,基本上,如果我有一个具有这种结构的项目: MyProject +-MyAppFoo +-MyAppBar +-FeatureA +-FeatureB +-FeatureC +-Core 应用程序MyAppFoo依
MyProject
+-MyAppFoo
+-MyAppBar
+-FeatureA
+-FeatureB
+-FeatureC
+-Core
应用程序MyAppFoo
依赖于模块FeatureA
和FeatureC
和FeatureA
依赖于Core
我希望能够访问FeatureA
、Core
和FeatureC
的项目实例,以获取它们的源代码和类路径
我当前的代码是这样的,我将它直接应用于项目的一个应用程序模块(例如,MyAppFoo
):
应用插件:“checkstyle”
后评价{
def变体
if(project.plugins.hasPlugin('com.android.application')){
variants=android.applicationVariants
}else if(project.plugins.hasPlugin('com.android.library')){
variants=android.libraryVariants
}否则{
返回
}
def dependsOn=[]
def类路径
def源
variants.all{variant->
德彭森
返回p.getPath().contains(“${project.projectDir}/src/main/”)
}
}
source+=variant.javaCompiler.source.filter{p->
return!p.getPath()包含(“${project.projectDir}/src/main/”)
}
if(!classpath){
classpath=project.fileTree(variant.javaCompiler.destinationDir)
}否则{
classpath+=project.fileTree(variant.javaCompiler.destinationDir)
}
}
def checkstyle=project.tasks.create“checkstyle”,checkstyle
checkstyle.group=“验证”
checkstyle.dependsOn dependsOn
checkstyle.source源
checkstyle.classpath=类路径
checkstyle.exclude('**/BuildConfig.java')
checkstyle.exclude(“**/R.java”)
checkstyle.exclude(“**/BR.java”)
checkstyle.showVillations为true
project.tasks.getByName(“check”).dependsOn checkstyle
}
我想要的是一个project
列表,其中只包含我的MyAppFoo
正在使用的模块,这是我运行gradle:MyAppFoo:checkstyle
时需要的,我想在模块MyAppFoo
、FeatureA
、Core
和FeatureC
上运行它
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
}