You can achieve code coverage of android apk using gradle and jacoco, by following below steps.
1.Configuring JaCoCo plugin in build.gradle (present inside 'app' folder ) as below apply plugin: 'jacoco' jacoco { toolVersion = "0.7.4+" } 2. Enable Jacoco code coverage on the debug build in build.gradle (present inside 'app' folder ) at 'buildTypes' as below debug { testCoverageEnabled true } 3. Add below line in your 'app/src/main/AndroidManifest.xml ' to access sdcard for writting coverage data <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 4. Add below lines in 'MainActivity.java' at end protected void onStop() { super.onStop(); if(BuildConfig.DEBUG) { try { Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT"); Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",coverageFile.getClass(), boolean.class, boolean.class); dumpCoverageMethod.invoke(null, "/sdcard/coverage.exec", ture, false); } catch (Exception e) {} } } 5.Build debug apk. 6..Install this apk in your device , and to do the test. 7.After testing kill your app. 8.Connect device with computer, run below command to get coverage data file from device 'adb pull /sdcard/coverage.exec' 9.Genrate coverage report using ant(with jacoco task) or jenkings (jacoco plugin) by using following:- Copy classes from "D:\coverage\app\build\intermediates\classes\debug" Copy source from "D:\coverage\app\\src\main\java" Copy exec file from "D:\coverage\mycoverage.exec" Add below as Exclusions:- **/R.class,**/R$*.class,**/BuildConfig.*,**/Manifest*.*,**/*Fragment*.*,**/*Activity$*.* Thanks Abhishek Gupta On Saturday, 17 October 2015 10:11:14 UTC+5:30, vishy wrote: > Hi guys, > > I am investigating code-coverage for our Android app. built with Gradle and > Android Studio. > > I need code-coverage reports in two situations: during manual tests, and > during automated tests built with our Appium suite. > > I need to get a proof-of-concept that JaCoCo can indeed do code coverage in > this case. > > Given that we need coverage for Appium and manual tests, I believe > offline-instrumentation with JaCoCo is what I need. > > I have succeeded in creating an instrumented APK. > But when I run the app manually, I keep seeing errors in the logcat > W/System.err: java.io.FileNotFoundException: /jacoco.exec: open failed: EROFS > (Read-only file system) > > From JaCoCo docs as well as general Googling, I understand that Jacoco needs > to know where to store the jacoco.exec file - and that I need to package a > jacoco-agent.properties (containing a destfile property) with the APK. > > I have tried putting jacoco-agent.properties in the "assets" folder of my app > - it was packaged into the APK properly, but did not seem to have any effect > (i.e Jacoco failed as usual) I tried putting it inside res - it did not get > packaged into the APK. I tried res/raw and hit the error about a dash in the > filename. > > I am at my wit's end, and would really appreciate help. How do I package > jacoco-agent.properties using my Gradle build into the APK, so that JaCoCo > will read it? -- You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/66a5b5af-8987-46a3-9902-0b6e0c7e079c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.