gradle/gradle · error · ServiceCreationException

Could not create service of %s using %s.%s().

Error message

Could not create service of %s using %s.%s().

What it means

A service produced by a factory method (a create*() method on a provider added via addProvider, or an explicit factory) failed because invoking the method threw; the registry wraps the original exception in ServiceCreationException and reports the service type plus the factory's owner class and method name. The cause chain carries the real error, so the factory method body, not the registry wiring, is where the failure happened.

Source

Thrown at platforms/core-runtime/service-registry-impl/src/main/java/org/gradle/internal/service/DefaultServiceRegistry.java:909

        }

        @Override
        protected String getFactoryDisplayName() {
            return String.format("method %s.%s()", format(getMethod().getOwner()), getMethod().getName());
        }

        @Override
        protected Object invokeMethod(Object[] params) {
            if (target == null) {
                throw new IllegalStateException("The target of the factory method has been discarded after the first service creation attempt");
            }

            Object result;
            ServiceMethod method = getMethod();
            try {
                result = method.invoke(target, params);
            } catch (Exception e) {
                throw new ServiceCreationException(String.format("Could not create service of %s using %s.%s().",
                    format("type", serviceTypes),
                    method.getOwner().getSimpleName(),
                    method.getName()),
                    e);
            }

            if (result == null) {
                throw new ServiceCreationException(String.format("Could not create service of %s using %s.%s() as this method returned null.",
                    format("type", serviceTypes),
                    method.getOwner().getSimpleName(),
                    method.getName()));
            }
            return result;
        }

        @Override
        protected Object createServiceInstance() {
            Object result = super.createServiceInstance();

View on GitHub (pinned to 534f27719b)

Solutions

  1. Inspect the cause of the ServiceCreationException and fix what the factory method body threw (usually an NPE from missing setup)
  2. Make the factory defensive: validate its inputs at the top and throw a descriptive exception naming the missing piece
  3. If the failure is environmental, resolve the environment or configuration before the service is first requested

Example fix

// before
public Cache createCache() { return new DefaultCache(settings.dir()); } // NPE when settings unset

// after
public Cache createCache() {
    if (settings == null) {
        throw new IllegalStateException("settings not registered before Cache creation");
    }
    return new DefaultCache(settings.dir());
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
    return registry.get(Cache.class);
} catch (ServiceCreationException e) {
    if (e.getMessage() != null && e.getMessage().contains("Could not create service of") && e.getCause() != null) {
        throw new IllegalStateException("Cache factory failed: " + e.getCause().getMessage(), e.getCause());
    }
    throw e;
}

Prevention

When it happens

Trigger: registry.get(Foo.class) where Foo is produced by, for example, Foo createFoo() on a provider object, and the method throws an NPE, IllegalStateException or similar because configuration is unset, the environment is missing something, or the factory body has a bug.

Common situations: Factory methods dereferencing state initialized later or conditionally absent (config flags, env vars, files); factories calling external systems that are down; refactorings that made a dependency nullable.

Related errors


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