How to get code coverage in Android using Maven (android-maven-plugin) - android

I have an Android Maven project (let's call it parent_project) that contains various submodules: my_library_project, app_using_my_library_project, test_project and extra_lib.
So, the structure would be like this:
parent_project
* my_library_project (Android Library Project)
* app_using_my_library_project (Demo app that uses the Android Library Project)
* test_project (Project containing the tests instrumented against app_using_my_library_project)
* extra_lib
What I would like is to generate test coverage for my Android project using Maven (and not Ant, I am already able to generate code coverage reports using Ant, following these instructions: https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project).
I have no strong preference for the code coverage tool used but I would prefer EMMA, since seems the most common in the Android development world.
I am using android-maven-plugin (http://code.google.com/p/maven-android-plugin/) in its 3.0.0-alpha-12 version and I have already tried to put in the configuration of my parent's pom.xml the next:
<test>
<coverage>true</coverage>
<createreport>true</createreport>
</test>
But that does not produce the desired code coverage report.
So:
Is there any difference between the pom configuration for getting code coverage for a standard Java project and an Android project?
Do you know any example Android project using Maven that has code coverage?
Any hints on how to do this?

If you're going to stick with maven, and want a plugin for maven that will do the code-coverage job, I think Cobertura is a better choice, as Emma stable last build is from 2005.
Although in "Android Application Testing Guide" (a recent book from June this year) they talk about Emma and demonstrate how to use it for testing, I think people stick to it, because it's needed to build Android from source (and if Google use it for their own OS development, it should be the best, right?).
If you're not fanatically bound to Maven, I strongly recommend to try Robotium.
Robotium has full support for Activities, Dialogs, Toasts, Menus, and Context Menus.
It also supports code coverage (Ant based though, for now) and some people recognize it as one of the leading testing platforms for Android.
Edit:
According to the Cobertura site, it supports code coverage in Maven 1 and Maven 2 environments. Although, you can find examples with Maven 3 also.
A problem exists between pom configurations of Maven 2 and Maven 3. It seems for the reporting to work you have to basically move your old reporting plugins into the configuration section of the new maven-site-plugin. (See the article for details).
Another option is to try and use Sonar with Maven. Sonar has cobertura embedded (also options to embedd EMMA) and some people state that they had successfully reported code coverage, despite they had problems using the "stand-alone" cobertura plugin.

I could generate code coverage reported using emma maven plugin and display reports in sonar for an android application project. Just follow the configuration in https://code.google.com/p/maven-android-plugin/wiki/EmmaMaven.
But for library project I get a 0% coverage. It doesn't generate the metadata files. However as soon I change the library project to package as an apk, it works like a charm. It runs the tests in a emulator(configured in jenkins) and shows the coverage reports.
If you are looking out specifically for library project, this wont help. I will post if I find some solution which deals with library projects.

Related

Is it possible to analyze Android Kotlin code-coverage with SonarQube?

I have configured a Jenkins item that, after gradle building and testing, calls the "Execute SonarQube Scanner" with the following specifications:
sonar.projectKey=my_project_key
sonar.projectName=my_Android
sonar.projectVersion=1.0
sonar.sources=app/src
sonar.java.binaries=app/build/intermediates/classes/debug
sonar.coverage.exclusions=app/src/test/**,app/src/androidTest/**,app/src/main/res/**
This successfully runs SonarQube and generates a report to IPaddress:9000 that measures: Bugs, Vulnerabilities, Debt, Code Smells, and Duplications just like it is supposed to. However there is no place in this report that defines the code covered by tests. SonarQube is running the Kotlin plugin successfully and sees the codebase itself, but is not determining coverage. Is this possible to gather this information with SonarQube when using Kotlin Android?
I did try installing Jacoco, but did not see any new data on the SonarQube results.
You can install a 3rd-party Sonar plugin here that will measure code quality for Kotlin projects. Unfortunately, as of this posting, SonarQube does not have its own plugin for this.
Once that is set up, take a look here for setting up gradle and sonar-project.properties. The post is a little dated, but contains a link to an updated version and is still a good starting point.
TL;DR for #2: The information SonarQube needs for Kotlin is located in 2 places. One is already identified by the OP. The other is located in app/build/tmp/kotlin-classes.
If you are using gradle and Jacoco.
You can use Jacoco for the code coverage with the xmlReportPaths. With that Sonar will trust what Jacoco output.
sonarqube {
properties {
property("sonar.java.coveragePlugin", "jacoco")
property("sonar.coverage.jacoco.xmlReportPaths", "./build/reports/jacoco/test/jacocoTestReport.xml")
}
}
Documentation from Sonar + Jacoco Plugin.
It also works with mixed source code (Java, Kotlin).
I abandoned attempting to get Kotlin code coverage with Sonar shortly after posting the question.
However, I have gotten many projects to work with Java and JaCoCo.
Retrospectively looking back at my sonar.properties file I may have been able to fix this by referencing the output of the JaCoCo task by adding the following line to the sonar.properties file:
sonar.jacoco.reportPath=build/jacoco/test.exec
I am not sure that this will work with Kotlin, but it helped with Java coverage and solved my use case.
Newer versions of Sonarqube (6+) now support Kotlin natively, and do not require any custom configuration in your project.
Just ensure the Kotlin and the Jacoco plugins are installed, and it should just work for you.
--- OLD ANSWER---------------------------------
My project is using Jacoco for generating coverage reports.
To have SonarQube pick up the results, try setting the following:
sonar.java.binaries={$buildDir}/classes/kotlin
In other words let SonarQube know where the Kotlin class files are.
This configuration assumes a 100% Kotlin project. If your project is a mix of Java and Kotlin, then you'll have to determine how to configure both directories.

How to calculate test coverage while using Robolectric

I am using make for building and Robolectric as a framework for running Android tests. I would like to calculate coverage of my app. For instrumentation tests I used to use emmalib. What is the best way for me to set up coverage calculation in this case? I can't migrate to gradle or maven.
Are you bound to Emma? How about using RoboElectric + Cobertura code coverage? (I think you could just use a CLI for the above combination)
So options
1.) RoboElectric + Cobertura - CLI alone probably for someone not on ANT
2.) JaCoCo might have some useful options
3.) Pure Android Testing + Emma/EclEmma
Useful Link trails to follow
Generating android code coverage though changes in build.xml and ant.properties
Android Gradle Code Coverage
https://intellectualcramps.wordpress.com/2013/08/18/code-coverage-of-robolectric-tests-using-jacoco/
https://bitbucket.org/ravidsrk/androidstarter
EDIT:
Well most of the tutorials I have come across use ant unfortunately, and I don't think it would be a bad idea for you to move to a recommended build system like Gradle so it opens up a lot options. But for JaCoCo you could take a look at here: https://intellectualcramps.wordpress.com/2013/08/18/code-coverage-of-robolectric-tests-using-jacoco/
UPDATE:
Moved this from comments to the answer section for information to anyone wanting to see this and because this is a bounty question
One solution would be to use Cobertura to generate code coverage, which can be integrated in eclipse and also run by an ant build script.
A template project of such an integration can be found here: https://github.com/adgllorente/android-cobertura-boilerplate
Note that all of the magic happens in the build.xml of the Test project. Theses tasks should probably be generalised to a custom_rules.xml file so you can still use android to update your projects.
Finally, for Gradle you have many different options:
http://raptordigital.blogspot.nl/2014/08/code-coverage-reports-using-robolectric.html
http://chrisjenx.com/gradle-robolectric-jacoco-dagger/
https://stackoverflow.com/a/25037742/2771851
Note that you can always use Gradle as a secondary build system just to generate the coverage reports. (but a second build system will introduce a lot of overhead)
jacoco sometimes does not work with Robelectric and powermock runner you can use clover an atlassian tools it is now open source tool.

Setting Up An Android/Maven Library Project in Intellij

As you can see from my passed 3 questions I am having major problems with my project setup. I am getting lots of very specific errors that seems like nobody can answer them. I am getting hugely frustrated. So I am going to try a fresh approach. I will describe how I would like my project set up and if somebody could give me some brief steps to follow I would be very grateful. After messing around with this for around a week I believe I have some knowledge but maven seems to be incredibly difficult to understand.
I am writing a library which will be used in 2 projects. The major libraries I would like to use are roboguice, robolectric and jackson(json library). I would like to have a way to test the library but I am unsure if this should be in a different module or not. Robolectric seems to suggest it should be in the same module. I am unsure how I should use this library in the other 2 projects. I have been looking at this http://code.google.com/p/maven-android-plugin/wiki/ApkLib but unfortunately the website tells me next to nothing about how I should create the apklib.
If it is possible I would also like the other 2 projects to have a dependency on that library and build it automatically.
I have been using this site to create the maven project http://stand.spree.de/wiki_details_maven_archetypes
But I have been running into issue after issue. If anybody can point me in the right direction I will be very appreicative
Even if maven is (almost) a foreign country to me, I had some moderate success setting up a maven project and interacting with it using intellij. I wanted to use it because it looked the easiest way to have robolectric working with intellij.
I wrote a blogpost you can find here
What you need is:
Maven android sdk deployer https://github.com/mosabua/maven-android-sdk-deployer
Maven android plugin http://code.google.com/p/maven-android-plugin/wiki/GettingStarted (using android archetypes is just fine)
If you want to build an apk lib instead of an apk, just specify apklib in the packaging tag
PS: I also saw this a while ago, but never gave it a try.

Maven build Android application - BuildConfig

While trying to use Maven for building Android projects I recognized that the maven-android-plugin doesn't autogenerate the BuildConfig class during build time. I have found a ticket regarding that problem and a Google Groups thread.
Issue: http://code.google.com/p/maven-android-plugin/issues/detail?id=275
Google Groups: http://groups.google.com/group/maven-android-developers/browse_thread/thread/cc74267bbb7baebb/4e63b710513bb535?lnk=gst&q=BuildConfig#4e63b710513bb535
The quick hack in the issues thread suggests creating another micro maven project to set the BuildConfig.DEBUG variable during build time with a profile switch. This would force me to build a multi module project for every single little application.
The suggestion in the Google Groups thread sounds ok for me (executing the Ant Task from the SDK to generate the BuildConfig class) but after digging through the build.xml and trying to get this work I gave up.
Has someone got a practical working solution for the BuildConfig problem with maven+android?
Thanks to Jake Wharton and Manfred Moser, BuildConfig generation has been supported since Android Maven Plugin 3.3.2.
Check out changelog for 3.3.2 release here.

Duplicate file when deploying apk

This problem seems to come up for a few people, but I haven't been able to apply the solution suggested in other threads for one reason or another so...
I am trying to build a simple android app with an embedded webserver. The server of choice if jetty. I am using maven to manage dependencies with the maven-android-plugin. I have added jetty-server v8.x as a dependency with the default scope. When I try and deploy my apk to the sandbox I get an error
Found duplicate file for APK: about.html
So the file in question is from the jetty package, or rather it is found in two jetty packages, one being a dependency of jetty-server. Other threads out there seem to be suggest I delete the file from one jar or the other but this is not really a scalable solution as I am not the owner of the jars.
Is there a more general solution to this problem? Something that manages the conflict and build or deploy time?
I should note, I am a bit of a Java noob, and have gone the NetBeans and maven route because this is the toolkit I'm familiar with.
OK, found it. The solution is to build using the following command
mvn android:deploy -Dandroid.extractDuplicates=true
There are a couple of issues:
First, to integrate Jetty, your project is going to need a pom.xml file. The Android APK (as-is) does not know how to resolve a pom with Maven dependencies without some sort of bridge. So, you'll need to install a few plugins for Pom management onto Eclipse. Follow the instructions here: http://rgladwell.github.com/m2e-android/
(I found it easier to create a new project using the method described here rather than converting my existing Android app to a Maven project, but I'm sure it'll work either way.)
Second, once you've installed this, you're going to have to make sure your pom.xml contains the necessary dependencies, build goals, and variables. So, open up your pom.xml, and make sure you have the following:
In the "build" node:
sourceDirectory -> src
defaultGoal -> install (I suppose...)
In the "dependencies" node:
... all the jetty/cometd dependencies (org.cometd.java, org.eclipse.jetty, etc.)
Third, you should know that you can't just click the green run button and launch the app via ADB. You have to install it the maven way (Run As -> Maven build/install/test/whatever)
You should open up your war file and check if you actually have two about.html files in there. Some time ago I had two web.xmls in my war files. If you actually have duplicates then you should try to exclude those files.
Maybe you have one file in your project and the duplicate is generated by the maven plugin.
I also just realized that you're probably already properly using the android-maven plugin. One other important tip: change the scope to runtime on the jetty dependencies. This builds successfully for me, whereas using the default scope (compile) always threw the duplicate file in APK error.

Categories

Resources