We currently deliver an Android AAR sdk that we ask users to import and add as a compile time dependency.
We recently set up SonaQube and Jacoco; so the AAR itself uses the Jacoco Plugin for test code coverage.
We have a couple of demo apps that consume the AAR to ensure compatibility.
The Issue
The issue we are now facing is this bug Here which is namely:
java.lang.ClassNotFoundException: Didn't find class "org.jacoco.agent.rt.internal_14f7ee5.Offline"
and
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/jacoco/agent/rt/internal_14f7ee5/Offline;
The issue occurs at runtime when the consuming App of the AAR starts up.
Workaround
I have set up the workaround provided in the above bug, namely forcing the APP build scripts for all APP projects to use the same jacoco version and to also include the jacoco group (agent, ant, core, and report) as compile time dependencies.
// App build.gradle
dependencies {
compile(project(path: ':TheAARInQuestion')) {
transitive false
}
....
compile "org.jacoco:org.jacoco.agent:$jacoco_version"
compile "org.jacoco:org.jacoco.ant:$jacoco_version"
compile "org.jacoco:org.jacoco.core:$jacoco_version"
compile "org.jacoco:org.jacoco.report:$jacoco_version"
}
// Top level build.gradle buildscript
buildscript {
def jacoco_version = '0.7.6.201602180812'
dependencies {
....
classpath "org.jacoco:org.jacoco.agent:$jacoco_version"
classpath "org.jacoco:org.jacoco.ant:$jacoco_version"
classpath "org.jacoco:org.jacoco.core:$jacoco_version"
classpath "org.jacoco:org.jacoco.report:$jacoco_version"
}
allprojects {
configurations.all {
resolutionStrategy {
force "org.jacoco:org.jacoco.agent:$jacoco_version"
force "org.jacoco:org.jacoco.ant:$jacoco_version"
force "org.jacoco:org.jacoco.core:$jacoco_version"
force "org.jacoco:org.jacoco.report:$jacoco_version"
}
}
}
}
My Question
I only want to apply jacoco and use the jacoco libraries in the test phase, IE testCompile only for the AAR build cycle so that consumers of the AAR do not need to do the above setup as it might conflict with their APK build process.
How can I only require the dependencies needed for testing only during test and not need them on the class path for the actual runtime implementation of the AAR?
IE I want to deliver an AAR that does not need customers to have jars on the class path nor do extra compile time dependencies. Is this possible?
EDIT 1
After reading up on jacoco further it appears that jacoco relies on JavaAgents which in turn are not supported fully by Dalvik VM(Android). Jacoco does have an offline mode but the gradle plugin does not support it out of the box. There is a feature request for it though here.
With this knowledge I have conditionally applied the plugin and tasks based on a property for when I want to run analysis in my build.gradle.