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
- Do not flip canBeConsumed on detached or plugin-context configurations
- Expose variants through a named consumable configuration in the project's own configuration container
- 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
- Treat detached configurations as resolvable-only
- Keep variant exposure (outgoing, extendsFrom of consumables) to named configurations in the project container
- Never apply blanket withCanBeConsumed(true) in shared configuration code
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
- Cannot call %s on %s as changes to this collection are disal
- Cannot add a %s with name '%s' as a %s with that name alread
- Cannot create a %s named '%s' because this container does no
- Cannot register a factory for type %s because it is not a su
- Cannot register a factory for type %s because a factory for
AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22).
Data as JSON: /api/errors/448f4684705864a4.
Report an issue: GitHub.