How to resolve platform dependencies

I am writing an init script that registers tasks to resolve and print compile dependencies for a module variant. Here is a fragment of the script:

def registerResolveDependenciesTask(Project project, Object variant) {
    def superConfig = variant.compileConfiguration
    def resolvableConfig =
            project.configurations.resolvable("resolvable" + superConfig.name.capitalize()) {
                extendsFrom(superConfig)
                attributes {
                    attribute(Attribute.of("artifactType", String.class), "jar")
                    attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, Usage.JAVA_API))
                    attribute(BuildTypeAttr.ATTRIBUTE, project.objects.named(BuildTypeAttr, variant.buildType.name))
                }
            }
    project.tasks.register("resolve" + variant.name.capitalize() + "CompileDependencies") {
        doLast {
            println "Resolved compile dependencies:"
            resolvableConfig.get().files.forEach {
                println it
            }
        }
    }
}

It works as expected as long as the module has no platform dependencies.
For example, I run it as ./gradlew --init-script=E:\Work\Projects\Sample\init.gradle :app:resolveDebugUnitTestCompileDependencies and the output is something like:

Resolved compile dependencies:
E:\Work\Projects\Sample\app\build\intermediates\compile_app_classes_jar\debug\bundleDebugClassesToCompileJar\classes.jar
C:\Users\alexe\.gradle\caches\modules-2\files-2.1\androidx.compose.material3\material3-desktop\1.2.1\d8e381c465466d16c573d8ecfc96c3ed45988276\material3-desktop-1.2.1.jar
...

If a module has platform dependencies:

    ...
    implementation("com.google.firebase:firebase-bom:33.2.0")
    implementation("com.google.firebase:firebase-analytics")
    implementation("com.google.firebase:firebase-config")
}

It would fail with an error:

* What went wrong:
Execution failed for task ':app:resolveDebugUnitTestCompileDependencies'.
> Could not resolve all files for configuration ':app:resolvableDebugUnitTestCompileClasspath'.
   > Could not find com.google.firebase:firebase-analytics:.
     Required by:
         project :app
   > Could not find com.google.firebase:firebase-config:.
     Required by:
         project :app

How do I fix this? I need to resolve all dependencies of a module variant.
Thanks!

Might your problem be, that there is indeed no platform dependency as you miss the platform(...) around and compiling would just fail the same?

Indeed, my bad! It works now

I have another case though.

Let’s say there are two modules: utils and app.
The utils module has a platform dependency:

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:33.2.0"))
    implementation("com.google.firebase:firebase-analytics")
    implementation("com.google.firebase:firebase-config")
}

The app module has the following dependencies:

dependencies {
    implementation(project(":utils"))

    implementation("com.google.firebase:firebase-analytics")
}

This assembles perfectly fine.

However, resolving dependencies doesn’t work and gives the same error.
./gradlew --init-script=E:\Work\Projects\Sample\init.gradle :app:resolveDebugUnitTestRuntimeDependencies
fails with:

Execution failed for task ':app:resolveDebugUnitTestRuntimeDependencies'.
> Could not resolve all files for configuration ':app:resolvableDebugUnitTestRuntimeClasspath'.
   > Could not find com.google.firebase:firebase-analytics:.
     Required by:
         project :app

How can I fix this? Thank you.

Can you show an MCVE where it can compile but your task fails?
Because it should not compile.

The idiomatic way is to also have implementation(platform("com.google.firebase:firebase-bom:33.2.0")) in app.

If utils is a library you can make the platform api instead of implementation as it then leaks into the consumer compile classpath and thus can be used from there. But usually it is bad practice to use transitive dependencies and api should only be used for things in your - well - API like return types, parameter types, superclasses, …

For constraints like a platform it is less problematic but still might be a bit unclean.

If it is about not declaring the version multiple times, I’d strongly recommend a version catalog.

I can’t attach a zip of the project here so I am sharing a link to it on GitHub GitHub - artsmvch/gradle-bug: Gradle ResolveDependencies Bug

Please try it:

  • ./gradlew :app:assembleDebug runs successfully
  • ./gradlew --init-script=./init.gradle :app:resolveDebugUnitTestRuntimeDependencies fails because it cannot resolve a dependency

If you execute ./gradlew :app:compileDebugAndroidTestJavaWithJavac or ./gradlew :app:dependencies you see that those are also failing to resolve the same.

I tried running ./gradlew :app:dependencies and it was successful, here is the output gist:aab03044da1b2099692184069929de04 · GitHub

I still wonder why it assembles successfully🤔

That task never causes the whole build to fail, but search for “FAILED” and you will see that the depencency could not be resolved.