'Jacoco in Android project - what am I missing?

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...



Solution 1:[1]

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"
                }
            }
        }
    }
      

       

                                                                         

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GvHarish