zoukankan      html  css  js  c++  java
  • 安卓代码覆盖率:android studio+ gradle+jacoco

    1. 在工程的oncreate()方法添加如下代码,目的是创建ec文件.
     String DEFAULT_COVERAGE_FILE_PATH = "/mnt/sdcard/coverage.ec";
    
            File file = new File(DEFAULT_COVERAGE_FILE_PATH);
    
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    

      2.在工程的ondestory()方法添加如下代码,目的是写入数据.

    				OutputStream out = null;
                    try {
                        out = new FileOutputStream("/mnt/sdcard/coverage.ec", false);
                        Object agent = Class.forName("org.jacoco.agent.rt.RT")
                                .getMethod("getAgent")
                                .invoke(null);
    
                        out.write((byte[]) agent.getClass().getMethod("getExecutionData", boolean.class)
                                .invoke(agent, false));
                    } catch (Exception e) {
                        Log.d(TAG, e.toString(), e);
                    } finally {
                        if (out != null) {
                            try {
                                out.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
    				}
    

      

      3.build里面增加如下内容,目的是为了生成报告.ec文件放在./build/outputs目录下.

    apply plugin: 'jacoco'
    
    task jacocoTestReport (type:JacocoReport) {
    
        group = "Reporting"
    
        description = "Generate Jacoco coverage reports on the  build."
    
    
    
        classDirectories = fileTree(
    
                dir: "${project.buildDir}/intermediates/classes",
    
                excludes: ['**/R.class',
    
                           '**/R$*.class',
    
                           '**/*$ViewInjector*.*',
    
                           '**/BuildConfig.*',
    
                           '**/Manifest*.*']
    
        )
    
    
    
        def coverageSourceDirs = [
    
                "src/main/java"
    
        ]
    
        additionalSourceDirs = files(coverageSourceDirs)
    
        sourceDirectories = files(coverageSourceDirs)
    
        executionData = fileTree(dir:'./build/outputs',include:'**/*.ec')
    
    
    
        reports {
    
            xml.enabled = true
    
            html.enabled = true
    
        }
    
    }
    

      

      4. AndroidManifest.xml里面需要确认一下写sd的权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

      

      5.在build.gradle的buildTypes添加一行代码testCoverageEnabled = true

      6.生成apk后,进行手动测试,测试完毕后使用adb pull /sdcard/coverage.ec d: est 进行导出,然后将ec文件放到./build/outputs目录下,执行命令:gradlew jacocoTestReport,然后就可以看报告了.

      同时,上面这个做法可以完美解决这个报错:

    java.io.FileNotFoundException: /jacoco.exec: open failed: EROFS (Read-only file system)

  • 相关阅读:
    tomcat 启动超时
    读书笔记-String
    读书笔记-集合
    读书笔记-算法
    多变量梯度下降
    多变量线性回归
    梯度下降算法在线性回归中的运用
    梯度下降
    线性回归——代价函数
    线性回归
  • 原文地址:https://www.cnblogs.com/xiaobaichuangtianxia/p/6027432.html
Copyright © 2011-2022 走看看