Cannot resolve object of unknown type when adding Dependency implementation

I have a custom class in my gradle plugin that implements the Dependency interface. When I try to add it to the compile configuration, I get an error ‘Cannot resolve object of unknown type CustomDependency’.
I verified that this is not an import issue by instantiating the object explicitly. That step does not fail, only when adding it to the dependencies does the error occur.

CustomDependency dependency = new CustomDependency()
project.getConfigurations().getByName("compile").getDependencies().add(project.getDependencies().create(dependency)

I’ve worked around it for now by having the dependency object has a toString method that exports the maven string format but it would be useful to know why the compiler isn’t able to find the object.

Why do you need to create a custom implementation of Dependency? You can achieve the same by project.getDependencies().create(notation).

I do some processing on the dependency versions and stuff and I don’t want to do that in strings. I was eventually able to get it to work by extending DefaultExternalModuleDependency instead of the Dependency interface. I’m not sure why that works though.

Keep in mind that DefaultExternalModuleDependency is an internal class and may change with any Gradle version. Alternatively, you could do the processing in a method you write which 1) does your processing and 2) creates the dependency as shown in my previous answer.

I already do that in a toString() method but my question is more about why this is happening at all.

Because we check for valid types in Gradle and throw an exception if we encounter an unknown type. See class CachingDependencyResolveContext.java for more information.