Android Code coverage: Android studio+ Gradle+jacoco

Source: Internet
Author: User
<span id="Label3"></p> <ol> <ol> <li>The OnCreate () method in the project adds the following code to create an EC File.</li> </ol> </ol><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">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 (); } }</pre></pre><p><p>2. In the Ondestory () method of the project, add the following code to write the Data.</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">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 (); } }</pre></pre><p><p>3.build adds the following content in order to generate Reports. the EC file is placed under The./build/outputs directory.</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">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 }}</pre></pre><p><p>  </p></p><p><p>4. Androidmanifest.xml inside need to confirm the permission to write SD</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"><uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE"/></pre></pre><p><p>  </p></p><p><p>5. After the APK is generated, perform the manual test, export it using adb pull/sdcard/coverage.ec d:\test after testing, and then place the EC file in The./build/outputs directory, execute the command: gradlew jacocotestreport, and then you can read the Report.</p></p><p><p>Android Code coverage: Android studio+ Gradle+jacoco</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.