日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

android gradle + junit + jacoco 集成jenkins,sonar系统

發(fā)布時間:2023/12/19 windows 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android gradle + junit + jacoco 集成jenkins,sonar系统 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先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.



這樣就是直接執(zhí)行用戶的 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是關(guān)于模擬機的測試,這樣在執(zhí)行build gradle 之前需要改造。

但是 android 項目規(guī)則不能使用java 規(guī)則,要安裝 android Lint 插件,然后指定規(guī)則:

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后產(chǎn)生classes目錄,一般AS和eclipse不同;sonar.profile為代碼檢查規(guī)則,就是我們需要安裝的 android lint.

android lint的下載:

在 sonarqube官網(wǎng),選擇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:

    mvn sonar:sonar -Dsonar.host.url=[your SonarQube URL]
  • 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]
  • 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 跳轉(zhuǎn)到git,目前android Lint 有0.1,1.0,1.1三個版本。




    總結(jié)

    以上是生活随笔為你收集整理的android gradle + junit + jacoco 集成jenkins,sonar系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。