versionNameSuffix does not work in Android studio 4.1.0 - android

As you know, gradle stopped (official documentation) support VERSION_CODE and VERSION_NAME in the Gradle DSL. For more info click hire. So, I added this code (app build.gradle):
defaultConfig {
versionCode 1
versionName "1.0"
buildConfigField 'int', 'VERSION_CODE', "1" //fix issue
buildConfigField 'String', 'VERSION_NAME', "\"1.0\"" //fix issue
}
//..
buildTypes {
debugToast{
versionNameSuffix "-debug(Toasts)" //NOW DOES NOT WORK !!
}
}
//..
And my question: How fix versionNameSuffix method, because it doesn't work?

Related

Changes in Android BuildConfig.VERSION_NAME

Today I noticed that my debug build broke because the BuildConfig.VERSION_NAME now returns
1.6-debug
instead of
1.6
which is how it used to work before. Does anyone know if there's any documentation changes around this change?
If you're building the debug variant, it now appends -debug when you call BuildConfig.VERSION_NAME which is not the case before.
Regardless of whether it's a release or debug build, the value of BuildConfig.VERSION_NAME is always the same.
defaultConfig {
applicationId "com.myapplication.id"
minSdkVersion 21
targetSdkVersion 29
versionCode 106
versionName "1.6"
multiDexEnabled true //important
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
minifyEnabled false
useProguard false
debuggable true
buildConfigField "Boolean", "DEBUG_MODE", "true"
versionNameSuffix "-debug"
}
}
Remove versionNameSuffix "-debug" in your build.gradle file:
buildTypes {
debug {
....
//versionNameSuffix "-debug"
}
}
You can check the doc:
Version name suffix. It is appended to the "base" version name when calculating the final version name for a variant.
In case there are product flavor dimensions specified, the final version name suffix will contain the suffix from the default product flavor, followed by the suffix from product flavor of the first dimension, second dimension and so on.
VERSION_NAME is versionName in your build.gradle. Maybe you have multiple build files for different build variants or just append the build variant to your versionName.

Gradle - Android - add version code number as versionNameSuffix to debug buildTypes

I want to add the version code as a suffix of versionNameSuffix.
Currently, I have:
debug {
versionNameSuffix ".debug"
}
How can I have something like:
debug {
versionNameSuffix ".debug.versionCode"
}
My version code is based on the productFlavors:
productFlavors {
free {
versionCode 123
}
}
#Resolution: I move versionCode out from productFlavors to root level and use the accepted anwser to inject versionCode in versionNameSuffix
Create constant version code in your app build.gradle
def versionCode = 1
Then in your flavours
debug {
versionNameSuffix ".debug.${versionCode}”
}
Use the same version code in your defaultconfig also
defaultConfig {
versionCode ${versionCode}
}

How to install two separate versions of an app from android studio?

I am working with the Firebase chat project from github.
I am trying to make 2 apps, one client-side and one admin-side.
I have two copies of the project with different project names and different app names.
Still, I can't run both on the same device with android studio. Running one requires uninstalling the other.
I understand it might be a pretty basic thing, but any suggestions?
Thanks
what you need to do is change package name on App Build.Gradle, find applicationId and change one of your project package name
hopefully it works
In your app level build.gradle file under android tag define different product flavors for different app.
Please find the below code and do changes according to your project.
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
flavorDimensions "default"
project.archivesBaseName = "Visualogyx";
signingConfigs {
release {
storeFile file(System.getenv('KEYSTOREPATH'))
storePassword System.getenv("VISUALOGYX_KEYSTORE_PASSWORD")
keyAlias System.getenv("VISUALOGYX_KEYALIAS_NAME")
keyPassword System.getenv("VISUALOGYX_KEYALIAS_PASSWORD")
}
}
defaultConfig {
applicationId "com.visualogyx.app"
minSdkVersion 16
targetSdkVersion 26
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
signingConfig signingConfigs.release
ndk {
abiFilters "armeabi-v7a"
}
}
lintOptions {
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
dexOptions {
javaMaxHeapSize "4g"
jumboMode true
}
productFlavors {
dev {
versionCode 778899
versionName "v.1.1.BUILD_NUM"
applicationIdSuffix ".dev"
resValue "string", "app_name", "VisualogyxDEV"
buildConfigField "String", "HOST", System.getenv("VISUALOGYX_HOST_DEV")
}
qa {
versionCode 778899
versionName "v.1.0.BUILD_NUM"
applicationIdSuffix ".qa"
resValue "string", "app_name", "VisualogyxQA"
buildConfigField "String", "HOST", System.getenv("VISUALOGYX_HOST_QA")
}
pro {
versionCode 778899
versionName "v.1.0.BUILD_NUM"
resValue "string", "app_name", "Visualogyx"
buildConfigField "String", "HOST", System.getenv("VISUALOGYX_HOST_PRO")
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "Visualogyx-${variant.baseName}-${variant.versionName}.apk"
}
}
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'META-INF/XXX'
}
}
Hope it works.Thanks.

Using my custom class in build variants

I have created 4 different flavours in build variants in my app.
I have added version and suffix in the configuration in my app's build.gradle.. Below is the code:
productFlavors {
qa {
applicationIdSuffix ".qa"
versionCode 1
versionName "1.0"
//manifestPlaceholders = [application: ".utils.Application"]
}
demo {
applicationIdSuffix ".demo"
versionCode 1
versionName "1.0"
//manifestPlaceholders = [application: ".utils.Application"]
}
dev {
versionCode 1
versionName "1.0"
//manifestPlaceholders = [application: ".utils.Application"]
//signingConfig signingConfigs.release
}
uat {
applicationIdSuffix ".uat"
versionCode 1
versionName "1.0"
//manifestPlaceholders = [application: ".utils.Application"]
}
}
In my app, I use a file APIAddresses.java for selecting the URLs to hit according to the environment.
Is there any way I can use only this file out of all the source code in build.gradle only to configure URLs for build variants?
May be create different copies of this file for each build variant, something similar to this.
productFlavors {
qa {
applicationIdSuffix ".qa"
versionCode 1
versionName "1.0"
APIAddresses_QA
}
demo {
applicationIdSuffix ".demo"
versionCode 1
versionName "1.0"
APIAddresses_DEMO
}
}
Thank you!
Yes you can use one version of your file APIAddresses.java for all your variants.
In your build.gradle, add
flavorDimensions "main"
Put your APIAddresses.java file in the app\src\main directory.
In your code, you can then generate different URLs for each variant using the variable
BuildConfig.FLAVOR
If you don't want to add lot of if-else conditions in your classes, you can add new buildConfigField to each flavor:
productFlavors {
qa {
applicationIdSuffix ".qa"
versionCode 1
versionName "1.0"
buildConfigField "String", "SERVER_URL", '"https://server1.com"'
}
demo {
applicationIdSuffix ".demo"
versionCode 1
versionName "1.0"
buildConfigField "String", "SERVER_URL", '"https://server2.com"'
}
...
}
After that you can call BuildConfig.SERVER_URL on Java side, and according to each flavor, it should return proper URL in my example

How to set different version number in android build types?

I need to set two different android build types i.e. staging and release.
defaultConfig {
applicationId "com.app.testing"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "SERVER_URL", '"http://testing.com"'
}
dev {
applicationIdSuffix ".dev"
buildConfigField "String", "SERVER_URL", '"http://testing.com"'
}
Now I want to add versionName for each build type. How can I do that?
Edit
productFlavors{
release {
versionname = "1.0"
}
dev{
versionname = "1.0"
}
}
You can also use different version names for each build type without using flavors
In your app-module build.gradle:
defaultConfig {
...
versionName ""
}
...
buildTypes {
debug {
...
versionNameSuffix 'debug-version-1'
...
}
release {
...
versionNameSuffix 'version 1'
...
}
}
versionName "" did the trick.
You can use productFlavors like below:
productFlavors
{
test
{
applicationId 'com.example.test'
versionName '1.0.0.test'
versionCode 1
}
product
{
applicationId 'com.example.product'
versionName '1.0.0.product'
versionCode 1
}
}
You can define it under your default config. You can change from build variants. You can combine your build types with flavors.
Good luck.
The version code could be in minor versions to such as the following:
defaultConfig {
applicationId "com.app.testing"
minSdkVersion 19
targetSdkVersion 23
versionCode 3.2.1
versionName "1.0"
}
In the example above 3 denotes a major release which drastically updates the way an app would look like or its functionality. 2 denotes a bug fix/improvement which updates the app in some way or another, while 1 denotes a minor update or fix.
Hope this helps.
I need to set two different android build types i.e. staging and
release.
Defining buildTypes
As in your question above, you have correctly defined your two buildTypes, "release" and "dev".
Now I want to add versionName for each build type. How can I do that?
Iterating through buildTypes
A clean and more programmatic way to define your versionName (or any other defaultConfig parameter for that matter) for different buildTypes, is to iterate through all application variants and perform a specific action for your buildTypes using if statements:
android {
...
defaultConfig {
...
applicationVariants.all { variant ->
if(variant.buildType.name == "release") {
//Release
versionName "1.2.3"
} else if(variant.buildType.name == "dev") {
//Dev
versionName "3.2.1"
}
}
}
}

Categories

Resources