Generate code coverage reports for Android Maven Library Project - android

I have a Android Library project and an Android Test project.
The test project has Android dependency and junit dependency.
The modules are:
MyProject
MySample
MyProjectTest
MySample uses MyProject as a library and gives an apk as output.
MyProjectTest has MySample as dependency and it has the test cases.
The unit test cannot run on jvm. That is why MySample project is used which generates an apk and the unit tests can run on the emulator. They run as instrumented tests as they need an emulator to execute.
There are lots of documentation available for an ant build but very few for maven android project.
I have configured emma for code coverage, but the coverage files are not generated.
My poms follow this link.https://code.google.com/p/maven-android-plugin/wiki/EmmaMaven with the following modification.
The tested.project.dir is set to MySample
/data/data/(package of MySample)/files/coverage.ec in property pullSource
I get the error: "Cannot pull coverage.ec from device as it doesnt exist."
Please help. Any pointer to the same is much appreciated.
Also I am open to any coverage tools. My only constraint is I need to test Maven Android Library Project.
I tried with cobertura too but still couldnt generate any coverage reports.

Related

NoClassDefFoundError when running unit test with Gradle task

I have a project using dynamic feature module, and I want to run my unit test in feature module via gradle task (for my CI purpose):
./gradlew :feature_product:test
But it always gives me NoClassDefFoundError for tests that have dependencies on classes from the base module:
com.example.android.feature.product.ProductViewTest > on vote change to negative FAILED
java.lang.NoClassDefFoundError: app.BaseView
ProductView class from the feature module extends BaseView from the base module.
Oddly, it succeeds when run in Android Studio, it works fine.
Then I notice something different in the logs, when I run via command line and when I run Android Studio. The first line in the Android Studio is generateDebugSources, something which absent when I run ./gradlew test
Executing tasks: [:lib_ui:generateDebugSources, ...]
How do I fix this? Does Android Studio has different command with the provided command ./gradlew test when I press Ctrl+Shift+R ?
After searching further about this issue, I found it also being reported in the android-test and app-bundle-samples projects and there is also an issue in the issue tracker.
It turns out this issue fixed in the Android Gradle Plugin 4.1.0 as per comment in the issue tracker.
If you don't want to update AGP to 4.1.0 which is still in alpha, adding this to the feature module's build.gradle fixed the issue for me, as per this comment:
testRuntimeOnly(files("$projectDir/../b_app/build/intermediates/app_classes/debug/classes.jar"))
If it is a missing task that you believe is necessary then calling it first like below should do the trick:
./gradlew :lib_ui:generateDebugSources :feature_product:test
I would even go full on and assemble the dependencies if necessary though that might take more time:
./gradlew :lib_ui:assemble :feature_product:assemble :feature_product:test

Android gradle plugin v3.3.0 -> can't find R$raw when using proguard

Just updated to Android gradle plugin version 3.3.0
We have the following setup (not sure which are important):
Application project (app) with 3 library modules (data, domain, utils)
Databinding enabled (databinding.enabled true)
Proguard enabled(proguardFiles 'proguard-rules.pro')
When I build the app using:
./gradlew assembleDevRelease
I get the following error:
can't find referenced class my.package.data.R$raw
When I build the app using:
./gradlew :app:assembleDevRelease
The app builds fine, generates an obfuscated *.apk which I can install
Question:
What's the difference between assembleRelease and :app:assembleRelease
- Why does switching to android gradle plugin 3.3.0 affect which task I have to call to build my apk? We use assembleRelease everywhere in our CI pipelines to build our apks.
What changed in android gradle plugin 3.3.0 that caused the task assembleRelease to break? We use assembleRelease everywhere in our CI pipelines to build our apks.
Any suggestions how we can make 'assembleRelease' working again? (update Proguard config?, enabling R8?)
What's the difference between assembleRelease and :app:assembleRelease
The former runs the assembleRelease task on all modules relative to current level. The latter runs it on the app module only (and its dependencies).
Why does switching to android gradle plugin 3.3.0 affect which task I have to call to build my apk? We use assembleRelease everywhere in our CI pipelines to build our apks.
The question does not have enough info to say for sure, but there are a number of changes listed in the release notes. For example, this issue might be related to:
Faster R class generation for library projects: Previously, the Android Gradle plugin would generate an R.java file for each of your project's dependencies and then compile those R classes alongside your app's other classes. The plugin now generates a JAR containing your app's compiled R class directly, without first building intermediate R.java classes. This optimization may significantly improve build performance for projects that include many library subprojects and dependencies, and improve the indexing speed in Android Studio.

Android instrumented test coverage report on Jenkins - Incompatible JaCoCo version

I've got a problem when I launch a build on Jenkins. I would like to have the coverage report of my instrumented tests in Jenkins but it fails due to an incompatible version of Jacoco.
In my Android project, I've added the line apply plugin: 'jacoco' in the app/build.gradle file.
My Android project use the gradle plugin 3.0.1 version.
In Jenkins, I set up my application like this:
In Jenkins, I've installed the JaCoCo plugin 3.0.1 version.
When I run a build in Jenkins with this configuration, it goes well but I don't get the coverage report of the instrumented tests.
So, I change the settings like this:
When I run a build, in the Console output, I get the following message:
ERROR: Step ‘Record JaCoCo coverage report’ aborted due to exception:
org.jacoco.core.data.IncompatibleExecDataVersionException: Cannot read execution data version 0x1006. This version of JaCoCo uses execution data version 0x1007.
On the Jenkins wiki, I read that there is a compatibility problem between different versions of Jenkins and JaCoCo: https://wiki.jenkins.io/display/JENKINS/JaCoCo+Plugin.
So, I tried to use a 7.5+ version in my Android project by adding this line in the build.gradle file:
jacoco {
toolVersion '0.7.5+'
}
But it still fails... :-(
I don't understand why it works with the *.exec files only but not when I add the *.ec files.
Someone see what I missed?

Generating Code Coverage Report with Android Studio 1.0

I'm trying to generate a Code Coverage report using Android Studio and the gradle plugin.
I've done alot of research and seem to have found that using the jacoco plugin is the way to do it. I currently only have instrumentation and junit tests which I run with the gradle wrapper using gradle 1.13 that I execute with ./gradlew connectedCheck.
The appropriate part of my gradle file looks like this:
mock.initWith(debug)
mock {
testCoverageEnabled true
}
Upon doing so it generates a coverage.ec file that I have no idea what to do with. Apparently you need a coverage.em file for emma to compile it but I'm not sure where to generate that.
I've also tried this without a gradle wrapper using gradle 2.2.1
According to http://gradle.org/docs/current/userguide/jacoco_plugin.html#jacoco-application-output it should be generating an html file with the coverage report in it but I am unable to do so. Does anyone have any great tutorials or a way to use the coverage.ec file?

Android Gradle two instrumentation test suites

I am trying to set up a second instrumentation test suite for an android project. The project builds under Gradle and the ADT plugin.
I have instrumentationTestSuiteA and instrumentationTestSuiteBand buildTypeAand buildTypeB.
I would like instrumentationTestSuiteA to run on buildTypeA, and instrumentationTestSuiteB on buildTypeB.
How could I accomplish this?

Categories

Resources