Android Studio how to get versionNameSuffix in code - android

In my Gradle file I declare the VersionName and VersionCode like
defaultConfig {
minSdkVersion 17
targetSdkVersion 25
versionCode 2
versionName "1.0"
versionNameSuffix ".alpha1"
setProperty("archivesBaseName", "MyApp.$versionName.$versionCode$versionNameSuffix")
signingConfig signingConfigs.config
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
and in my application I can retreive the versionCode and versionName with
applicationInformation.VERSIONNAME = BuildConfig.VERSION_NAME;
applicationInformation.VERSIONCODE = BuildConfig.VERSION_CODE;
But how can I get the versionNameSuffix? There isn't a field in BuildConfig for it.

BuildConfig.VERSION_NAME contains the concatenation of versionName and versionNameSuffix like :
public static final String VERSION_NAME = "0.71.alpha1";
If you need only the versionNameSuffix, you can create a new BuildConfig variable in your build.gradle like this :
buildTypes {
debug {
buildConfigField "String", "VERSION_NAME_SUFFIX", "\"" + defaultConfig.versionNameSuffix + "\""
}
release {
buildConfigField "String", "VERSION_NAME_SUFFIX", "\"" + defaultConfig.versionNameSuffix + "\""
}
}
You can now get VERSION_NAME_SUFFIX from BuildConfig.VERSION_NAME_SUFFIX which features :
public static final String VERSION_NAME_SUFFIX = ".alpha1";

Related

buildConfigField not working after updating gradle to 7.0.3

I updated gradle 4.0.1 to 7.0.3 because I need the support of new gradle.
I let the auto updater run and after it was done, when I run the code I get the following error:
C:\Users\me\Projects\proj\proj\proj\app\build\generated\source\buildConfig\stage\debug\proj\BuildConfig.java:22: error: illegal forward reference
public static final String APPLICATION_LIST_URL = BACKEND_HOST + "/page";
In build.gradle the buildConfigField is declared like this:
defaultConfig {
applicationId "my.app.id"
minSdkVersion 21
versionCode getBuildTimestamp()
versionName "2.0.0"
buildConfigField 'String', 'APPLICATION_LIST_URL', 'BACKEND_HOST + "/page"'
}
I tried Invaldiate cache/restart and I do not know what else I can try.
EDIT
BACKEND_HOST is also defined:
productFlavors {
local {
dimension "type"
targetSdkVersion 30
buildConfigField 'String', 'APK_DOWNLOAD_RESOLVE_URL', 'BACKEND_HOST + "DOES_NOT_EXIST"'
...
}
remote {
dimension "type"
targetSdkVersion 30
applicationIdSuffix ".remote"
buildConfigField 'String', 'APK_DOWNLOAD_RESOLVE_URL', 'BACKEND_HOST + "/remote/download"'
}
def backendRemote= '"https://myUrl"'
android.applicationVariants.all {
variant ->
def appName = "myApp"
def backendHost = backendRemote
variant.resValue "string", "app_name", appName
resValue "string", "app_version", "${appName} ${variant.versionName}"
variant.buildConfigField "String", "AUTH_HOST", backendHost
variant.buildConfigField "String", "BACKEND_HOST", backendHost
}
}
And I was building it with remote flavor
It's unclear how the build tools determine the order of the field declarations in the BuildConfig. What works though is this (note the BuildConfig.BACKEND_HOST instead of just BACKEND_HOST):
buildConfigField 'String', 'BACKEND_HOST', 'my.backend.host.com'
buildConfigField 'String', 'APPLICATION_LIST_URL', 'BuildConfig.BACKEND_HOST + "/page"'
Chapter 8.3.3 of https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html explains what forward references are legal and which ones are illegal.
Here's a minimal code sample showing how the BACKEND_HOST can be defined in each flavor:
defaultConfig {
applicationId "com.example.myapplication"
minSdk 30
targetSdk 31
versionCode 1
versionName "1.0"
buildConfigField 'String', 'APPLICATION_LIST_URL', 'BuildConfig.BACKEND_HOST + "/page"'
}
flavorDimensions "version"
productFlavors {
free {
dimension "version"
buildConfigField "String", "BACKEND_HOST", '"www.free.com"'
}
paid {
dimension "version"
buildConfigField "String", "BACKEND_HOST", '"www.paid.com"'
}
}
This works because more specific BuildConfig fields (flavors) are evaluated first before the less specific ones (defaultConfig).
This code is copied from the OP's question and modified to compile by making the references to BACKEND_HOST static:
defaultConfig {
applicationId "my.app.id"
minSdkVersion 21
versionCode getBuildTimestamp()
versionName "2.0.0"
buildConfigField 'String', 'APPLICATION_LIST_URL', 'BuildConfig.BACKEND_HOST + "/page"'
buildConfigField "String", "BACKEND_HOST", '"www.paid.com"'
}
flavorDimensions "type"
productFlavors {
local {
dimension "type"
targetSdkVersion 30
buildConfigField 'String', 'APK_DOWNLOAD_RESOLVE_URL', 'BuildConfig.BACKEND_HOST + "DOES_NOT_EXIST"'
}
remote {
dimension "type"
targetSdkVersion 30
applicationIdSuffix ".remote"
buildConfigField 'String', 'APK_DOWNLOAD_RESOLVE_URL', 'BuildConfig.BACKEND_HOST + "/remote/download"'
}
}
def backendRemote= '"https://myUrl"'
android.applicationVariants.all {
variant ->
def backendHost = backendRemote
variant.buildConfigField "String", "AUTH_HOST", backendHost
variant.buildConfigField "String", "BACKEND_HOST", backendHost
}

Gradle versionName and versionCode not working after 3.0 update

flavorDimensions "app"
productFlavors {
dev {
resValue "string", "base_url", "http://mydevurl/"
dimension "app"
}
prod {
resValue "string", "base_url", "http://myprodurl/"
dimension "app"
}
}
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def versionName
if (variant.buildType.isDebuggable()) {
versionName = "debug_0.1.1"
}else{
versionName = "0.1.1"
}
flavor.versionName = versionName
flavor.versionCode = 50
}
Above gradle setup was working fine till Gradle 3.0 update. Couldn't find anything related in the website referred. How to manage this dynamic version control based on this new flavorDimension change?
You can dynamically set the versionCode and versionName with the 3.0 Android gradle plugin by using VariantOutput.setVersionCodeOverride() and VariantOutput.setVersionNameOverride().
For your project, this would look something like:
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def versionName
if (variant.buildType.isDebuggable()) {
versionName = "debug_0.1.1"
}else{
versionName = "0.1.1"
}
flavor.versionName = versionName
flavor.versionCode = 50
variant.outputs.all { output ->
output.setVersionNameOverride(versionName)
output.setVersionCodeOverride(50)
}
}
reference: https://issuetracker.google.com/issues/63785806#comment6

Is it possible to get the versionName without the suffix from Android?

Consider app.gradleincludes the following:
defaultConfig {
versionName "2.1.6"
}
debug {
debuggable true
versionNameSuffix "-debug"
}
Is it possible to get the version name without the suffix? When using PackageInfo pInfo = applicationContext.getPackageManager().getPackageInfo(applicationContext.getPackageName(), 0);
pInfo.versionName returns 2.1.6-debug is there a way to get only 2.1.6 without doing some string or regex matching.
Thank you!
I would do it using generated BuildConfig:
Gradle
defaultConfig {
def version = "2.1.6"
versionName version
buildConfigField "String", "VERSION", "\"$version\""
}
Java
String version = BuildConfig.VERSION;
Question is old, but it might help someone - I have looked for something similar recently and used this:
debug{
buildConfigField "String", "APP_VER", "\"" + android.defaultConfig.versionName + "\""
}
OR for all buidTypes
buildTypes.each {
it.buildConfigField "String", "APP_VER", "\"" + android.defaultConfig.versionName + "\""
}
OR for all app variants:
applicationVariants.all { variant ->
variant.buildConfigField "String", "APP_VER", "\"" + android.defaultConfig.versionName + "\""
}

Is there a way to change gradle variable value based on selected productFlavor?

Below is my gradle example:
I want to change STORE_FILE_PATH value dynamically based on selected productFlavors. Currently STORE_FILE_PATH is always overwriting it's value with last defined productFlavor. (In my case it always becomes "/pro.jks")
Help me find solution. Thanks
def STORE_FILE_PATH = "";
android {
productFlavors {
free {
versionCode 1
versionName "1.0.0"
applicationId "com.example.free"
buildConfigField "boolean", "IS_AD_ENABLED", "true"
STORE_FILE_PATH = "/free.jks"
}
pro {
versionCode 1
versionName "1.0.0 pro"
applicationId "com.example.pro"
buildConfigField "boolean", "IS_AD_ENABLED", "false"
STORE_FILE_PATH = "/pro.jks"
}
}
signingConfigs {
signingConfig {
keyAlias 'aa'
keyPassword '123'
storeFile file (STORE_FILE_PATH)
storePassword '123'
}
}
}
You should define multiple signingConfigs and use it in different productFlavors
signingConfigs{
freeKey{}
proKey{}
}
productFlavors {
free {
versionCode 1
versionName "1.0.0"
applicationId "com.example.free"
buildConfigField "boolean", "IS_AD_ENABLED", "true"
signingConfig signingConfigs.freeKey
}
pro {
versionCode 1
versionName "1.0.0 pro"
applicationId "com.example.pro"
buildConfigField "boolean", "IS_AD_ENABLED", "false"
signingConfig signingConfigs.proKey
}
}

Gradle BuildConfigFields Syntax

What could be wrong with this syntax? I am getting Gradle DSL method not found:'buildConfigFields()' error.
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.okason.drawingapp"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
def filesAuthorityValue = applicationId + ".files"
//Now you can use ${filesAuthority} in your manifest
manifestPlaceholders = [filesAuthority: filesAuthorityValue]
//Now you can use BuildConfig.FILES_AUTHORITY in our code
buildConfigFields "String", "FILES_AUTHORITY", "\"${filesAuthorityValue}\""
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
you should be using buildConfigField and not buildConfigFields
Change
//Now you can use BuildConfig.FILES_AUTHORITY in our code
buildConfigFields "String", "FILES_AUTHORITY", "\"${filesAuthorityValue}\""
to
//Now you can use BuildConfig.FILES_AUTHORITY in our code
buildConfigField "String", "FILES_AUTHORITY", "\"${filesAuthorityValue}\""
are you sure that buildConfigFields even exits ?

Categories

Resources