gradle/gradle · error · InvalidUserDataException

Cannot create a %s because this type is not known to %s. Kno

Error message

Cannot create a %s because this type is not known to %s. Known types are: %s

What it means

Detached resolvers (newDetachedResolver) back adhoc resolution: detached configurations, buildscript/plugin classpath resolution, and settings dependency resolution management. Gradle wires a configureEach guard so that every configuration in that container stays non-consumable, because detached resolvers only represent adhoc root components that never expose variants. Marking any configuration there consumable raises InvalidUserCodeException immediately.

Source

Thrown at platforms/core-configuration/domain-object-collections/src/main/java/org/gradle/api/internal/DefaultPolymorphicNamedEntityInstantiator.java:47

import java.util.Map;
import java.util.Set;

public class DefaultPolymorphicNamedEntityInstantiator<T> implements PolymorphicNamedEntityInstantiator<T> {
    private final Map<Class<? extends T>, NamedDomainObjectFactory<? extends T>> factories = new HashMap<>();
    private final Class<? extends T> baseType;
    private final String displayName;

    public DefaultPolymorphicNamedEntityInstantiator(Class<? extends T> type, String displayName) {
        this.displayName = displayName;
        this.baseType = type;
    }

    @Override
    public <S extends T> S create(String name, Class<S> type) {
        @SuppressWarnings("unchecked")
        NamedDomainObjectFactory<S> factory = (NamedDomainObjectFactory<S>) factories.get(type);
        if (factory == null) {
            throw new InvalidUserDataException(
                    String.format("Cannot create a %s because this type is not known to %s. Known types are: %s", type.getSimpleName(), displayName, getSupportedTypeNames()),
                    new NoFactoryRegisteredForTypeException());
        }
        return factory.create(name);
    }

    public String getSupportedTypeNames() {
        List<String> names = new ArrayList<>();
        for (Class<?> clazz : factories.keySet()) {
            names.add(clazz.getSimpleName());
        }
        Collections.sort(names);
        return names.isEmpty() ? "(None)" : Joiner.on(", ").join(names);
    }

    @Override
    public <U extends T> void registerFactory(Class<U> type, NamedDomainObjectFactory<? extends U> factory) {
        if (!baseType.isAssignableFrom(type)) {

View on GitHub (pinned to 534f27719b)

Solutions

  1. Do not flip canBeConsumed on detached or plugin-context configurations
  2. Expose variants through a named consumable configuration in the project's own configuration container
  3. Resolve via a resolvable detached configuration and keep it consumption-free

Example fix

// before
def detached = project.configurations.detachedConfiguration(dep)
detached.withCanBeConsumed(true) // InvalidUserCodeException

// after
def detached = project.configurations.detachedConfiguration(dep)
detached.withCanBeResolved(true) // adhoc resolution only, never consumable
// to expose variants, use the project container instead:
configurations.consumable('myOutgoing') { /* ... */ }
Defensive patterns

Strategy: validation

Validate before calling

// Guard shared helpers that clone configuration state into detached contexts
def template = configurations.findByName('api')
if (template?.isCanBeConsumable()) {
    throw new GradleException('Refusing to detach or clone a consumable configuration; detached resolvers are resolve-only')
}

Prevention

When it happens

Trigger: Calling withCanBeConsumed(true)/setCanBeConsumed(true), directly or by copying a consumable configuration's settings, on a configuration living in a detached resolver: a project.configurations.detachedConfiguration(...) instance, plugin-resolution classpath configurations, or settings-level resolution services.

Common situations: Generic helper code that 'normalizes' configurations by setting canBeConsumed=true; plugins applying the same configuration template to detached and project containers; copying a consumable configuration into a detached one for adhoc resolution.

Related errors


AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22). Data as JSON: /api/errors/448f4684705864a4. Report an issue: GitHub.