skip to content

Lazy Configuration API

Gradle's lazy value model: Provider and Property, transformation chains, lazy collections, and the ObjectFactory that creates managed types. Interviewers ask because the entire modern authoring API is built on it.

part ofGradleread it →
on this pageshow

questions

page 2 of 2

What does ObjectFactory.named() do, and when do you use Named types in Gradle?

level: seniorimportance: should knowfreq 30%

basics

~10 s

objects.named(Type, "value") creates an instance of a Named interface subtype carrying that string name. It's used for attribute values like Usage, Category, or custom variant attributes in dependency resolution.

open as a page

How does ObjectFactory.newInstance() work, and how does service injection into the created instance behave?

level: seniorimportance: should knowfreq 28%

basics

~10 s

objects.newInstance(Type, args) creates an instance, generating impls for abstract managed types and supplying constructor @Inject services (like ObjectFactory) plus any extra args you pass. It's how you create nested DSL objects.

open as a page

How do you run an external process during configuration in a config-cache-safe way using ProviderFactory?

level: seniorimportance: should knowfreq 45%

basics

~10 s

Use providers.exec { commandLine(...) } which returns an ExecOutput whose standardOutput is a Provider. It is lazy and tracked as a configuration-cache input, unlike project.exec.

open as a page

What pitfalls arise when wiring ProviderFactory values into task inputs, and how do you keep them lazy and config-cache-safe?

level: seniorimportance: should knowfreq 30%

basics

~10 s

Don't call .get() at configuration time, don't capture Project in task actions, and wire Providers into Property inputs with .set(provider) so resolution stays deferred and tracked.

open as a page

What is a ValueSource and when would you use providers.of(...) over providers.environmentVariable or providers.exec?

level: seniorimportance: should knowfreq 35%

basics

~10 s

A ValueSource is a reusable, parameterized unit of external reading. You implement obtain() and call providers.of(MySource) { parameters... } to get a lazy, config-cache-safe Provider for any custom external input.

open as a page

You're modernizing an old custom task that uses plain String/File fields. How do you migrate it to Property/Provider, and what pitfalls do you watch for?

level: seniorimportance: should knowfreq 30%

basics

~10 s

Replace eager fields with Property<T> declared as abstract managed properties or created via ObjectFactory, annotate inputs/outputs, set defaults with convention(), and read values at execution (inside the action) instead of at configuration time.

open as a page

Design a plugin extension that lets users aggregate values from multiple build-script blocks into a collection consumed by a task. How do collection properties make this clean?

level: seniorimportance: nice to knowfreq 22%

basics

~20 s

Expose a ListProperty/MapProperty on the extension, give consumers add/put helper methods, set sensible conventions, and wire the task input to the extension property. Each build block appends lazily; the task resolves the aggregate at execution.

open as a page

How do you obtain ProjectLayout inside a plugin or non-Project class without referencing project, and what convention defaults would you set for output locations?

level: seniorimportance: nice to knowfreq 22%

basics

~10 s

Inject ProjectLayout via @Inject in a task or plugin-instantiated class instead of calling project.layout. Then set conventions like outputFile.convention(layout.buildDirectory.file("...")) so outputs default under build/ but stay overridable.

open as a page

showing 31–38 of 38