Gradle plugin doesn't add dependencies to the project

I have a custom gradle plugin, which is supposed to read dependencies from json and add them, everything is working, but the dependencies are not added for some reason. All of this is happening in afterEvaluate.

        project.afterEvaluate {

            if (dependencies.isNotEmpty()) {
                dependencies.forEach {
                    println("Applying dependency: ${it.key}")
                    if (it.value) {
                        //project.dependencies.add("runtimeOnly", it.key)
                        mcRunt.dependencies.add(project.dependencies.create(it.key))
                    } else {
                        //project.dependencies.add("implementation", it.key)
                        mcImpl.dependencies.add(project.dependencies.create(it.key))
                    }
                }
                dependencies.clear()
                project.configurations.getByName("implementation").extendsFrom(mcImpl)
                project.configurations.getByName("runtimeOnly").extendsFrom(mcRunt)
            }
        }

it correctly prints out Applying dependency: some-dependency-name,
but they are not being added, does anyone know why? thanks

It probably depends on the remaining build script that you didn’t show. But anyway using afterEvaluate is bad practice anyway. Guessing mcRunt and mcImpl are configurations, the way you add dependencies is not right anyway, but you should add the dependencies to project.dependencies instead. But this additionally looks like you do cross-project configuration which you anyway should also avoid wherever possible, but use convention plugins instead.

When adding them to project.dependencies all of the dependencies couldn’t be resolved (marked as (n)), what would be the correct approach to add the dependencies then?

Hm, I’m on mobile right now, but not sure what I meant there. Right now I think you should be able to add the dependencies directly to the configuration.

But anyway, (n) does not mean it could not be resolved. It means it was not tried to be resolved because you are looking at a not-resolvable configuration in which they were declared. You usually then have a resolvable configuration that extends it.

Well if I keep it in original state(mcImpl and mcRunt configurations which extend implementation and runtimeOnly configurations), then run dependencies task I get following output:

They are being applied to compileClasspath but not to implementation, might be the problem.
Also if using afterEvaluate isn’t the correct way to add dependencies, where else should I be adding them?

What is your problem then? implementation is not resolvable, which probably causes your confusion, but as implementation is just for declaring dependencies, but for resolving, it shouldn’t matter.

afterEvaluate is practically always wrong and just adding ordering quirks and timing problems, except for exceptional cases where you have to work with legacy plugins. How to do it “properly” is hard to say without knowing more of the build.