How do I reference Build.VERSION_CODES in my build.gradle file? - android

My build.gradle file contains this line:
targetSdkVersion 26
I would prefer it to be like this:
targetSdkVersion Build.VERSION_CODES.o
Is that possible? It would seem much cleaner/safer, but this syntax doesn't work.

The target SDK version appears in multiple projects [...] It would be nice to define this in one place for all usage instances
So we're talking about defining project-wide accessible build time values.
Put this in your root project build.gradle:
ext {
compileSdkVersion = 26
buildToolsVersion = "26.0.0"
minSdkVersion = 17
targetSdkVersion = 26
supportLibVersion = "26.0.0"
playServicesVersion = "10.2.6"
}
And now you can reference these values in your module build.gradle like so:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
// ...
}
// ...
}
dependencies {
compile "com.android.support:design:$supportLibVersion"
compile "com.google.firebase:firebase-messaging:$playServicesVersion"
// ...
}
// ...
I think you can even omit the rootProject.ext. prefix as long the result name does not conflict with a DSL member name.
In Groovy you can use variables in strings as long as you use double quotes.

Is that possible?
No, sorry. Build is a Java class that exists in the Android framework. It is not available to Gradle.

Related

External reference/variable in build.gradle file

Is it possible to use some external reference or variable in build.gradle files?
I have several build.gradle files in my app source files, including the one for module app, module base, module player, etc. (it depends on the structure of your code and the names of your packages).
Inside of each of these files is the following or similar structure:
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0.001"
}
Is there any way I can code this the way that I don't have to change these values in every file? Is it possible to use some external reference or variable and that way I can edit my versionCode, versionName, etc. just on one place?
In your Project gradle
ext {
minSdkVersion = 14
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '26.0.2'
// App dependencies
supportLibraryVersion = '26.1.0'
mockitoVersion = '1.10.19'
roomVersion = "1.0.0"
}
In your App gradle
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
.
.
}
}
dependencies {
// App's dependencies, including test
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
}
Go to File/Project structure/app/flavors then you can get versionCode, versionName, etc then change them what you want and it effects all of your Files.
Check this Image
Yes it is.
In your project-level Gradle config (the one in the root of your project, outside any module folders), you can define variables under the buildscript block:
ext.thisVersionCode = 1
ext.thisVersionName = "1.0.001"
Then you should be able to reference them from your module-level configs:
defaultConfig {
versionCode = rootProject.ext.thisVersionCode
versionName = rootProject.ext.thisVersionName
}
You can define variables in top level build.gradle file and then reference these variables in each module's build.gradle - that way youll be changing them only once in one file.
to give you an example,this is top level file https://github.com/Ejstn/android-starter/blob/master/build.gradle
and this one is module level file: https://github.com/Ejstn/android-starter/blob/master/app/build.gradle
You can also declare whole dependency as variable like in this google's app: https://github.com/google/santa-tracker-android/blob/master/build.gradle

How to use a variable for android section of build.gradle?

Based on this answer, I realized that we can use Gradle variables (I'm not familiar with Gradle of course, so excuse my terminology) to make some consistencies across many Android projects.
For example, I want to change the android closure configuration from this:
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
}
To this:
android {
compileSdkVersion configurationVariables.sdk
buildToolsVersion configurationVariables.buildToolsVersion
defaultConfig {
minSdkVersion configurationVariables.minSdk
targetSdkVersion configurationVariables.targetSdk
versionCode 1
versionName "1.0"
}
}
However, I get this error:
Error:(5, 0) startup failed: build file
'path_to_android\build.gradle': 5: Statement labels may not be used in
build scripts. In case you tried to configure a property named
'buildToolsVersion', replace ':' with '=' or ' ', otherwise it will
not have the desired effect. # line 5, column 24.
buildToolsVersion: configurationVariables.buildToolsVersion
How can I use variables to centralize my build configuration across projects and modules?
Update: I'm defining my configurationVariables as follow:
ext {
configurationVariables = [
sdk = 27,
buildToolsVersion = "27.0.3",
minSdk = 16,
targetSdk = 27
]
}
I write this in a config.gradle file and use apply from to import it in the build.gradle of the root project to apply it on all subprojects.
your config file structure store value as a varible. Generally this structure is use to store variable.Your config file should be like this
ext {
sdk = 27
buildToolsVersion = "27.0.3"
minSdk = 16
targetSdk = 27
}
and you use this variable as
compileSdkVersion sdk
buildToolsVersion buildToolsVersion
I haven't use array for storing this variable but as you given in another answer link they store array variable with colon(:) and you are directly storing values. I am not sure but try to use colon like this if you want to use an array :
ext {
configurationVariables = [
sdk : 27,
buildToolsVersion : "27.0.0",
minSdk : 16,
targetSdk : 27
]
}

Gradle build - if then else possible

In an open source library of mine, I use something like following:
android {
compileSdkVersion setup.compileSdk
buildToolsVersion setup.buildTools
defaultConfig {
minSdkVersion setup.minSdk
targetSdkVersion setup.targetSdk
}
}
I don't want to force everyone to define those constants, but I want to use them and I use the same constants in all my projects and libraries. So I want to be able to use one code that works for me and for everyone else not defining those variables. I'm looking for something like following:
android {
// pseudo code line
if setup is defined
{
compileSdkVersion setup.compileSdk
buildToolsVersion setup.buildTools
defaultConfig {
minSdkVersion setup.minSdk
targetSdkVersion setup.targetSdk
}
}
// default setup, if the user did not define global constants
else
{
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
}
}
}
Is something like that possible? Any alternative suggestions?
I imagine that your setup variable is stored in project extension
project.ext.setup = setup
This way, it can be accessible from your project and all your subprojects
You can test the existence of setup like this
if (project.hasProperty('setup'))
The idea is to create a default setup var if no one is provided
if (!project.hasProperty('setup')){
project.ext.setup = new Setup()
project.setup.compileSdk = 24
project.setup.buildTools = "24.0.2"
project.setup.minSdk = 16
project.setup.targetSdk = 24
}
android {
compileSdkVersion project.setup.compileSdk
buildToolsVersion project.setup.buildTools
defaultConfig {
minSdkVersion project.setup.minSdk
targetSdkVersion project.setup.targetSdk
}
}
Sure, I've tried this and it built successfully.
android{
...
if (someCondition){
compileSdkVersion 23
buildToolsVersion "23.0.1"
...
}else {
compileSdkVersion 24
buildToolsVersion "24.0.3"
...
}
}

Android Studio: use one version number for all modules

I'm building a complex project with multiple modules that gets build together to create a distributed sdk.
My purpose is to have one variable for version major, minor and revision who I later inject into the buildConfig.
Cant find how to do it.
This is what I tried so far:
project1:
// PROJECT VERSIONS
project.ext {
major = 1
minor = 7
revision = 2
}
project2:
defaultConfig {
minSdkVersion 8
targetSdkVersion 23
versionCode project(':project1').ext.major
versionName "1.0"
}
Thank you!
You can configure these values in the build.gradle file in the root of the project.
Example:
ext {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
minSdkVersion = 15
targetSdkVersion = 23
}
Then in the module/build.gradle you can use:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
}
//...
}
In the beginning of build.gradle of project2 add the following line:
evaluationDependsOn(':project1')
Then the evaluation you wrote will work.

How to inject Android configuration to each subproject with Gradle?

Rather than duplicating the android configuration block in each of the sub projects:
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
I would much rather put this in the top-level/root gradle build file like:
subprojects{
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
However, this doesn't work. :(
Error:
"..Could not find method android() for arguments..."
The solution to this turned out to be:
subprojects{
afterEvaluate {
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
}
As far as I know, this is because to process/use the android{...} at evaluation time means it must be present (ie. explicitly written or included as part of an 'apply plugin') as soon as it hits the subprojects in the root build file. And, more accurately, it means that the top-level project must have it defined (which it likely doesn't because it might not be an 'android' or 'android-library' build itself). However, if we push it off to after the evaluation then it can use what is available within each subproject directly.
This question + solution also assume that all subprojects are some form of android project (in my case true, but not necessarily for others).
The safer answer would be to use:
subprojects{
afterEvaluate {
if(it.hasProperty('android')){
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 14
}
}
}
}
}

Categories

Resources