安卓程式碼涵蓋範圍:android studio+ gradle+jacoco

來源:互聯網
上載者:User

標籤:repo   build   led   執行命令   extern   ldd   port   寫入   sdi   

  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.產生apk後,進行手動測試,測試完畢後使用adb pull /sdcard/coverage.ec d:\test 進行匯出,然後將ec檔案放到./build/outputs目錄下,執行命令:gradlew jacocoTestReport,然後就可以看報告了.

安卓程式碼涵蓋範圍:android studio+ gradle+jacoco

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.