How to use ext in gradle in Android

Issue #338

Gradle uses Groovy and it has ext, also known as ExtraPropertiesExtension

Additional, ad-hoc, properties for Gradle domain objects.

Extra properties extensions allow new properties to be added to existing domain objects. They act like maps, allowing the storage of arbitrary key/value pairs. All ExtensionAware Gradle domain objects intrinsically have an extension named “ext” of this type.

1
2
3
4
5
6
7
8
9
project.ext {
myprop = "a"
}
assert project.myprop == "a"
assert project.ext.myprop == "a"

project.myprop = "b"
assert project.myprop == "b"
assert project.ext.myprop == "b"

In root build.gradle, ext adds extra property to rootProject object. There we can access rootProject.ext or just ext

1
2
3
ext {
myLibraryVersion = '1.0.0'
}

In module app/build.gradle, ext adds extra property to project object. There we can access project.ext or just ext

1
2
3
ext {
myLibraryVersion = '1.0.0'
}

Comments