Skip to main content

Build Gradle Task Configuration (build_gradle)

Modify build.gradle files

The build_gradle task is designed to facilitate modifications to the build.gradle files in Android projects. It is commonly used to add dependencies, configure build settings, and perform other customizations within Android app modules. This task provides the flexibility to make changes to different sections of the build.gradle file.

Task Properties

PropertyTypeDescription
task"build_gradle", requiredSpecifies the task type, which should be set to "build_gradle" for this task.
namestringAn optional name for the task. If provided, the task state will be saved as a variable. Visit Task and Action States page to learn more.
labelstringAn optional label or description for the task.
whenobjectVisit Conditional Tasks and Actions page to learn how to execute task conditionally.
location"root" (default) or "app"Specifies the target location within the project structure, distinguishing between the root and app folders. It helps determine which build.gradle file to modify during the configuration process. "root" modifies android/build.gradle file and "app" modifies android/app/build.gradle file
actionsArray<Action>, requiredAn array of action items that define the modifications to be made in the file. Each action item contains the following fields:

Action Properties

Common properties

PropertyTypeDescription
namestringAn optional name for the task. If provided, the task state will be saved as a variable. Visit Task and Action States page to learn more.
whenobjectVisit Conditional Tasks and Actions page to learn how to execute action conditionally.

Context reduction properties

PropertyTypeDescription
blockstringA string specifying the block path within the build.gradle file where modifications should occur. Check Block section below.
beforestring or {regex: string, flags: string}Text or code that is used to specify a point within the context where text should be inserted before. It can be a string or an object with a regex and flags field to perform a regex-based search.
afterstring or {regex: string, flags: string}Text or code that is used to specify a point within the context where text should be inserted after. It can be a string or an object with a regex and flags field to perform a regex-based search.
searchstring or {regex: string, flags: string}A string or object (with regex and flags) that narrows the context to a specific text within the method or file.

Context modification properties

PropertyTypeDescription
prependstring or {file: string}Text or code to prepend at the beginning of the specified context. It can be a string or an object with a file field that points to a file containing the code to prepend.
appendstring or {file: string}Text or code to append at the end of the specified context. It can be a string or an object with a file field that points to a file containing the code to append.
replacestring or {file: string}Text or code to replace the entire specified context. It can be a string or an object with a file field that points to a file containing the code to replace.

Other properties

PropertyTypeDescription
exactbooleanA boolean flag that modifies the whitespace and new line management.
strictbooleanSpecifies the behavior of the before and after fields. If set to true, the task will throw an error if the text in the before or after field is not found in the context, otherwise, it will ignore the field.
ifNotPresentstringIndicates that the task should only be executed if the specified text or code is not present within the specified context.
commentstringAn optional comment to add before the inserted code or text. The comment is purely informational and does not affect the code's functionality.

Block property

The block property in the build_gradle task serves as a block path within the build.gradle file. This path helps define the specific context within the file where modifications should be made. By specifying this block path, you can precisely target the portion of the build.gradle file that requires customization.

Example

task: build_gradle
label: "Enabling multidex"
location: "app",
actions:
- block: "android.defaultConfig"
after: versionName
prepend: multiDexEnabled true

In this example, the build_gradle task enables multidex in defaultConfig block in the build.gradle file. The block property is set to " android.defaultConfig", indicating the modification context.