Gradle build - if then else possible - android

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"
...
}
}

Related

buildToolsVersion and compileSdkVersion had been deprecated

I've just noticed in a library module, that compileSdkVersion and buildToolsVersion had recently been deprecated. These are the two configurations, which it complains about (strikethrough text). This currently only affects com.android.library, not com.android.application:
plugins {
id "com.android.library"
}
android {
defaultConfig {
compileSdkVersion 31
buildToolsVersion "31.0.0"
}
}
When clicking through, on may notice that com.android.build.gradle.AndroidConfig had been deprecated in favor of BaseExtension (whatever that may mean for a matching build.gradle):
Apparently compileSdkVersion is now called compileSdk (this part seems to work) ...and buildToolsVersion has to be defined as a property. But when building (it's not that I've not tried), property buildToolsVerson = "31.0.0" is unknown:
android {
defaultConfig {
compileSdk 31
buildToolsVersion = "31.0.0"
}
}
How to make it build without deprecation warnings?
If you are using a kts gradle file instead (build.gradle.kts)
plugins {
id("com.android.library")
}
android {
defaultConfig {
// compileSdkVersion 31
compileSdk = 31
}
}
It builds alike this (the library module needs to be changed):
plugins {
id "com.android.application"
}
android {
defaultConfig {
compileSdkVersion 31
}
}
plugins {
id "com.android.library"
}
android {
defaultConfig {
// compileSdkVersion 31
compileSdk 31
}
}

I've two different react native packages which require different android compileSdkVersion, how do I fix this?

I use react-native-music-control and react-native-navigation(by wix) , rnmc requires compilesdkversion 23 and buildToolsVersion '23.0.1' whereas rnn requires compilesdkversion 25 and buildToolsVersion '25.0.0', rnn does not work with 25.0.0/25 and rnn won't work with 23/23.0.1 . I don't have much experience with android so I'm not sure how to fix this, any guidance would be great.
Add the following to build.gradle in your projects root directory
subprojects {
afterEvaluate {
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
targetSdkVersion 25
}
}
}
}

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

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.

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