4

After upgrading Gradle from 6.8 to 7.1.1 I'm getting:

A problem occurred evaluating script.
> Failed to apply plugin class 'org.gradle.api.publish.plugins.PublishingPlugin'.
   > Cannot run Project.afterEvaluate(Action) when the project is already evaluated.

Thrown from my publish.gradle file:

configure(subprojects.findAll({ it.name in ["subpr1", "subpr2", "subpr3"] })) {
    subproject ->
        apply plugin: 'com.android.library'
        apply plugin: 'maven-publish'  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

I did not find any evaluation* or evaluate* tasks usages in my build.gradle files.

Any ideas how to fix this?

My root build.gradle:


buildscript {
    ext.gradle_version = "7.0.0"

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
    }
}

ext {
    ...
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

def properties = new Properties()
properties.load(new FileInputStream("local.properties"))

allprojects {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        maven {
            url = uri("...")
            credentials {
                username = properties.getProperty("...")
                password = properties.getProperty("...")
            }
        }
    }
}

apply plugin: 'android-reporting'

task publishAll() {
    subprojects.each { pr ->
        dependsOn {
            pr.tasks.findAll { task -> task.name.startsWith('publish') }
        }
    }
    outputs.upToDateWhen { false }
}
publishAll.outputs.upToDateWhen { false }

task clean(type: Delete) {
    delete rootProject.buildDir
}

apply from: 'publish.gradle'

My guess would be that something is the issue here and/or that it's related to the migration guide section "afterEvalute is now an error".

task publishAll() {
    subprojects.each { pr ->
        dependsOn {
            pr.tasks.findAll { task -> task.name.startsWith('publish') }
        }
    }
    outputs.upToDateWhen { false }
}
publishAll.outputs.upToDateWhen { false }

The build.gradle of subproject1:

plugins {
    id 'com.android.library'
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionName rootProject.ext.cyfaceBackendVersion
        testInstrumentationRunner rootProject.ext.testInstrumentationRunner
    }

    productFlavors {
        a {
            dimension "project"
        }
        b {
            dimension "project"
        }
        mock {
            dimension "mode"
        }
        full {
            dimension "mode"
        }
    }

    buildTypes {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = true
        }
    }

    compileOptions {
        sourceCompatibility rootProject.ext.sourceCompatibility
        targetCompatibility rootProject.ext.targetCompatibility
    }

    lintOptions {
        disable 'ObsoleteLintCustomCheck'
    }
}

dependencies {...}

hb0
  • 3,350
  • 3
  • 30
  • 48
  • If I move `apply from: 'publish.gradle'` a bit up in the `build.gradle` I do get another error instead, the Line `classpath += files(configurations.compile)` later in my `publish.gradle` throws ` MissingPropertyException: Could not get unknown property 'compile'. – hb0 Aug 05 '21 at 13:54

2 Answers2

3

The problem is:

apply from: 'publish.gradle'

Which is executed after the evaluate phase and, thus, the error:

Cannot run afterEvaluate when the project is already evaluated

The solution is to write a convention gradle plugin in buildSrc.

To follow up, see e.g. this video from Gradle.

hb0
  • 3,350
  • 3
  • 30
  • 48
0

I had almost the same problem. There was the following code in root build.gradle

plugins {
    id 'java'
}

description = "MyPrject"

repositories {
    mavenCentral()
}

dependencies {
    implementation(project(":module1"))
    implementation(project(":module2"))
    implementation(project(":module3"))
}

...

gradle.projectsEvaluated {

    subprojects {

        apply plugin: 'java'
        apply plugin: 'maven-publish'

        repositories {
            mavenCentral()
        }

        task sourcesJar(type: Jar, dependsOn: classes) {
            archiveClassifier.set("sources")
            from sourceSets.main.allSource
        }

        jar {
            manifest {
                attributes(
                        'Implementation-Title': 'MyPrject',
                        'Implementation-Version': project.version
                )
            }
        }

        publishing {
            publications {
                mavenJava(MavenPublication) {
                    artifactId "$archivesBaseName"

                    from components.java
                    artifact sourcesJar
                }
            }

            repositories {
                ...
            }
        }

    }
}

In my case solution is remove apply plugin commands from subprojects sections and add this to all my module build.gradle file (module1/build.gradle, module2/build.gradle and module3/build.gradle)

plugins {
    id 'java'
    id 'maven-publish'
}

After that Gradle 7.X version build SUCCESS