Cover photo
YenTing Chen (Jim)
105 followers|243,531 views


YenTing Chen (Jim)

Shared publicly  - 
Android Test + Espresso + JaCoCo
If you try to run Espresso tests on device and at the same time grab the code coverage information, than your are looking on the right topic... Its all about this.

Unfortunately JaCoCo + Espresso does not work for everyone. 

Quick search will reveal a planty of reported issues and mostly no working solutions. I also ignore this problem till Michenux contact me and ask for help. (Good for us all in Sweden is a "red date" in calendar and I have some free time)

The issue and why it exists?
AndroidTest configuration produce a special APK. Inside it included source code of the original binary, libraries and instrumentation. Problem is that Android Dalvik is not 100% compatible JVM, so it does not support JVM javaagents (its the way how originally JaCoCo/Emma should be used with Java). So everything we need is inside the APK, but we cannot able to run it properly.           

That is why inside Android should be used a special way for code coverage instruments. Its known as a "JaCoCo offline mode". In two words - this is a pre-processing of *.class files and embedding into them of special Jacoco method, that executed from class constructor. So when you execute the tests, Jacoco is able to identify that and collect runtime information.


Android build system should do that all, but looks like it fails at the current moment. 

Steps that build system should do:
1) download and install dependency JaCoCo library - DONE
2) include JaCoCo Agent into APK - DONE
3) create classes with enabled jacoco (offline mode) - DONE
4) replace original files by "jacoco enabled" - NOT WORKING
5) define coverage result output path - NOT WORKING
6) run tests - DONE
7) PULL coverage file from device - NO
8) Generate HTML coverage report - NO

The Solution
all in this Gradle file:

Step #1: use the latest version of JaCoCo ("")
Step #2: Destination path can be defined by resource file - (~/src/androidTest/resources/ [for alternative look inside the org.michenux.espressotestcoverageapp.AndroidJacocoTestRunner class]

> #destfile =/sdcard/
> destfile=/data/data/{}/

Step #3: Modify build sequence. Place own customization tasks between "preDex${flavor}${buildType}AndroidTest" and "dex${flavor}${buildType}AndroidTest". I name them: "fixJacocoAgentAndroidTest${flavor}${buildType}" and "fixJacocoAndroidTest${flavor}${buildType}"

Step #3.1:
"fixJacocoAgentAndroidTest${flavor}${buildType}"  - this task include jacocoagent.jar into final binary (simply copy JAR into "${project.buildDir}/intermediates/pre-dexed/androidTest/${flavor}/${buildType}" folder). After that other tasks will use it during the compilation. 

Step #3.2:
"fixJacocoAndroidTest${flavor}${buildType}" - copy "jacoco enabled" classes on top of old classes.

Step #4:
Run the project. Inside the app folder you will find a "" with non-zero size. 


What's left:
- PULL file from device to PC (adb pull *)
- create JaCoCo HTML report (I did this in my older posts, just use the gradle from them, it will work if you pull coverage file into project build folder)

P.S. latest android gradle plugin (1.3-beta3) looks like partly resolves the issues
19 comments on original post

YenTing Chen (Jim)

Shared publicly  - 
Book : High Performance Android Apps
Really looking forward to this upcoming book by +Doug Sillars   . I've had a chance to get a sneak peak at the internals, and it's going to be a great reference for the novice and professional perf programmer alike.

6 comments on original post

YenTing Chen (Jim)

Shared publicly  - 
Have him in circles
105 people
Chiao-Liang Hsu's profile photo
Nicole Teng's profile photo
Miranda Wu's profile photo
chingting hsieh's profile photo
Matt Debouge's profile photo
Tony Yeh's profile photo
Yang Chuang's profile photo
Jamie Pitts's profile photo
jarvis Lin's profile photo

YenTing Chen (Jim)

Shared publicly  - 
Part of reveals in Google IO 2015

YenTing Chen (Jim)

Shared publicly  - 

YengTing Chen changed his profile photo.

Shared publicly  - 
YengTing Chen changed his profile photo.
林國良's profile photo
Have him in circles
105 people
Chiao-Liang Hsu's profile photo
Nicole Teng's profile photo
Miranda Wu's profile photo
chingting hsieh's profile photo
Matt Debouge's profile photo
Tony Yeh's profile photo
Yang Chuang's profile photo
Jamie Pitts's profile photo
jarvis Lin's profile photo
Apps with Google+ Sign-in
  • GDG-X
  • AlphaBear
  • Farmdale
  • Farms & Castles
  • PBA® Bowling Challenge™
  • Where's Waldo & Friends
  • Epic Skater
  • 黒猫になった魔法使いマギ
  • Angry Birds Fight!
  • Get 11
  • Implosion
  • 1010!
  • Paperama
  • Cooking Fever
  • Despicable Me:Minion Rush
  • Brain Dots
Basic Information
Contributor to
YenTing Chen (Jim)'s +1's are the things they like, agree with, or want to recommend.
ねこあつめ - แอปพลิเคชัน Android ใน Google Play

ねこあつめの2ステップ!①遊び道具(グッズ)とゴハンを庭先に置く。 ②ねこがやってくるのを待つ。ゴハンに惹かれてやってきたねこたちがグッズで遊んでいる姿を観察できます! 白猫、黒猫、茶トラにキジトラ。ねこの種類は20種類以上。 中にはこだわりのグッズにしか興味をしめさないレアネコ

Layout from Instagram - Aplicaciones de Android en Google Play

La última aplicación de Instagram te permite crear composiciones divertidas y únicas mezclando tus propias fotos y compartiéndolas con tus a

Google I/O 2015 - Apl Android di Google Play

The official Google I/O 2015 app was built to be your co-pilot to navigate the conference, whether you’re attending in-person or remotely. -

Google Developers

Inspiring developers everywhere

Google - Android Apps on Google Play

Google app for Android: The fastest, easiest way to find what you need on the web and on your device.* Quickly search the web and your phone

1010! - Google Play Android 應用程式

1010! 是一款引人入胜的益智游戏,玩法简单但有特色。1010! 简单易学且玩法有趣,是一款完美的手机益智游戏。 在这款让人入迷的益智游戏中,只需将模块拖放到屏幕中,在垂直和水平方向创建并消除整行模块,阻止模块填满整个屏幕。在 1010! 中,你可以连接到你的 Facebook

Google-kalenteri - Android-sovellukset Google Playssa

Hanki virallinen Google Kalenterin sovellus Android-puhelimelle tai -tabletille. Säästät aikaa ja saat kaiken irti jokaisesta päivästä. • Uu

Drippler - Android Tips & Apps - Android Apps on Google Play

Drippler takes your Android experience to the next level! Download Drippler to get tailored Android reviews, technology articles, tips and u

Google Now Launcher

Upgrade the launcher on your device to make Google Now one swipe away. From your primary home screen, swipe right to access Google Now cards

MiniMovie-Slideshow Maker

*The new update is published by stage rollout which will be dispatched by Google server. If you haven't seen the latest update with the name

Selfie Grid

This time, the creator of the popular photo app PicCollage brings you the most instant selfie camera in the world! Selfie Grid allows you to




打工趣讓你可以快速尋找短期打工 只要打開 APP 就能即時掌握屬於你的機會* 一目了然的打工資訊 打工趣整理出清楚詳細的打工內容,幫你省下篩選大量資訊的時間,快速找到你想要的打工機會。* 簡單追蹤,方便查看 打工趣內建方便的追蹤功能,在迅速瀏覽的同時,順手把有興趣的打工加入到清單


KAMI is deceptively simple: fold out coloured paper to fill the screen in as few moves as possible. Get your hands on the first nine levels

Pic Collage

With over 70 million downloads, PicCollage is your new favorite app ❤. It's a party with your photos! Pic Collage allows you to create amazi


Top-ranked Yelp for Android has over 50 million reviews on businesses worldwide — all in the palm of your hand. Whether you are looking for

ASUS MyOcean (Live wallpaper)

Not just a live wallpaper! ASUS MyOcean magically entwined Ocean level and battery, it provides new, innovative and informative live wallpap