Android Studio: use one version number for all modules - android

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.

Related

use a compatible library with a minSdk of at most 16

Hi I am trying to build detox to test my app. I am facing compatibility issue. My app runs with android versions 21 to 31. I get the below error when I run the command "yarn detox build -c android"
Error:
Task :react-native-midnight:processDebugAndroidTestManifest FAILED [androidx.vectordrawable:vectordrawable-animated:1.0.0]
/Users/user/.gradle/caches/transforms-2/files-2.1/5677f0d1d2fd33816116c626e2dd87f1/vectordrawable-animated-1.0.0/AndroidManifest.xml
Warning:
Package name 'androidx.vectordrawable' used in: androidx.vectordrawable:vectordrawable-animated:1.0.0,
androidx.vectordrawable:vectordrawable:1.0.1.
/Users/user/projects/MyWorkspaceapp/node_modules/react-native-midnight/android/build/intermediates/tmp/manifest/androidTest/debug/manifestMerger10963475594834660155.xml:5:5-74
Error:
uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library [com.facebook.react:react-native:0.65.2]
/Users/user/.gradle/caches/transforms-2/files-2.1/b7e25968130290bea6922f5b0f7f24b0/jetified-react-native-0.65.2/AndroidManifest.xml
as the library might be using APIs not available in 16
Suggestion: use a compatible library with a minSdk of at most 16,
or increase this project's minSdk version to at least 21,
or use tools:overrideLibrary="com.facebook.react" to force usage (may lead to runtime failures)
See http://g.co/androidstudio/manifest-merger for more information
about the manifest merger.
build.gradle:
ext {
compileSdkVersion = 31
targetSdkVersion = 31
minSdkVersion = 21
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
// buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion 21
targetSdkVersion 31
multiDexEnabled true
}
}
AndroidManifest.xml
> <uses-sdk android:minSdkVersion="21"
> android:targetSdkVersion="31"
> android:maxSdkVersion="31"
> tools:overrideLibrary="com.facebook.react" />
Add the below line in build.gradle. It works
subprojects {
ext {
compileSdk = rootProject.ext.compileSdkVersion
minSdk = rootProject.ext.minSdkVersion
targetSdk = rootProject.ext.targetSdkVersion
}
afterEvaluate { subproject ->
if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
}
}
}
}
}

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
]
}

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.

Method for arguments with gradle in android studio

Syncing the project files with gradle files in android studio is resulting in the following error
Error:(14, 0) Could not find method applicationId() for arguments [com.amazon.mysampleapp] on project ':app' of type org.gradle.api.Project.
where the gradle file reads
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
applicationId "com.amazon.mysampleapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled = true
}
What is happening here? Removing applicationID moves the error to minSdkVersion
The applicationId doesn't go inside the android block, but instead your buildConfig block:
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.1"
defaultConfig {
applicationId "com.example.my.app"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
}
Pulled that from this page about applicationId: http://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename

Running different version in Android library versus your app

I have the following in my main app:
...
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 23
versionCode 19
versionName "1.0.5"
}
...
In a library I use, this is defined:
...
android {
compileSdkVersion 17
buildToolsVersion "19.1.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 7
}
...
I have uppgraded my API target etc. in my app. The library I have, use the same API target etc. (haven't changed the code). If I upgrade my app and use libraries, can I leave them as is? Or do I need to sync the two in respect with compileSdkVersion, buildToolsVersion, minSdkVersion and targetSdkVersion?

Categories

Resources