2

Seemed like adding Jacoco to an Android project would be a straightforward process, but so far it's been nothing but pain.

Having tried a few guides to getting it working, this one seems to give the best results for me so far:

https://medium.com/nerd-for-tech/setup-jacoco-code-coverage-with-your-multimodule-android-app-kotlin-a0f82573a1

But shortly after it all goes wrong. Turns out that to use kotlin 1.5+ (which the project I'm adding to does) you need to use jacoco version 0.8.7 or higher. So I've done that.

But using versions of jacoco beyond 0.8.3 causes the build to fail with complaints about $jacocoData.

A search on the error message from that suggests setting the compile/target versions to VERSION_1_8 instead of VERSION_11. Okay, give that a go, but then the build fails with errors like:

> Duplicate class com.google.android.gms.common.api.internal.zza found in modules jetified-play-services-base-15.0.1-runtime (com.google.android.gms:play-services-base:15.0.1) and jetified-play-services-basement-17.2.1-runtime (com.google.android.gms:play-services-basement:17.2.1)

Is there a clear guide anywhere to getting code coverage data out of an Android project written in Java 11 and kotlin 1.5+ with multiple variants? Because this is something that seems like it should be easy but is actually turning out to be incredibly painful...

Adrian
  • 1,652
  • 2
  • 22
  • 32
  • please add your gradle build files so we can reproduce – Jacouille May 05 '22 at 10:22
  • I wish I could provide a link to a clear guide, but there doesn't seem to be one. You might want to try the [hiya:jacoco-android](https://plugins.gradle.org/plugin/com.hiya.jacoco-android) plugin. Although it hasn't been updated recently, it still works well with recent versions of Kotlin. – Rapunzel Van Winkle May 08 '22 at 03:13

1 Answers1

0

I already faced this situation this is working for me.
Build.gradle(project)

project level gradle

Build.gradle(Module )

            plugins{
                id 'jacoco'
                 } 
        
            jacoco {
                toolVersion = "0.8.7"
              }
    
        android{
         buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                }
        
                debug {
                    debuggable true
                    testCoverageEnabled true //jacoco see https://stackoverflow.com/a/39262886/6478047
                   
                }
            }
        
          compileOptions {
                sourceCompatibility JavaVersion.VERSION_1_8
                targetCompatibility JavaVersion.VERSION_1_8
            }
        
            kotlinOptions {
                jvmTarget = '1.8'
            }
        
          testOptions {
                animationsDisabled true
                unitTests.includeAndroidResources = true
            }
        
            packagingOptions {
                resources.excludes.add("META-INF/*")
                exclude "**/attach_hotspot_windows.dll"
                exclude "META-INF/licenses/**"
                exclude "META-INF/AL2.0"
                exclude "META-INF/LGPL2.1"
            }
      }
    
dependencies {}

    configurations.all {
        resolutionStrategy {
            eachDependency { details ->
                if ('org.jacoco' == details.requested.group) {
                    details.useVersion "0.8.7"
                }
            }
        }
    }
      

       

                                                                         
GvHarish
  • 340
  • 2
  • 13