I've set up some custom build tasks with gradle(Android Project), now those tasks work fine. Example of this task:
TEST {
buildConfigField "String", "KEY", "\"..\""
buildConfigField "String", "CLIENT_ID", "\"\""
buildConfigField "String", "PROTOCOL_VERSION", "\"5.0.0\""
buildConfigField "String", "BACKEND_ENVIRONMENT", "\"..\""
buildConfigField "String", "BACKEND_COUNTRY", "\"..\""
debuggable true
jniDebuggable true
signingConfig signingConfigs.debug
}
Now the problem is, that when I run the predefined task connectedAndroidTest it complains about missing variables in the code, which are the BuildConfigFields that aren't recognised. So my question how to work around this problem?
Thanks,
Define variables for debug buildType (just for debug builds)):
buildTypes {
debug {
buildConfigField "String", "KEY", "\"..\""
buildConfigField "String", "CLIENT_ID", "\"\""
buildConfigField "String", "PROTOCOL_VERSION", "\"5.0.0\""
buildConfigField "String", "BACKEND_ENVIRONMENT", "\"..\""
buildConfigField "String", "BACKEND_COUNTRY", "\"..\""
}
}
or in defaultConfig (for all builds):
defaultConfig {
buildConfigField "String", "KEY", "\"..\""
buildConfigField "String", "CLIENT_ID", "\"\""
buildConfigField "String", "PROTOCOL_VERSION", "\"5.0.0\""
buildConfigField "String", "BACKEND_ENVIRONMENT", "\"..\""
buildConfigField "String", "BACKEND_COUNTRY", "\"..\""
}
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 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 need to enable Fabric for different build environments and also take into account whether the apk has been built using Android Studio or a CI server (jenkins) in our case.
Approach that I am thinking of using is:
defaultConfig {
applicationId "com.pack.proj"
minSdkVersion 19
targetSdkVersion 23
versionCode 12
ext.buildNumber = System.getenv("BUILD_NUMBER") ?: "dev"
versionName "1.2.$buildNumber"
signingConfig signingConfigs.inspection
}
productFlavors {
dev1 {
resValue 'string', 'app_name', 'App Name'
resValue 'string', 'base_url', 'http://dev1.server.in/'
buildConfigField 'boolean', 'USE_FABRIC', 'true'
}
dev2 {
resValue 'string', 'app_name', 'App Name'
resValue 'string', 'base_url', 'http://dev2.server.in/'
// ********HOW DO I CALL THE TASK 'enableFabric' FROM HERE*********
}
}
task enableFabric{
if(versionName.contains('dev'))
buildConfigField 'boolean', 'USE_FABRIC', 'false'
else
buildConfigField 'boolean', 'USE_FABRIC', 'true'
}
Now my questions are
How do I call the task enableFabric from dev1 and dev2 product
flavors?
And when I run the gradle script I get the error that versionName is unrecognised property in task enableFabric?
also buildConfigField 'boolean', 'USE_FABRIC', 'true' when used in the defaultConfig does not work for me. when I use
if(BuildConfig.USE_FABRIC == true){
// DO something
}
in the Java code.
put the if else directly in dev2. or if you plan to use it more than once turn it into a function instead of task
example:
productFlavors {
dev1 {
resValue 'string', 'app_name', 'App Name'
resValue 'string', 'base_url', 'http://dev1.server.in/'
buildConfigField 'boolean', 'USE_FABRIC', 'true'
}
dev2 {
resValue 'string', 'app_name', 'App Name'
resValue 'string', 'base_url', 'http://dev2.server.in/'
buildConfigField 'boolean', 'USE_FABRIC', useFabric()
}
}
outside of your android block, do something like
def useFabric() {
if (versionName.contains('dev'))
return false;
else return true;
}
I have a product with multiple product flavors like:
buildTypes {
debug {
}
release {
}
}
productFlavors {
flavor1 {
buildConfigField "String" "country" "se"
buildConfigField "String" "language" "sv-SE"
buildConfigField "String" "appName" "Flavor1"
}
flavor2 {
buildConfigField "String" "country" "se"
buildConfigField "String" "language" "sv-SE"
buildConfigField "String" "appName" "Flavor2"
}
flavor3 {
buildConfigField "String" "country" "se"
buildConfigField "String" "language" "sv-SE"
buildConfigField "String" "appName" "Flavor3"
}
flavor4 {
buildConfigField "String" "country" "se"
buildConfigField "String" "language" "sv-SE"
buildConfigField "String" "appName" "Flavor4"
}
flavor5 {
buildConfigField "String" "country" "se"
buildConfigField "String" "language" "no-NO"
buildConfigField "String" "appName" "Flavor5"
}
}
I would prefer a common section with all properties and only override those that are different. Is this possible?
I would also like to put all flavors (and perhaps buildTypes) in it's own file to make it more readable. So whenever you have to change a flavor, you can easily find it in its own file, and not have to scroll over thousands of line which it will be if I have all flavors and buildTypes together with all the rest in the main build file.
Selvin is correct, use the defaultConfig closure - there is no neater way! In the following example, flavors 1, 2 & 5 would set the default country and language to de. Flavors 3 & 4 override this with their own languages.
defaultConfig {
buildConfigField "String", "country", "de"
buildConfigField "String", "language", "de"
}
buildTypes {
debug {
}
release {
}
}
productFlavors {
flavor1 {
buildConfigField "String", "appName", "Flavor1"
}
flavor2 {
buildConfigField "String", "appName", "Flavor2"
}
flavor3 {
buildConfigField "String", "country", "uk"
buildConfigField "String", "language", "en_GB"
buildConfigField "String", "appName", "Flavor3"
}
flavor4 {
buildConfigField "String", "country", "fr"
buildConfigField "String", "language", "fr"
buildConfigField "String", "appName", "Flavor4"
}
flavor5 {
buildConfigField "String", "appName", name.capitalize()
}
}
NOTE
Just an FYI that you can use name.capitalize() to turn the name of any flavour, e.g. flavor5, into the app name of Flavor5 by using the capitalize() method - which will capitalize the first character in the String. However, this MUST go in the flavor, not defaultConfig