简体   繁体   中英

How to fix Build.gradle() error in Android Studio when using Jococo as a dependency?

I'm trying to build an open-source project named AmazeFileManager from Github - AmazeFileManager

I have previously built the project in Android Studio using Gradle with

Android Gradle plugin: 3.6.4
Gradle version: 5.6.4

Problem:

When I tried updating the Android plugin and Gradle versions to

Android Gradle Plugin: 4.1.0
Gradle Version: 6.5

I get this error when rebuilding the project:

Cannot set the value of read-only property 'executionData' for task ':app:jacocoTestFdroidDebugUnitTestReport' of type org.gradle.testing.jacoco.tasks.JacocoReport

Question:

What can I do to resolve this error so I can successfully rebuild the project?

Below are the snippets of the Gradle files used:

build.gradle (Module:app)

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'com.dicedmelon.gradle:jacoco-android:0.1.4'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

plugins {
    id "com.diffplug.gradle.spotless" version "4.3.0"
}

allprojects {

    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
        maven { url "https://jcenter.bintray.com" }
        mavenCentral()
    }
    tasks.withType(Test) {
        maxParallelForks = 2
        maxHeapSize = "2g"
        forkEvery = 2
    }
}

spotless {
    java {
        licenseHeaderFile 'spotless.license-java'
        target 'app/src/**/*.java'
        googleJavaFormat('1.7')
        removeUnusedImports() // removes any unused imports
        importOrder 'java', 'javax', 'org', 'com', 'android', 'androidx', ''
    }
}

configurations {
    robo16
    robo17
    robo18
    robo19
    robo21
    robo22
    robo23
    robo24
    robo25
    robo26
    robo27
    robo28
    robo29
}

dependencies {
    robo16 "org.robolectric:android-all:4.1.2_r1-robolectric-r1"
    robo17 "org.robolectric:android-all:4.2.2_r1.2-robolectric-r1"
    robo18 "org.robolectric:android-all:4.3_r2-robolectric-r1"
    robo19 "org.robolectric:android-all:4.4_r1-robolectric-r2"
    robo21 "org.robolectric:android-all:5.0.2_r3-robolectric-r0"
    robo22 "org.robolectric:android-all:5.1.1_r9-robolectric-r2"
    robo23 "org.robolectric:android-all:6.0.1_r3-robolectric-r1"
    robo24 "org.robolectric:android-all:7.0.0_r1-robolectric-r1"
    robo25 "org.robolectric:android-all:7.1.0_r7-robolectric-r1"
    robo26 "org.robolectric:android-all:8.0.0_r4-robolectric-r1"
    robo27 "org.robolectric:android-all:8.1.0-robolectric-4611349"
    robo28 "org.robolectric:android-all:9-robolectric-4913185-2"
    robo29 "org.robolectric:android-all:10-robolectric-5803371"
}

def robolectricDependencies = "${rootProject.buildDir.path}/robolectric"

task fetchRobolectricDependencies(type: Copy) {
    from configurations.robo16
    from configurations.robo17
    from configurations.robo18
    from configurations.robo19
    from configurations.robo21
    from configurations.robo22
    from configurations.robo23
    from configurations.robo24
    from configurations.robo25
    from configurations.robo26
    from configurations.robo27
    from configurations.robo28
    from configurations.robo29
    into robolectricDependencies
}

subprojects {
    afterEvaluate {
        if (project.plugins.hasPlugin("com.android.application") || project.plugins.hasPlugin("com.android.library")) {
            android {
                testOptions.unitTests.all {
                    systemProperty 'robolectric.offline', 'true'
                    systemProperty 'robolectric.dependency.dir', robolectricDependencies
                }
            }

            tasks.withType(Test) {
                it.dependsOn fetchRobolectricDependencies
            }
        }
        if (project.plugins.hasPlugin("jacoco-android")){
            android {
                testOptions.unitTests.all {
                    jacoco {
                        excludes = ['jdk.internal.*']
                    }
                }
            }
        }
        dependencies {
            compileOnly 'com.github.pengrad:jdk9-deps:1.0'

            if (project.hasProperty('kapt')) {
                kapt 'javax.xml.bind:jaxb-api:2.3.1'
                kapt 'com.sun.xml.bind:jaxb-core:2.3.0.1'
                kapt 'com.sun.xml.bind:jaxb-impl:2.3.2'
            }

            annotationProcessor 'javax.xml.bind:jaxb-api:2.3.1'
            annotationProcessor 'com.sun.xml.bind:jaxb-core:2.3.0.1'
            annotationProcessor 'com.sun.xml.bind:jaxb-impl:2.3.2'
        }
    }

}  

build.gradle(Project:AmazeFileManager-release-3.5)

   buildscript {
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:4.1.0'
            classpath 'com.dicedmelon.gradle:jacoco-android:0.1.4'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    plugins {
        id "com.diffplug.gradle.spotless" version "4.3.0"
    }
    
    allprojects {
    
        repositories {
            google()
            jcenter()
            maven { url "https://jitpack.io" }
            maven { url "https://jcenter.bintray.com" }
            mavenCentral()
        }
        tasks.withType(Test) {
            maxParallelForks = 2
            maxHeapSize = "2g"
            forkEvery = 2
        }
    }
    
    spotless {
        java {
            licenseHeaderFile 'spotless.license-java'
            target 'app/src/**/*.java'
            googleJavaFormat('1.7')
            removeUnusedImports() // removes any unused imports
            importOrder 'java', 'javax', 'org', 'com', 'android', 'androidx', ''
        }
    }
    
    configurations {
        robo16
        robo17
        robo18
        robo19
        robo21
        robo22
        robo23
        robo24
        robo25
        robo26
        robo27
        robo28
        robo29
    }
    
    dependencies {
        robo16 "org.robolectric:android-all:4.1.2_r1-robolectric-r1"
        robo17 "org.robolectric:android-all:4.2.2_r1.2-robolectric-r1"
        robo18 "org.robolectric:android-all:4.3_r2-robolectric-r1"
        robo19 "org.robolectric:android-all:4.4_r1-robolectric-r2"
        robo21 "org.robolectric:android-all:5.0.2_r3-robolectric-r0"
        robo22 "org.robolectric:android-all:5.1.1_r9-robolectric-r2"
        robo23 "org.robolectric:android-all:6.0.1_r3-robolectric-r1"
        robo24 "org.robolectric:android-all:7.0.0_r1-robolectric-r1"
        robo25 "org.robolectric:android-all:7.1.0_r7-robolectric-r1"
        robo26 "org.robolectric:android-all:8.0.0_r4-robolectric-r1"
        robo27 "org.robolectric:android-all:8.1.0-robolectric-4611349"
        robo28 "org.robolectric:android-all:9-robolectric-4913185-2"
        robo29 "org.robolectric:android-all:10-robolectric-5803371"
    }
    
    def robolectricDependencies = "${rootProject.buildDir.path}/robolectric"
    
    task fetchRobolectricDependencies(type: Copy) {
        from configurations.robo16
        from configurations.robo17
        from configurations.robo18
        from configurations.robo19
        from configurations.robo21
        from configurations.robo22
        from configurations.robo23
        from configurations.robo24
        from configurations.robo25
        from configurations.robo26
        from configurations.robo27
        from configurations.robo28
        from configurations.robo29
        into robolectricDependencies
    }
    
    subprojects {
        afterEvaluate {
            if (project.plugins.hasPlugin("com.android.application") || project.plugins.hasPlugin("com.android.library")) {
                android {
                    testOptions.unitTests.all {
                        systemProperty 'robolectric.offline', 'true'
                        systemProperty 'robolectric.dependency.dir', robolectricDependencies
                    }
                }
    
                tasks.withType(Test) {
                    it.dependsOn fetchRobolectricDependencies
                }
            }
            if (project.plugins.hasPlugin("jacoco-android")){
                android {
                    testOptions.unitTests.all {
                        jacoco {
                            excludes = ['jdk.internal.*']
                        }
                    }
                }
            }
            dependencies {
                compileOnly 'com.github.pengrad:jdk9-deps:1.0'
    
                if (project.hasProperty('kapt')) {
                    kapt 'javax.xml.bind:jaxb-api:2.3.1'
                    kapt 'com.sun.xml.bind:jaxb-core:2.3.0.1'
                    kapt 'com.sun.xml.bind:jaxb-impl:2.3.2'
                }
    
                annotationProcessor 'javax.xml.bind:jaxb-api:2.3.1'
                annotationProcessor 'com.sun.xml.bind:jaxb-core:2.3.0.1'
                annotationProcessor 'com.sun.xml.bind:jaxb-impl:2.3.2'
            }
        }
    
    }

  

build.gradle(Module:commons_compress_7z)

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard.cfg'
        }
    }

}

dependencies {
    implementation 'org.apache.commons:commons-compress:1.18'
    implementation 'org.tukaani:xz:1.8'
}

If you haven't already found the cause for this, it is because of changes in Gradle 6, which the new AGP uses.

For more info, check out this Medium post or more specificallythis PR in the jacoco-android library that you are using to add the jacoco tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM