Search code examples
androidgradleandroid-gradle-plugingradle-plugintoml

Gradle sync problem with plugins in `buildSrc` directory


Since upgrading to current versions, alike Gradle 8.10 with AGP 8.60 (Koala Feature Drop), I cannot sync my project anymore, which also publishes a plugin from the buildSrc directory to GitHub & JitPack (I can load it from there and it does build when renaming buildSrc).

The error message would be:

class com.android.tools.idea.gradle.project.sync.SyncVariantResultFailure cannot be cast to
class com.android.tools.idea.gradle.project.sync.SyncVariantResultSuccess
(com.android.tools.idea.gradle.project.sync.SyncVariantResultFailure and
 com.android.tools.idea.gradle.project.sync.SyncVariantResultSuccess
are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @35ab5cf2)

class com.android.tools.idea.gradle.project.sync.SyncVariantResultFailure cannot be cast to
class com.android.tools.idea.gradle.project.sync.SyncVariantResultSuccess
(com.android.tools.idea.gradle.project.sync.SyncVariantResultFailure and
 com.android.tools.idea.gradle.project.sync.SyncVariantResultSuccess
are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @35ab5cf2)

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

Which suggests, that this is an AGP issue: com.android.tools.idea.gradle.project.sync. Neither of the two options offered there provides a remedy. Also cleared caches and manually deleted all generated directories, but to no avail. The stack-trace is too long, but generally points to:

at com.android.tools.idea.gradle.project.sync.VariantDiscovery.discoverVariantsAndSync(VariantDiscovery.kt:141)

I've renamed the buildSrc directory and tried, that's why I know this is the cause. Also when reverting com.android.tools.build:gradle-api from version 8.6.0 to 8.5.2, it builds again.

plugins {
    id 'org.gradle.java-gradle-plugin'
}
dependencies {
    implementation "com.android.tools.build:gradle-api:8.6.0"
    // ...
}

It's not even about the publishing plugins, but it already fails to sync when org.gradle.java-gradle-plugin is being applied. How to avoid this SyncVariantResultFailure, in order to continue running plugin code from the buildSrc directory - or does buildSrc need to be declared differently now? Not sure if this makes a difference, but the plugin uses libs.versions.toml, which is only being seen when directly opening directory buildSrc, not as nested directory.

Which variant it is looking for and how to define it?


Solution

  • The underlying issue was actually something else, basically a libs.versions.toml syntax error,
    some_library_301 must be some_library_v301, because a syntax rule is being enforced now:

    Invalid alias some_library_301. There must be letter after '-' or '_ delimiter.

    This wasn't even related to the buildSrc directory, but the parent project's libs.versions.toml. Before it already complained about it (marked yellow), but now this fails with a rather cryptic error message. https://docs.gradle.org/8.10/userguide/version_catalog_problems.html