gradleandroid-gradle-pluginmaven-publish

Gradle 7 Migration: Failed to apply PublishPlugin (maven-publish): Cannot run afterEvaluate when the project is already evaluated


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


Solution

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