Hello community :wave: I am looking for the way t...
# community-support
t
Hello community 👋 I am looking for the way to add dependency to the existing configuration. The brief steps are: 1. Make a network call to get the json, save it to file expose it as task output. The task named as
LoadCommunityNativeConfig
. 2. Wire task output to the project relying on
org.gradle.api.provider.Provider
. The problem is that the
LoadCommunityNativeConfig
is not executed when its output accessed. As far as I understand, I am missing a proper wiring of
LoadCommunityNativeConfig
to the existing tasks. The Gradle runtime throws
Copy code
Querying the mapped value of flatmap(provider(task 'loadCommunityNativeConfig', class LoadCommunityNativeConfig)) before task ':app:loadCommunityNativeConfig' has completed is not supported
Below 2 approaches I’ve tried to add the dependency. Snippet 1
Copy code
project.dependencies.addProvider("implementation", loadCommunityNativeConfig.flatMap { it.outArtifact }.map {
    // maps json file to data class NativeConfig
	val nativeConfig: NativeConfig = it.asFile.toNativeConfig()
	val isEnabled = nativeConfig.facebookConfig.isNoOp
	if (isEnabled) {
	  "com.facebook.android:facebook-core:17.0.1"
	} else {
	  "javax.inject:javax.inject:1"
	}
})
Snippet 2
Copy code
val implementation = project.configurations.named("implementation")
val dependencies = project.dependencies

implementation.configure {
    val container = this
    container.dependencies.addLater(
        loadCommunityNativeConfig.flatMap { it.outArtifact }.map {
            val nativeConfig = it.asFile.toNativeConfig()
            val isEnabled = nativeConfig.facebookConfig.isNoOp
            if (isEnabled) {
                dependencies.create("com.facebook.android:facebook-core:17.0.1")
            } else {
                dependencies.create("javax.inject:javax.inject:1")
            }
        }
    )
}
In addition, I’ve tried to combine ☝️ both approaches with this snippet.
Copy code
project.artifacts.add("implementation", loadCommunityNativeConfig.flatMap { it.outArtifact }) {
            builtBy(loadCommunityNativeConfig)
        }
I would appreciate any feedback or guidance in the right direction 🙇
Maybe, my approach is wrong and instead of adding it dynamically. I can add during configuration and then exclude during dependency resolution stage, though, I can not find anything that would allow me to wire my custom task to the dependency resolution stage.
Ultimately I need to achieve this goal. 1. Make a network call. 2. Get json. 3. Out of json make a decision to either add or not to add dependency to
implementation
configuration.
v
Yeah, well, you most probably cannot do the network call in a task's execution phase then, unless maybe that specific configuration is only resolved at execution time. Which for example then also means no configuration cache, ever.
👀 1
Just make the network call in a provider there directly. Or if you need the result multiple times maybe through a shared build service to only do it once in a given build.
t
Just make the network call in a provider there directly.
Worked out.
👌 1
Is there a way in Gradle to wire a task as dependency for the configuration resolution process? I feel like have a dependency on the task that produces file is better.