This recipe shows how you can add a source folder to the set of source folders for a specific type of
android source files. In this example, we add a source folder to Android's assets
. The source folder
content is provided by a Task and it is therefore dynamic.
In the Variant API, the source folders are accessible through the Component.sources
method. This sources
will provide access to all the variant's source folders for each source type.
Therefore, other types of android source files can be extended in a similar way like java, kotlin, java resources,
android resources, shaders, etc... See Component.sources
for the complete list.
There are two types of SourceDirectories
:
Flat
and Layered
.
In this recipe, we use assets
, which are of type Layered
, meaning the directories are stored in type
Provider<List<Collection<Directory>>>
.
We use SourceDirectories.addGeneratedSourceDirectory
to add a new folder for assets
processing. You can extrapolate
the same mechanism for other types of source files.
Module | Content |
---|---|
build-logic | Contains the Project plugin that is the core of the recipe. |
app | An Android application that will be configured with the added source folder. |
When you need to generate source files dynamically (based on other source files for instance), you should do so
in a Task with proper Input and Output declarations so you get correct up-to-date checks and caching.
You will find AssetCreatorTask
as an example of such a task in the
CustomPlugin.kt.
Once the TaskProvider
is created, you need to use SourceDirectories.addGeneratedSourceDirectory
to register its
output as a new source folder.
variant.sources.assets?.addGeneratedSourceDirectory(
assetCreationTask,
AssetCreatorTask::outputDirectory)
To run the examples, you can just do:
./gradlew :app:verifyDebugAsset