Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment to resolve Gradle issue #14132 with code cov. #180

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Join Slack group](https://img.shields.io/badge/Chat-Slack-blue?link=https://chat.bitrise.io/)](https://chat.bitrise.io/)
[![trace-gradle-plugin](https://img.shields.io/maven-central/v/io.bitrise.trace.plugin/trace-gradle-plugin?label=trace-gradle-plugin)](https://search.maven.org/artifact/io.bitrise.trace.plugin/trace-gradle-plugin/)
[![trace-sdk](https://img.shields.io/maven-central/v/io.bitrise.trace/trace-sdk?label=trace-sdk)](https://search.maven.org/artifact/io.bitrise.trace/trace-sdk/)
[![codecov](https://codecov.io/gh/bitrise-io/trace-android-sdk/branch/main/graph/badge.svg?token=9zDLykViPd)](https://codecov.io/gh/bitrise-io/trace-android-sdk)

Catch bugs before they reach production — get detailed crash reports and monitor how your app is
performing across the entire install base. When issues are detected we show you exactly what
Expand Down
12 changes: 6 additions & 6 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,10 @@ workflows:
- module: trace-sdk
- arguments: "--stacktrace"
title: android_build
- android-unit-test:
- gradle-runner@1:
inputs:
- project_location: "$PROJECT_LOCATION"
- module: trace-sdk
- arguments: "--stacktrace"
- variant: "$VARIANT"
title: android_unit_test
- gradle_task: :trace-sdk:testDebugUnitTestCoverage
- gradlew_path: "$BITRISE_SOURCE_DIR/gradlew"
- cache-push@2:
title: cache_push
- script:
Expand Down Expand Up @@ -1042,6 +1039,9 @@ workflows:

$BITRISE_SOURCE_DIR/gradlew ":trace-test-application:connectedAndroidTest" --stacktrace
title: trace-test-application connectedAndroidTest
- codecov@2:
inputs:
- CODECOV_TOKEN: "$CODECOV_TOKEN"
- cache-push@2:
title: cache_push
- script@1:
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.2'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'org.jacoco:org.jacoco.core:0.8.1'
classpath files('./buildSrc/build/classes/java/main')
}
}
Expand Down
102 changes: 97 additions & 5 deletions trace-sdk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
apply plugin: 'jacoco'
apply plugin: 'com.android.library'

android {
Expand All @@ -8,6 +9,7 @@ android {
targetSdkVersion 30
versionCode = "$project.versionCode" as int
versionName "$project.version"
buildConfigField('String', 'VERSION_NAME', "\"${project.version}\"")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
Expand All @@ -16,6 +18,10 @@ android {
}
}
buildTypes {
debug {
minifyEnabled false
testCoverageEnabled true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
Expand All @@ -27,6 +33,92 @@ android {
}
}

jacoco {
toolVersion = "0.8.5"
}

tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
// see related issue https://github.com/gradle/gradle/issues/5184#issuecomment-457865951
}

project.afterEvaluate {
// A Set that will contain all the report task names for each variant
Set<String> reportTaskNames = new HashSet<>()
android.'libraryVariants'
.all { variant ->
def variantName = variant.name
def unitTestTask = "test${variantName.capitalize()}UnitTest"
def androidTestCoverageTask = "create${variantName.capitalize()}CoverageReport"

// Add the report task name for the given variant to the Set above
reportTaskNames.add("create${variantName.capitalize()}AndroidTestCoverageReport")

tasks.create(name: "${unitTestTask}Coverage", type: JacocoReport, dependsOn: [
"$unitTestTask",
"$androidTestCoverageTask"
]) {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build"

reports {
html.enabled = true
xml.enabled = true
csv.enabled = true
}

def excludes = [
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
]

def javaClasses = fileTree(dir: variant.javaCompileProvider.get().destinationDir,
excludes: excludes)

classDirectories.setFrom(files([
javaClasses
]))

def variantSourceSets = variant.sourceSets.java.srcDirs.collect { it.path }.flatten()
sourceDirectories.setFrom(project.files(variantSourceSets))

def androidTestsData = fileTree(dir: "${buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/", includes: ["**/*.ec"])

executionData(files([
"$project.buildDir/jacoco/${unitTestTask}.exec",
androidTestsData
]))
}
}
// Get the task of running the connected tests
Task connectedAndroidTestTask = tasks.findByName("connectedAndroidTest")
// Create a set that will contain the required dependsOn elements
Set<?> deps = new HashSet<>();
connectedAndroidTestTask.dependsOn.each {
// Filter out Jacoco report tasks. this is an issue, connected tests should not depend on them
String providerName = ((TaskProvider) it).getName()
if (!reportTaskNames.grep{it == providerName}) {
deps.add(it)
}
}
// Replace the original dependsOn with the filtered
connectedAndroidTestTask.setDependsOn(deps)

// Finally make sure that each report task depends on the connected tests.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like it's failing the build as it's trying to run the connected tests as well i'm not sure this is accurate - we run the unit tests in the build-trace_sdk workflow and this now fails.

reportTaskNames.forEach({
// Will be null, if testCoverageEnabled is false for the given variant
def reportTask = tasks.findByName(it)
if (reportTask != null) {
reportTask.dependsOn(connectedAndroidTestTask)
}
})
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.concurrent:concurrent-futures:${versions.concurrentFutures}"
Expand All @@ -40,16 +132,16 @@ dependencies {
implementation "com.squareup.retrofit2:retrofit:${versions.retrofit}"
implementation "com.squareup.okhttp3:logging-interceptor:${versions.okhttp}"
implementation "com.squareup.retrofit2:converter-gson:${versions.converterGson}"
implementation("com.squareup.okhttp3:okhttp"){
implementation("com.squareup.okhttp3:okhttp") {
version { require versions.okhttp }
because constraintReasons.okhttp
}
implementation "io.bitrise.trace.internal:opencensus:${versions.opencensus}"
implementation("com.google.guava:listenablefuture:${versions.listenableFuture}"){
implementation("com.google.guava:listenablefuture:${versions.listenableFuture}") {
because constraintReasons.guava
}
implementation "io.azam.ulidj:ulidj:${versions.ulidj}"
implementation("com.scottyab:rootbeer-lib:${versions.rootbeer}"){
implementation("com.scottyab:rootbeer-lib:${versions.rootbeer}") {
because constraintReasons.rootBeer
}

Expand All @@ -62,10 +154,10 @@ dependencies {
androidTestImplementation("androidx.test:runner:${versions.androidxTest}") {
exclude group: "org.hamcrest", module: "hamcrest-core"
}
androidTestImplementation ("androidx.test:rules:${versions.androidxTest}") {
androidTestImplementation("androidx.test:rules:${versions.androidxTest}") {
exclude group: "org.hamcrest", module: "hamcrest-core"
}
androidTestImplementation ("androidx.test.ext:junit:${versions.extJunit}") {
androidTestImplementation("androidx.test.ext:junit:${versions.extJunit}") {
exclude group: "org.hamcrest", module: "hamcrest-core"
}
androidTestImplementation "org.hamcrest:hamcrest:${versions.hamcrest}"
Expand Down