android gradle + junit + jacoco 集成jenkins,sonar系统
2017-11-28 13:58
387 查看
首先gradle 文件配置如下:
apply plugin: 'jacoco'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.javaandroidunittestdemo"
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug{
testCoverageEnabled true
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.+'
testCompile 'junit:junit:4.12'
}
jacoco{
toolVersion = "0.7.7.201606060606"
}
task jacocoTestReport(type: JacocoReport,dependsOn:"testDebugUnitTest") {
def coverageSourceDirs = [
"src"
]
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
//ignoreFailures = true
jacoco {
reportsDir = file("../report/test-results/")
}
def utilTest = '/build/jacoco/testDebugUnitTest.exec'
executionData = fileTree(utilTest)
reports {
xml{
enabled true
//Following value is a file
destination "${buildDir}/reports/jacoco/xml/jacoco.xml"
}
csv.enabled false
html{
enabled true
//Following value is a folder
destination "${buildDir}/reports/jacoco/html"
}
}
classDirectories = fileTree(
dir: "./build/intermediates/classes/debug",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
sourceDirectories = files(coverageSourceDirs)
additionalSourceDirs = files(coverageSourceDirs)
}
注意:这个 debug{
testCoverageEnabled true
}
一定要打开,因为我们对debug进行单元测试。
在jenkins上我们配置gradle script:
然后配置sonar-scanner:
指明 jacoco.exec文件和 test-result文件路径。
有的用户直接在gradle 脚本里面添加build.
这样就是直接执行用户的 gradle脚本。
但是用户有时候在脚本里面配置了其他测试:
def coverageSourceDirs = [
'../epa_new_android/src/main/java'
]
task jacocoTestReport(type: JacocoReport, dependsOn: ["testDebugUnitTest", "connectedDebugAndroidTest"]) {
doFirst {
delete('../report/testUnitTest.exec')
delete('../report/androidTestUnitTest.ec')
// File file1 = new File('../report/test-results/jacocoTestReport');
// file1.deleteDir();
delete('../report/test-results/')
}
jacoco {
reportsDir = file("../report/test-results/")
}
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '/build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
def utilTest = '/build/jacoco/testDebugUnitTest.exec'
def androidTestDir = '/build/outputs/code-coverage/connected/'
def androidTest
def androidTestFileName;
FileTree iotTree = fileTree(dir: androidTestDir, includes: ['*.ec'])
iotTree.each {
File file ->
androidTestFileName = file.name
androidTest = file.path
}
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files(utilTest, androidTest)
reports {
xml.enabled = true
html.enabled = true
}
doLast {
copy {
from utilTest
into '../report'
rename("testDebugUnitTest.exec", 'testUnitTest.exec')
}
copy {
from androidTest
into '../report'
rename(androidTestFileName, 'androidTestUnitTest.ec')
}
copy {
from "/build/test-results"
into '../report/test-results/unittest'
}
}
}
connectedDebugAndroidTest是关于模拟机的测试,这样在执行build gradle 之前需要改造。
但是 android 项目规则不能使用java 规则,要安装 android Lint 插件,然后指定规则:
其中,sonar.projectKey和sonar.projectName随便填不与其他项目重复的就好,一般是项目名;sonar.sources指向Java代码目录;sonar.binaries指向build后产生classes目录,一般AS和eclipse不同;sonar.profile为代码检查规则,就是我们需要安装的 android lint.
android lint的下载:
在 sonarqube官网,选择sonar plugin ,选择java :
Install SonarJava (see Installing a Plugin for more details). By default SonarJava is provided out of the box with SonarQube.
Execute analysis:For Maven projects, use the SonarQube Scanner for Maven by executing the following command from the root directory of the project:
For Gradle projects, declare the
Then use the SonarQube Scanner for Gradle by executing the following command from the root directory of the project:
Follow the link provided at the end of the analysis to browse your project's quality in SonarQube UI.
Java bytecode is requiredAnalyzing a Java project without providing the Java bytecode produced by
provide the Java Bytecode for more accurate analysis
handle correctly the java version used by source code within projects: Handling Java Source Version
analyse other java-related files: Analyse maven pom.xml files
use additional java-specific plugins like Cobertura or Android Lint
create your own Custom Rules
点击android Lint 跳转到git,目前android Lint 有0.1,1.0,1.1三个版本。
apply plugin: 'jacoco'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.javaandroidunittestdemo"
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug{
testCoverageEnabled true
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.+'
testCompile 'junit:junit:4.12'
}
jacoco{
toolVersion = "0.7.7.201606060606"
}
task jacocoTestReport(type: JacocoReport,dependsOn:"testDebugUnitTest") {
def coverageSourceDirs = [
"src"
]
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
//ignoreFailures = true
jacoco {
reportsDir = file("../report/test-results/")
}
def utilTest = '/build/jacoco/testDebugUnitTest.exec'
executionData = fileTree(utilTest)
reports {
xml{
enabled true
//Following value is a file
destination "${buildDir}/reports/jacoco/xml/jacoco.xml"
}
csv.enabled false
html{
enabled true
//Following value is a folder
destination "${buildDir}/reports/jacoco/html"
}
}
classDirectories = fileTree(
dir: "./build/intermediates/classes/debug",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
sourceDirectories = files(coverageSourceDirs)
additionalSourceDirs = files(coverageSourceDirs)
}
注意:这个 debug{
testCoverageEnabled true
}
一定要打开,因为我们对debug进行单元测试。
在jenkins上我们配置gradle script:
然后配置sonar-scanner:
指明 jacoco.exec文件和 test-result文件路径。
有的用户直接在gradle 脚本里面添加build.
这样就是直接执行用户的 gradle脚本。
但是用户有时候在脚本里面配置了其他测试:
def coverageSourceDirs = [
'../epa_new_android/src/main/java'
]
task jacocoTestReport(type: JacocoReport, dependsOn: ["testDebugUnitTest", "connectedDebugAndroidTest"]) {
doFirst {
delete('../report/testUnitTest.exec')
delete('../report/androidTestUnitTest.ec')
// File file1 = new File('../report/test-results/jacocoTestReport');
// file1.deleteDir();
delete('../report/test-results/')
}
jacoco {
reportsDir = file("../report/test-results/")
}
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '/build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
def utilTest = '/build/jacoco/testDebugUnitTest.exec'
def androidTestDir = '/build/outputs/code-coverage/connected/'
def androidTest
def androidTestFileName;
FileTree iotTree = fileTree(dir: androidTestDir, includes: ['*.ec'])
iotTree.each {
File file ->
androidTestFileName = file.name
androidTest = file.path
}
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files(utilTest, androidTest)
reports {
xml.enabled = true
html.enabled = true
}
doLast {
copy {
from utilTest
into '../report'
rename("testDebugUnitTest.exec", 'testUnitTest.exec')
}
copy {
from androidTest
into '../report'
rename(androidTestFileName, 'androidTestUnitTest.ec')
}
copy {
from "/build/test-results"
into '../report/test-results/unittest'
}
}
}
connectedDebugAndroidTest是关于模拟机的测试,这样在执行build gradle 之前需要改造。
但是 android 项目规则不能使用java 规则,要安装 android Lint 插件,然后指定规则:
sonar.projectKey=SDKDemo sonar.projectName=SDKDemosonar.projectVersion=1.0 sonar.sources=app/src/main/java sonar.binaries=app/build/intermediates/classes/ sonar.language=java sonar.sourceEncoding=UTF-8 sonar.profile=Android Lint
其中,sonar.projectKey和sonar.projectName随便填不与其他项目重复的就好,一般是项目名;sonar.sources指向Java代码目录;sonar.binaries指向build后产生classes目录,一般AS和eclipse不同;sonar.profile为代码检查规则,就是我们需要安装的 android lint.
android lint的下载:
在 sonarqube官网,选择sonar plugin ,选择java :
First Analysis of a Java Project
Install SonarQube Server (see Setup and Upgrade for more details)Install SonarJava (see Installing a Plugin for more details). By default SonarJava is provided out of the box with SonarQube.
Execute analysis:For Maven projects, use the SonarQube Scanner for Maven by executing the following command from the root directory of the project:
org.sonarqubeplugin in your build.gradle file:
Java bytecode is requiredAnalyzing a Java project without providing the Java bytecode produced by
javac(Android users: Jack doesn't provide the required
.classfiles) and all project dependencies (jar files) is possible, but will result in an increased number of false negatives, i.e. legitimate issues will be missed by the analyzer.From SonarJava version 4.12 binary files are required for java projects with more than one java file. If not provided properly, analysis will fail with the messagePlease provide compiled classes of your project with sonar.java.binaries propertySee Java Plugin and Bytecode for how to provide the Java bytecode if you are not using Maven to run your analysis.
Advanced Usage
With SonarJava, you can :deal with Unit Tests and Code Coverage : Code Coverage by Unit Tests for Java Project tutorialprovide the Java Bytecode for more accurate analysis
handle correctly the java version used by source code within projects: Handling Java Source Version
analyse other java-related files: Analyse maven pom.xml files
use additional java-specific plugins like Cobertura or Android Lint
create your own Custom Rules
点击android Lint 跳转到git,目前android Lint 有0.1,1.0,1.1三个版本。
相关文章推荐
- Android持续集成--JENKINS+GRADLE+JACOCO+SONARQUBE(一)
- Android持续集成--JENKINS+GRADLE+JACOCO+SONARQUBE(三)
- Android持续集成--JENKINS+GRADLE+JACOCO+SONARQUBE(四)
- Android持续集成--JENKINS+GRADLE+JACOCO+SONARQUBE(二)
- Android项目的持续集成:Gradle+SonarQube+Jenkins
- Jenkins+Gradle+Git+Centos 实现android持续集成、打包(超详细)
- android gradle + junit +jacoco +sonarscaner(sonarrunner) 获取单元测试覆盖率以及代码质量
- Jenkins+maven+git+sonar 系统持续集成&代码单测管理
- 基于Jenkins+git+gradle的android持续集成
- Android Jenkins+Git+Gradle持续集成-实在太详细
- 基于Jenkins+git+gradle的android持续集成
- 基于Jenkins+git+gradle的android持续集成
- 学习jenkins+git+gradle实现Android持续集成心得总结
- 在jenkins和sonar中集成jacoco(三)--使用jacoco收集集成测试的覆盖率
- Android项目集成Jenkins(JUnit test & Coverage)
- 在jenkins和sonar中集成jacoco(四)--在sonar中集成jacoco
- 在jenkins和sonar中集成jacoco(二)--在jenkins中生成jacoco覆盖率报告
- Jenkins+svn_Gradle实现android开发持续集成问题笔记
- Jenkins+maven+git+sonar 系统持续集成&代码单測管理
- Jenkins+Gradle实现android开发持续集成问题汇总