標籤:repo build led 執行命令 extern ldd port 寫入 sdi
- 在工程的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