Set variables in build.gradle - android

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()

Related

How to define a shared build variable?

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.

Defining resValue conditionally in build.gradle

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

Android Studio - Assigning multiple value to ManifestPlaceholders in Gradle

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

Android app name depends on buildType and flavours Gradle

Below is an extract from my gradle source code. What I want to achieve is to add suffix to app name when buildType.debug is executed. I tried the following code, but variables are in gradle assigned in sequence as they are written in file and not as task order. So in the example below buildVariant variable will always be equal to Release.
{
def buildVariant = ""
buildTypes {
debug {
manifestPlaceholders = [showDebug: 'true']
buildVariant = " (DEBUG)"
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
manifestPlaceholders = [showDebug: 'false']
signingConfig signingConfigs.myConf
buildVariant = " Release"
}
}
productFlavors {
flavour1{
resValue 'string', 'app_name', 'Flavour1'+buildVariant
}
flavour2{
resValue 'string', 'app_name', 'Flavour2'+buildVariant
}
flavour3{
resValue 'string', 'app_name', 'Flavour3'+buildVariant
}
}
It was a really interesting puzzle to solve, so thanks for the question!
Here what you can do:
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
....
}
buildTypes {
debug {
}
release {
}
}
productFlavors {
FlavorA {
}
FlavorB {
}
}
applicationVariants.all { variant ->
variant.resValue "string", "app_name", '"' + variant.productFlavors.get(0).name + '_' + variant.buildType.name + '"'
}
}
dependencies {
}
As a result, it'd print the app name "FlavorA_debug", "FlavorB_release", etc.
(NB! I ran it with gradle classpath 'com.android.tools.build:gradle:1.3.0' - works great, though I didn't try with older versions)

Get gradle build type in product flavor

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.

Categories

Resources