How to declare a constant that is visible to the build.gradle file of all modules?

I have a project with several modules - libraries and applications. Every time a new version of Android comes out, I need to update targetSdk, compileSdk, buildToolsVersion, etc. For all modules. Constant can help in this tedious job!

How can I determine the project level constant that is visible for the whole build.gradle module?

+4
source share
2 answers

How can I do something like this, I need to create a properties file and then just read that for all of my global variables. You can do this using java syntax:

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

More groovy syntax is what you do:

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

, , .

- ExtraPropertiesExtension , Android gradle build: , , , .

UPDATE

, ExtraPropertiesExtension, <project base>/build.gradle :

allprojects {
    repositories {
        jcenter()
    }
    //THIS IS WHAT YOU ARE ADDING
    project.ext {
        myprop = "HELLO WORLD";
        myversion = 5
    }
}

build.gradle :

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

Android Studio

"gradle.properties" gradle.

gradle.properties

ANDROID_BUILD_MIN_SDK_VERSION = 16
ANDROID_BUILD_TARGET_SDK_VERSION= 20
ANDROID_BUILD_TOOLS_VERSION=20.0.0
ANDROID_BUILD_SDK_VERSION=20
ANDROID_BUILD_COMPILE_SDK_VERSION=21

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'
        }
    }
}
+3

All Articles