I want to make HostName field dynamic. so I can have different HostName for both app1 and app2, for develop and release
**app1 {
dimension "client"
applicationId = "com.app1.random"
}
app2 {
dimension "client"
applicationId = "com.app2.random"
}
develop {
dimension "environment"
buildConfigField "String", "HOSTNAME", "\"https://app1.example.com\""
}
release {
dimension "environment"
buildConfigField "String", "HOSTNAME", "\"https://app1.com\""
}**
Hope this help you
flavorDimensions "default"
productFlavors{
develop {
buildConfigField "String", "HOSTNAME", "\"https://app1.example.com\""
}
release {
buildConfigField "String", "HOSTNAME", "\"https://app1.com\""
}
}
Related
I need to build an application created with a flavor and create a dynamic variable who points to an applicationId of another flavor (Because the code of an internal library uses the applicationId of other applications) but I don't know how to do that.
Here is the sample code :
defaultConfig {
applicationId "com.sample.mycompany"
}
buildTypes {
release {
}
qualif {
applicationIdSuffix = ".qual"
}
debug {
applicationIdSuffix = ".dev"
}
}
flavorDimensions "client", "nature"
productFlavors {
ClientA {
dimension "client"
applicationIdSuffix = ".A"
}
ClientB {
dimension "client"
applicationIdSuffix = ".B"
}
NatureX {
dimension "nature"
applicationIdSuffix = ".X"
}
NatureY {
dimension "nature"
applicationIdSuffix = ".Y"
// A buildconfigField variable here to get com.sample.mycompany[client].X[buildTypes]
}
NatureZ {
dimension "nature"
applicationIdSuffix = ".Z"
// A buildConfigField variable here to get com.sample.mycompany[client].X[buildTypes]
}
}
When I compile with the Build Variant : ClientANatureYDebug
, final applicationId is com.sample.mycompany.A.Y.dev
I want a dynamic variable with buildConfigField (or something else to retrieve the new variable in Java) who is com.sample.mycompany.A.X.dev
I think to get the final applicationId and replace the applicationIdSuffix of the current nature compilation dimension by .X and get the result in a new variable but I do not know how. Can you help me ?
Fixed with :
buildConfigField "String", "VAL_SHARE_TO_RECEIVER_APP_ID", "APPLICATION_ID.replace(\".Y\", \".X\")"
I want to modify the token and API urls from my java code so that I can create a development or release build depending on mode selected from my custom screen.
Below is the code used in build.gradle
productFlavors {
dev {
buildConfigField "String", "TOKEN_URL", '"https://example.com"'
buildConfigField "String", "API_URL", '"https://api.ex"'
versionCode getDevVersionCode()
}
prod {
buildConfigField "String", "TOKEN_URL", '"https://release.example2.com"'
buildConfigField "String", "API_URL", '"https://api.release.ex"'
versionCode getReleaseVersionCode()
}
}
first in app/build.gradle, add the productFlavor under android
productFlavors {
qa {
buildConfigField "boolean", "LOG_HTTP", "true"
}
staging {
buildConfigField "boolean", "LOG_HTTP", "true"
}
live {
buildConfigField "boolean", "LOG_HTTP", "false"
}
}
then you need to create specific directories in app/src directory
finally, you just create a same name File in these different directories, like the following picture
in each ServerConstants(the name can be arbitrate), you should reference specific variables of "TOKEN_URL" and "API_URL"
I want use something like this, i know that's not possible, but i wanna know if have another way to use combine Flavor With build type
productFlavors{
demo{
release {
buildConfigField "String", "URL", '"192.1.1.1"'
}
debug {
buildConfigField "String", "URL", '"192.2.2.2"'
}
}
full{
release {
buildConfigField "String", "URL", '"192.2.2.2"'
}
debug {
buildConfigField "String", "URL", '"192.3.3.3"'
}
}
}
Rather than product flavors you can create multiple build types to suit your requirement.
for eg create build types like below
release {
buildConfigField "String", "URL", '"192.2.2.2"'
}
debug {
buildConfigField "String", "URL", '"192.3.3.3"'
}
releasedemo {
buildConfigField "String", "URL", '"192.1.1.1"'
}
debugdemo {
buildConfigField "String", "URL", '"192.2.2.2"'
}
I have six builds like
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
manifestPlaceholders = [appVersion: ""];
}
staging {
applicationIdSuffix '.staging'
versionNameSuffix '-staging'
manifestPlaceholders = [appVersion: "-stg"];
}
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-debug'
manifestPlaceholders = [appVersion: "-debug"];
}
}
productFlavors {
panel {
versionCode 2
versionName '1.1'
applicationIdSuffix '.panel'
manifestPlaceholders = [appName: "exampleapp-panel"];
}
admin {
manifestPlaceholders = [appName: "exampleapp-admin"];
}
}
I want to change each function or variable for each build.
I can use it this way in XCode, but I have not found a way in Android studio.
Can I make a setting file using in AndroidStudio like this?
You can use something like following for each build/product flavor.
buildConfigField "boolean", "VALUE_KEY", "true"
resValue "string", "my_string", "Some string"
The first can be accessed in your code as BuildConfig.VALUE_KEY, the 2nd is written to strings.xml
try this :
productFlavors {
pro {
minSdkVersion 15
applicationId 'com.myapps'
signingConfig signingConfigs.release
targetSdkVersion 23
resValue "string", "app_name", "myapps"
buildConfigField "boolean", "IS_DEV", "false"
}
dev {
minSdkVersion 15
applicationId 'com.myapps.dev'
targetSdkVersion 23
resValue "string", "app_name", "myapps 1.2"
buildConfigField "boolean", "IS_DEV", "true"
}
}
I have two environment of my project one Prod another one is Staging. So whenever I have to build any of the environment, I have to change multiple keys like map key, label name and other things in manifest. So I have searched and find out some of the solutions and manifestPlaceholders is one of them.
Now what I want to do is to assign multiple value in manifestPlaceholders. So can I put multiple values in it and yes then how to put multiple values in it. Here is the code for the manifestPlaceholders
buildTypes {
debug {
manifestPlaceholders = [ google_map_key:"your_dev_key"]
}
release {
manifestPlaceholders = [ google_map_key:"prod_key"]
}
}
I have solved my problem as below code by adding multiple manifestPlaceholders values. Added this to my module build.gradle.
productFlavors {
staging {
applicationId "xxxxxxxxxxx"
manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
}
prod {
applicationId "xxxxxxxxxxx"
manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
}
}
EDIT:
You can use resValue also as Emanuel Moecklin suggested in comments.
productFlavors {
staging {
applicationId "xxxxxxxxxxx"
manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
resValue "string", "base_url", "xxxxxxxxxx"
}
prod {
applicationId "xxxxxxxxxxx"
manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
resValue "string", "base_url", "xxxxxxxxxx"
}
}
You can easily set/change multiple manifestPlaceholders values. You can either define all values at once, as in your answer, or one by one.
defaultConfig {
// initialize more values
manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
// or this way
manifestPlaceholders.google_map_key = "xxxxxxxxxx"
manifestPlaceholders.app_label_name = "xxxxxxxxxx"
}
productFlavors {
staging {
}
prod {
// use some different value for prod
manifestPlaceholders.google_map_key = "yyyyyyyyyy"
}
}
I have mentioned for both build Types and flavors
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
resValue "string", "google_maps_key", "release google map key"
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
resValue "string", "google_maps_key", "debug google map key"
}
}
productFlavors {
alpha {
applicationId = "com.example.alpha"
resValue 'string', 'app_name', 'alphaapp'
resValue "string", "maps_api_key", "XXXXXXXXXXXXXXXXXXXXX"
}
beta {
applicationId = "com.example.beta"
resValue 'string', 'app_name', 'betaapp'
resValue "string", "maps_api_key", "XXXXXXXXXXXXXXXXXXXXXX"
}
}
I could not use the suggested approaches in other answers because gradle seems to have changed the type of manifestPlaceholders to val mutablemap in a recent release.
This was the only fix that worked for me:
manifestPlaceholders["key"] = "value0"
manifestPlaceholders["key"] = "value1"
Well, we can set manifestPlaceholders key values using the following ways,
select File > Project Strucutre
Add environment
2-1. select Flavors tab
2-2. Add flavor dimension and name for dimension ex."env"
2-3. Add product flavor an name product ex. "prod"
Add manifestPlaceHolder keys
3-1. Select the above product
3-2. Add key in Manifest Placeholders list
press OK
build.gradle file is automatic updated after press the buttton