Reputation: 14594
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?
Upvotes: 10
Views: 12627
Reputation: 5006
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}"
...
}
Upvotes: 3
Reputation: 12684
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')
props.load(propsFile.newDataInputStream())
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.
UPDATE
If order to do this using the ExtraPropertiesExtension
, in your <project base>/build.gradle
add:
allprojects {
repositories {
jcenter()
}
//THIS IS WHAT YOU ARE ADDING
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)
Upvotes: 12
Reputation: 8747
For Android Studio users
You can define the constants in file "gradle.properties" and use them in module's gradle file.
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
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'
}
}
}
Upvotes: 5