I am defining resValue in build.gradle like following
defaultConfig {
................................
................................
resValue "string", "google_api_web_client_id", google_api_web_client_id
}
And the value is resided in gradle.properties file .
What I want is to put a separate value for debug build like, but the variable name should same google_api_web_client_id.
in my gradle.properties file I have put the following
geo_api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
geo_api_key_debug=xxxxxxxxxxxxxxxxxxyyyyyyyyyyyyy
What I want is , when the build type is debug it will automatically take the debug value , in case of release it will take the release value .
I can do it , by defining constant but in that case I have to put those value in build.gradle . Which I don't want .
How can I accomplish this ?
you can put it like this
buildTypes {
debug {
resValue 'string', 'google_api_web_client_id', 'debug_key'
}
release {
resValue 'string', 'google_api_web_client_id', 'release_key'
}
}
You can do this as follow,
buildTypes {
release {
buildConfigField "String", "google_api_web_client_id", "YUOR_CLIEN_ID"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
buildConfigField "String", "google_api_web_client_id", "YUOR_CLIEN_ID"
}
}
I think this help you and other with similar question
in your gradle.properties
MY_GOOGLE_API_KEY = "234235623"
in your build.gradle (app)
release {
if (project.hasProperty('MY_GOOGLE_API_KEY')) {
resValue 'string', MY_GOOGLE_API_KEY, 'release_key'
}
}
Related
I'm trying to set up custom variable for each build type.
My build.gradle file looks like this:
buildTypes {
each {
buildConfigField "string", "SHARED_URL", "https://stackoverflow.com/"
}
debug {
buildConfigField "string", "PRIVATE_URL", "https://debugoverflow.com/"
}
release {
buildConfigField "string", "PRIVATE_URL", "https://releaseoverflow.com/"
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
And I want to have an access to the SHARED_URL in my code by final String sharedUrl = BuildConfig.SHARED_URL; both in the debug and in the release build. So, how can I achieve this goal?
P.S. I'm understand that I can just copy variable SHARED_URL in the both builds, but I don't want to boilerplating.
You can put your common buildConfigField in defaultConfig:
android {
defaultConfig {
buildConfigField "string", "SHARED_URL", "https://stackoverflow.com/"
}
buildTypes {
debug {
buildConfigField "string", "PRIVATE_URL", "https://debugoverflow.com/"
}
release {
buildConfigField "string", "PRIVATE_URL", "https://releaseoverflow.com/"
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
SHARED_URL will now be available for all build variants.
We have a white label application with a handful of flavors for different clients. A new client has come on that wants the ability to publish the app through their own developer account. However, prior to release, we need to test the app through our internal test track and verify that the prod environment works (such as billing).
When we got started with development, we created a new product flavor, "com.business.android.product". Now that we are getting close to release, we need a different package name, "com.example.android.thing". My question is, how can we have two package names for the same flavor (i.e. using the same code in the /product source folder)?
Here is an example of our flavor and build type setup
productFlavors {
prod1 {
applicationId "com.business.android"
buildConfigField 'boolean', 'REPORT_CRASHES', "true"
}
prod2 {
applicationId "com.business.android.product2"
buildConfigField 'boolean', 'REPORT_CRASHES', "true"
}
prod3 {
applicationId "com.business.android.product3"
buildConfigField 'boolean', 'REPORT_CRASHES', "true"
def flavor = "spg"
}
prod4 {
applicationId "com.company.android.product4"
buildConfigField 'boolean', 'REPORT_CRASHES', "true"
}
/* Need a way to have all the code in /prod4 flavor source folder but with
* a very different applicationId - ex. somebusiness.android.product4
*
*/
}
buildTypes {
debug {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.KEY
def buildType = "debug"
def targetEnvironment = "production"
buildConfigField "boolean", "PRODUCTION_ENV", "true"
}
debugTst {
minifyEnabled false
debuggable true
signingConfig signingConfigs.KEY
def buildType = "debug"
buildConfigField "boolean", "PRODUCTION_ENV", "false"
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.KEY
def buildType = "release"
buildConfigField "boolean", "PRODUCTION_ENV", "true"
}
releaseTst {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.KEY
def buildType = "release"
buildConfigField "boolean", "PRODUCTION_ENV", "false"
}
}
This issue can be resolved by performing the follow:
sourceSets {
prod4Ext.java.srcDirs += 'src/prod4/java'
prod4Ext.res.srcDirs += 'src/prod4/res'
}
This will provide the prod4 flavor's source code and layouts to the newly created flavor prod4Ext.
In my build.gradle (Module: app) I specified one buildConfigField and one resValue variable.
buildTypes {
release {
debuggable false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "APP_EXP_DATE", "\"DEC 31 23:59:59 EDT 2018\""
resValue "String", "app_exp_date", "\"DEC 31 23:59:59 EDT 2018\""
}
}
Then I expected them to use in my Java code like this:
BuildConfig.APP_EXP_DATE
R.string.app_exp_date
but unfortunately I am experiencing the following errors:
error: cannot find symbol variable APP_EXP_DATE
error: illegal start of type
How can I make it work to be able to access variables from gradle in my Java code?
Well, you have some options:
Define your strings and values under defaultConfig as follow:
android {
// your code
defaultConfig {
// your code
resValue "string", "<key>", "<value>"
buildConfigField "string", "<key>", "<value>"
// ...
}
// your code
}
You can put your string in both Release and Debug type
buildTypes {
release {
// your code
resValue "string", "<key>", "<value>"
buildConfigField "string", "<key>", "<value>"
// ...
}
debug {
// your code
resValue "string", "<key>", "<value>"
buildConfigField "string", "<key>", "<value>"
// ...
}
}
Hope this help!
How can I make it work to be able to access variables from gradle in
my Java code?
The issue in your codes seems to be using in release buildType which might cause the issue (not resolving it.
But, this is how it should be :
In gradle.properties:
ExpDate="DEC 31 23:59:59 EDT 2018"
In app/Build.gradle (Note that it should be in android block code):
def APP_EXP_DATE = '"' + ExpDate + '"' ?: '"Define Expire Date"'
android.buildTypes.each { type ->
type.buildConfigField 'String', 'APP_EXP_DATE', ExpDate
}
Usage:
BuildConfig.APP_EXP_DATE
As a Toast:
Toast.makeText(activity, BuildConfig.APP_EXP_DATE, Toast.LENGTH_LONG).show()
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
I need to create different app names depending on the product flavour used.
While this was easy by simply setting a string resource, I can no longer do that because when the app is uploaded to hockeyapp the app name is set as '#string/app_name' instead of the value of app_name.
I have made some progress by setting the label in the manifest to be '${applicationName}' and setting the value with
manifestPlaceholders = [ applicationName : appName ];
in the product flavour block so that the value gets set at compile time.
The problem comes when I try to append the build type to the application name. I can't seem to find a way to know what build type is currently being used within the product flavour.
This is a stripped down version of the build for readability
android {
buildVersionName "1.0.0
buildTypes {
release {
... nothing special
}
uat {
signingConfig signingConfigs.debug
buildType = "uat"
applicationIdSuffix = "." + buildType
}
debug {
signingConfig signingConfigs.debug
buildType = "uat"
applicationIdSuffix = "." + buildType
}
}
productFlavors{
flavor1{
def appName = "app name " + buildType;
manifestPlaceholders = [ applicationName : appName ];
applicationId [id]
def clientIteration = [client iteration]
versionName buildVersionName + clientIteration
versionCode [version code]
}
flavor2{
... same as above with different app name
}
flavor3{
... same as above with different app name
}
}
}
This code works fine except the variable 'buildType' is always the last buildtype (in this case debug) which means the app name always has debug on the end.
Probably worth noting that I don't need to have anything appended on the end of the app name for releases.
You can append the values like this
android {
productFlavors {
Foo {
applicationId "com.myexample.foo"
manifestPlaceholders = [ appName:"Foo"]
}
Bar {
applicationId "com.myexample.bar"
manifestPlaceholders = [ appName:"Bar"]
}
}
buildTypes {
release {
manifestPlaceholders = [ appNameSuffix:""]
}
debug {
manifestPlaceholders = [ appNameSuffix:".Debug"]
applicationIdSuffix ".debug"
}
}
}
and in the manifest
<application
android:label="${appName}${appNameSuffix}"
...
</application>
If you want to access different values based on build type you can do it like this
buildTypes {
debug{
buildConfigField "String", "Your_string_key", '"yourkeydebugvalue"'
buildConfigField "String", "SOCKET_URL", '"some text"'
buildConfigField "Boolean", "LOG", 'true'
}
release {
buildConfigField "String", "Your_string_key", '"yourkeyreleasevalue"'
buildConfigField "String", "SOCKET_URL", '"release text"'
buildConfigField "Boolean", "LOG", 'false'
}
}
And to access those values using build variants:
if(!BuildConfig.LOG)
// do something with the boolean value
Or
view.setText(BuildConfig.yourkeyvalue);
I know I'm a bit late for the party but if you want different names based on the flavours, you should have something like this:
productFlavors{
flavour 1 {
applicationId "your_app_id"
resValue "string", "app_name", "Flavour 1 app name"
.......
}
flavour 2 {
applicationId "your_app_id"
resValue "string", "app_name", "Flavour 2 app name"
.......
}
}
and in your AndroidManifest.xml:
android:label="#string/app_name"
Hope this helps.
This link http://inaka.net/blog/2014/12/22/create-separate-production-and-staging-builds-in-android/ may help you out.
If you have two productFlavors (Production and Staging, for instance)
You should create two different resource folders:
project/app/src/production/res/values/strings.xml
<resources>
<string name="root_url">http://production.service.com/api</string>
</resources>
project/app/src/staging/res/values/strings.xml
<resources>
<string name="root_url">http://staging.service.com/api</string>
</resources>
You should add this following code inside the android {}:
productFlavors {
production {
applicationId "com.inaka.app.production"
}
staging {
applicationId "com.inaka.app.staging"
}
}
It's a good idea to have different icons for different productFlavors, just add the icon inside each different resource folder.