gpt4 book ai didi

java - 如何使用gradle在android项目中生成jacoco报告

转载 作者:行者123 更新时间:2023-11-29 09:30:21 25 4
gpt4 key购买 nike

在java项目中很简单,gradle的示例文件夹中有两个实例。但是当我尝试在 android 项目中这样做时,出现了很多问题。

无法添加

{apply plugin: 'java'}

test {
jacoco{
excludes = ['org/bla/**']
includes = ['com/bla/**']
append = false
}
}

和android插件有些冲突,test是属于java插件的,不知道怎么办。我可以进行构建、checkstyle、pmd、findbugs 和测试,但 jacoco 报告。

我的gradle文件如下:

//Import android test dependencies
import com.android.build.gradle.api.TestVariant


//Load classpath and define the repository.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}


//Sub project,we can add a lot of sub project here.
project('TVEAndroid')
{

//Load plugins
apply plugin: 'android'
apply plugin: 'jacoco'
apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'pmd'


//This is different with the one above,the previous one is just for load classpath,this one is for the real build.
repositories {
mavenCentral()
}

//Load dependencies,We will use nesux to hold the repositories in the future,so it will be changed.
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}

//Jacoco plugin information declaration,but jacoco didn't work here,but it works in the java project with the same configuration.
jacoco {
toolVersion = "0.6.2.201302030002"
reportsDir = file("$buildDir/customJacocoReportDir")
}


//Define android build information
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}

//Set the build path,the root folder
instrumentTest.setRoot('../TVEAndroidTest')

//Set the code and resuource path for build
instrumentTest {
java { srcDirs = [
'../TVEAndroidTest/src/'
] }
res.srcDirs = ['res']
assets.srcDirs = [
'../TVEAndroidTest/assets'
]
resources.srcDirs = [
'../TVEAndroidTest/src'
]

}


//Define the package name for build
defaultConfig {
testPackageName "com.accedo.android.tve.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}

// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}

jacoco {
append = false
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
classDumpFile = file("$buildDir/jacoco/classpathdumps")
}

//PMD task
task pmd(type: Pmd) {
ruleSetFiles = files('../config/quality/pmd/pmd-ruleset.xml')
ruleSets = ["basic", "braces", "strings"]
source = android.sourceSets.main.java.srcDirs
}

//CheckStyle task
task checkstyle(type: Checkstyle) {
configFile file('../config/quality/checkstyle/checkstyle.xml')
source android.sourceSets.main.java.srcDirs
include '**/*.java'
exclude '**/gen/**'

classpath = files( project.configurations.compile.asPath )
}

//Findbugs task
task findbugs(type: FindBugs) {

excludeFilter file('../config/quality/findbugs/findbugs-filter.xml')
classes = fileTree('build/classes/debug')
source = android.sourceSets.main.java.srcDirs
classpath = files( project.configurations.compile.asPath )

reports {
xml {
destination "build/reports/findbugs/findbugs.xml"
}
}

effort = 'max'
}

jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}

}

当前的问题是“找不到方法 jacocoTestReport() ...balabala”

任何建议将不胜感激!

最佳答案

jacoco 插件与 Android 不兼容。要获得 jacoco 报告,您需要自行检测您的类(class)。为此,请将其添加到您的 gradle 构建配置文件中:

configurations {
codeCoverage
codeCoverageAnt
}
dependencies {
codeCoverage 'org.jacoco:org.jacoco.agent:0.5.10.201208310627:runtime@jar'
codeCoverageAnt 'org.jacoco:org.jacoco.ant:0.5.10.201208310627'
}

tasks.whenTaskAdded { task ->
if (task.name == 'testDefaultFlavorDebug') { /* Name of your test task */
task.jvmArgs "-javaagent:${configurations.codeCoverage.asPath}=destfile=${project.buildDir.path}/coverage-results/jacoco.exec,sessionid=HSServ,append=false",
'-Djacoco=true',
'-Xms128m',
'-Xmx512m',
'-XX:MaxPermSize=128m'
}
}

运行测试后,“build/coverage-results”目录中将有一个 jacoco.exec 文件

关于java - 如何使用gradle在android项目中生成jacoco报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19792067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com