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.
Related
I have configured 2 build types like this:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
versionNameSuffix "-prod"
signingConfig signingConfigs.prod
archivesBaseName "$versionName"
}
dev {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
versionNameSuffix "-dev"
signingConfig signingConfigs.dev
archivesBaseName "$versionName"
}
}
The problem is that I get this error: > Could not get unknown property 'versionName'
Why is this not working? The problem occurs on archivesBaseName and seems that $versionName is not recognised
Try this one :
VersionName is flavorDimensions which is define in defaultConfig. Version Name field capture data from defaultConfig.
First of all android give priority of defaultConfig data then it override flavors data if data is same.
flavorDimensions "versionCode"
flavorDimensions "versionName"
flavorDimensions "appId"
productFlavors {
UAT {
dimension "versionCode"
dimension "versionName"
dimension "appId"
buildConfigField "String", "BASE_URL", '"https://www.google.com/staging/app/api/v1/"'
buildConfigField "boolean", "IS_LOCAL_DEV", "false"
buildConfigField "boolean", "IS_UAT", "true"
}
QA {
dimension "versionCode"
dimension "versionName"
dimension "appId"
buildConfigField "String", "BASE_URL", '"https://www.google.com/staging/app/api/v1/"'
buildConfigField "boolean", "IS_LOCAL_DEV", "false"
buildConfigField "boolean", "IS_UAT", "false"
}
}
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'
}
}
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 Gradle file, I've assigned buildTypes and productFlavors in the following way.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
signingConfigs {
release{
//storeFile file("")
//storePassword ""
//keyAlias ""
//keyPassword ""
}
}
flavorDimensions "default"
productFlavors {
dev{
dimension "default"
applicationId "com.xxx.android.dev"
}
qa{
dimension "default"
applicationId "com.xxx.android.qa"
}
staging{
dimension "default"
applicationId "com.xxx.android.staging"
}
prod{
dimension "default"
applicationId "com.xxx.android.prod"
}
}
Now the problem is each flavors has the both build types which is release and debug , But I want to assign debug for build only so we cannot produce a release build when selecting debug variant. Any idea how to do it?
I managed to get it done by adding build targets. i didn't want to change any source codes for this . so no need to go with flavors
buildTypes {
qa {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField 'String', ‘BASE_URL', ‘"url“'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField 'String', ‘BASE_URL', ‘"url“'
}
debug {
buildConfigField 'String', ‘BASE_URL', ‘"url“'
}
}
I have three build types, and I am trying to get the staging build type to run but I am getting the following error:
Error: The apk for your currently selected variant (app-staging-unsigned.apk) is not signed. Please specify a signing configuration for the variant (staging).
Is there a way for me to run staging without signing, as like a second debug?
android {
buildTypes {
debug {
buildConfigField "String", "SERVER", '"dev.gamesmart.com"'
}
staging {
buildConfigField "String", "SERVER", '"staging.gamesmart.com"'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "SERVER", '"gamesmart.com"'
}
}
}
Try:
android {
buildTypes {
debug {
buildConfigField "String", "SERVER", '"dev.gamesmart.com"'
}
staging.initWith(buildTypes.debug)
staging {
buildConfigField "String", "SERVER", '"staging.gamesmart.com"'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "SERVER", '"gamesmart.com"'
}
}
}
This says "have staging start as a clone of debug, then we'll modify from there", so staging should apply the debug signing config.