Home » Android » android – How to declare a constant that is visible to all modules' build.gradle file?

android – How to declare a constant that is visible to all modules' build.gradle file?

Posted by: admin June 15, 2020 Leave a comment


I have a project that has multiple Modules – libraries and applications. Everytime a new version of Android comes out, I need to upgrade the targetSdk, compileSdk, buildToolsVersion, etc. for all the modules. A constant could help with this tedious work!

How could I define project-level constant that is visible to all module’s build.gradle?

How to&Answers:

The way I choose to do something similar is to create a properties file and then just read that for all my global variables. You can do this with java syntax:

Properties props = new Properties()
props.load(new FileInputStream("/path/file.properties"))

A more groovy like syntax is that’s what you perfer is:

Properties props = new Properties()
File propsFile = new File('/usr/local/etc/test.properties')

That way, you may duplicate code in all your modules, but at least your problem is solved.

The second option is the use the ExtraPropertiesExtension I’ve personally never used it but according to the response to the question Android gradle build: how to set global variables it seems to do what you want.


If order to do this using the ExtraPropertiesExtension, in your <project base>/build.gradle add:

allprojects {
    repositories {
    project.ext {
        myprop = "HELLO WORLD";
        myversion = 5

Then after a sync, in your build.gradle files for each module you can use this like so:

System.out.println(project.ext.myprop + " " + project.ext.myversion)


For Android Studio users

You can define the constants in file “gradle.properties” and use them in module’s gradle file.



module’s build.gradle file

android {
    compileSdkVersion project.ANDROID_BUILD_COMPILE_SDK_VERSION=21
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId "com.abc.def"
        minSdkVersion project.ANDROID_BUILD_MIN_SDK_VERSION
        targetSdkVersion project.ANDROID_BUILD_TARGET_SDK_VERSION
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


For Android projects, the Android docs recommends using rootProject.ext.
In your top-level build.gradle (taken from the Configure project-wide properties):

buildscript {...}
allprojects {...}

// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
    // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 26
    // You can also use this to specify versions for dependencies. Having consistent
    // versions between modules can avoid behavior conflicts.
    supportLibVersion = "27.1.1"

Then, in your submodules, you reference these variables like so:

android {
  // Use the following syntax to access properties you define at the project level:
  // rootProject.ext.property_name
  compileSdkVersion rootProject.ext.compileSdkVersion
dependencies {
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"