首先gradle 文件配置如下:

apply plugin: 'jacoco'android {compileSdkVersion 25buildToolsVersion "25.0.3"defaultConfig {applicationId "com.javaandroidunittestdemo"minSdkVersion 23targetSdkVersion 25versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles 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 = truejacoco {reportsDir = file("../report/test-results/")}def utilTest = '/build/jacoco/testDebugUnitTest.exec'executionData = fileTree(utilTest)reports {xml{enabled true//Following value is a filedestination "${buildDir}/reports/jacoco/xml/jacoco.xml"}csv.enabled falsehtml{enabled true//Following value is a folderdestination "${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 androidTestdef androidTestFileName;FileTree iotTree = fileTree(dir: androidTestDir, includes: ['*.ec'])iotTree.each {File file ->androidTestFileName = file.nameandroidTest = file.path}additionalSourceDirs = files(coverageSourceDirs)sourceDirectories = files(coverageSourceDirs)executionData = files(utilTest, androidTest)reports {xml.enabled = truehtml.enabled = true}doLast {copy {from utilTestinto '../report'rename("testDebugUnitTest.exec", 'testUnitTest.exec')}copy {from androidTestinto '../report'rename(androidTestFileName, 'androidTestUnitTest.ec')}copy {from "/build/test-results"into '../report/test-results/unittest'}}}

connectedDebugAndroidTest是关于模拟机的测试,这样在执行build gradle 之前需要改造。

但是 android 项目规则不能使用java 规则,要安装 android Lint 插件,然后指定规则:

  sonar.projectKey=SDKDemosonar.projectName=SDKDemosonar.projectVersion=1.0sonar.sources=app/src/main/javasonar.binaries=app/build/intermediates/classes/  sonar.language=javasonar.sourceEncoding=UTF-8sonar.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

  1. Install SonarQube Server (see Setup and Upgrade for more details)
  2. Install SonarJava (see Installing a Plugin for more details). By default SonarJava is provided out of the box with SonarQube.
  3. Execute analysis:
    1. For Maven projects, use the SonarQube Scanner for Maven by executing the following command from the root directory of the project:

      mvn sonar:sonar -Dsonar.host.url=[your SonarQube URL]
    2. For Gradle projects, declare the org.sonarqube plugin in your build.gradle file:

      plugins {
          id "org.sonarqube" version "2.5"
      }

      Then use the SonarQube Scanner for Gradle by executing the following command from the root directory of the project:

      ./gradlew sonarqube -Dsonar.host.url=[your SonarQube URL]
  4. Follow the link provided at the end of the analysis to browse your project's quality in SonarQube UI.

Java bytecode is required

Analyzing a Java project without providing the Java bytecode produced by javac (Android users: Jack doesn't provide the required .class files) 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 message

Please provide compiled classes of your project with sonar.java.binaries property

See 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 tutorial
  • 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三个版本。

android gradle + junit + jacoco 集成jenkins,sonar系统相关推荐

  1. android gradle + junit +jacoco +sonarscaner(sonarrunner) 获取单元测试覆盖率以及代码质量

    android gradle  job config xml 如下: This XML file does not appear to have any style information assoc ...

  2. GitLab + Jenkins + Sonar Qube 持续集成

    GitLab + Jenkins + Sonar Qube 持续集成 一. 持续集成 二. 持续交付 三. 持续部署 四. 持续集成的实施流程 五. gitlab 5.1 gitlab安装配置 5.1 ...

  3. android 支付宝 40247,iOS——集成支付宝 系统繁忙,请稍后再试ALI40247

    问题描述:在调用支付宝时,老是显示,系统繁忙,请稍后再试ALI40247. 解决方案: 一.如何签约APP支付接口 第一步:进入管理中心:https://openhome.alipay.com/pla ...

  4. 持续集成Jenkins大法好

    持续集成Jenkins从初级到高级 1. 虚拟机安装 2. jenkins 安装 2.1 主要配置 2.2 安装Java环境 2.3 启动jenkins 2.4 开放8777端口供外网访问 2.5 外 ...

  5. Android --- Gradle是什么?

    威哥学习Java写了大半年的Android,今天打开项目,一直疑惑着项目里面这么多后缀.gradle,到底是干嘛的呢?汗颜啊!!! 所有网上总结了下,和初学Android的同学一起分享下. 首先我们的 ...

  6. android多版本打包,Android的持续化集成及多版本打包

    文档概述 关于Android开发,除了技术方面需要掌握,还有发布流程需要了解.本文档就包括以上两个方面,主要介绍: 使用配置文件配置不同功能的apk 使用gradle为Android构建签名包 Jen ...

  7. Android Gradle是什么?

    威哥学习Java写了大半年的Android,今天打开项目,一直疑惑着项目里面这么多后缀.gradle,到底是干嘛的呢?汗颜啊!!! 所有网上总结了下,和初学Android的同学一起分享下. 首先我们的 ...

  8. Mac 平台 Android FFmpeg 编译与集成实践

    文章目录 FFmpeg 是什么 为什么要学 FFmpeg 开发 FFmpeg 编译 1. 编译环境准备 2. FFmpeg 环境配置 3. 完整的脚本 4. FQA 编译小结 在Android Stu ...

  9. Android Gradle 插件版本说明

    Android Studio 构建系统以 Gradle 为基础,并且 Android Gradle 插件添加了几项专用于构建 Android 应用的功能.虽然 Android 插件通常会与 Andro ...

最新文章

  1. Java getBytes字符集问题
  2. 算法笔记-桶排序代码与原理、非比较排序、计数排序、基数排序、C#代码
  3. Linux IPv6 地址配置
  4. 5.spring boot使用FastJson解析JSON数据
  5. 分布式集群的Session问题
  6. 安装eclipse中html/jsp/xml editor插件以及改动html页面的字体
  7. Spring框架中的Quartz定时任务使用笔记(通过@Scheduled注解的方式实现)
  8. 【人体姿态估计1】Convolutional Pose Machines_2016
  9. 【项目管理】人力资源计划和组建项目团队
  10. 深入谈一谈iOS模块独立运行
  11. 成功领导者的20个好习惯
  12. 己椒苈黄汤治水肿案(联想的风)
  13. MES助力中国制药行业 (GMP)
  14. C++17 Any类
  15. C# doevents
  16. 做完一个网站重构项目的总结以及感想!
  17. 网络游戏植入营销的成功案例
  18. win10关闭windows聚焦_Win10聚焦锁屏壁纸无法自动更换的处理方法
  19. uniapp ios端云打包失败,求助
  20. 学大伟业DAY2模拟赛

热门文章

  1. 改变libreOffice的Calc的背景颜色
  2. hexo的yelee主题中的标签字体大小的修改
  3. ubuntu18.10无法ping百度
  4. pytorch 构建神经网络模型总结
  5. Qt OpenGL 问题总结
  6. springboot 项目依赖
  7. Java structured lock vs unstructured lock
  8. win7/WIN8.1(x64) 下使用MSDE WIN10不行
  9. Atitit.js图表控件总结
  10. LUA实现单词替换功能