afterEvaluate not working after updating android gradle plugin to 4.1.0

Viewed 1743

Hello we use afterEvaluate in our root build.gradle to set some BuildConfig values and upload archives, but after updating gradle plugin from

com.android.tools.build:gradle:4.0.1

to

com.android.tools.build:gradle:4.1.0

It seems afterEvaluate is not being triggered at all.

Any idea how to solve this issue?

build.gradle looks like this

subprojects{
...
 android{
 ...
    afterEvaluate { project ->
        buildTypes.each { buildType ->
            final String suffix = buildType.name == 'release' ? '' : ".${buildType.name}"

            project.libraryApplicationIds.each { String constant, String libAppId ->
                buildType.buildConfigField "String", constant, "\"$libAppId$suffix\""
                buildType.manifestPlaceholders[constant] = "$libAppId$suffix"
            }
        }
    }
 ...
 }
...
}
1 Answers

Looks like I have faced the same issue, but found it in our Gradle plugins which suppose to inject build config fields in app variants.

I managed to reproduce this on clean project with such simple plugin which emulates what we do in our plugin.

class MyPlugin: Plugin<Project> {

    override fun apply(project: Project) {
        project.afterEvaluate {
            val androidExtension = project.extensions.getByType(AppExtension::class.java)
            androidExtension.applicationVariants.forEach {
                System.out.println("Adding build config for ${it.name}")
                it.buildConfigField("String", "TEST_CONFIG", "\"TEST\"")
            }
        }
    }

}

Gradle version in both cases is 6.5

So I just change AGP version from 4.0.2 to 4.1.0. In the former case (ver. 4.0.2) it works fine and TEST_CONFIG field is added to BuildConfig, in the latter field is not added to BuildConfig

What I noticed is that build logs are different in both cases when I run ./gradlew :app:generateDebugBuildConfig

For old plugin version when everything works output is:

> Configure project :app

> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:generateDebugBuildConfig
Adding build config for debug
Adding build config for release

For new plugin version when it does not work:

> Configure project :app

> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
Adding build config for debug
Adding build config for release
> Task :app:generateDebugBuildConfig

So it looks like there are some internal changes in the plugin which cause this reordering, but unfortunately I have not found the fix yet

UPDATE: I have found that this got broken in version 4.1.0-alpha08

UPDATE: Sample project on Github

UPDATE: Reported to Google, waiting for response - Google Issue Tracker

Related