How to override BuildConfig from Android-Library? - android

I have a Android Library and I am using it in app module. In library I use a BuildConfig.SPECIAL_VALUE.
I would like to override this BuildConfig in my app module build gradle.
I found similary question but It doesn't help me (Access buildConfigField from gradle in Android app)
I try this but is not working:
My app module build.gradle:
android {
publishNonDefault true
compileSdkVersion 28
flavorDimensions "default"
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "app....id"
flavorDimensions "default"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
multiDexEnabled true
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField 'boolean', 'DEBUG_LOG_ENABLED', 'false'
signingConfig signingConfigs.productivity_mentor_release_config
debuggable = false
manifestPlaceholders = [crashlyticsEnabled: true]
}
debug {
testCoverageEnabled = false
buildConfigField 'boolean', 'DEBUG_LOG_ENABLED', 'true'
debuggable = true
manifestPlaceholders = [crashlyticsEnabled: false]
}
unitTest {
testCoverageEnabled = false
buildConfigField 'boolean', 'DEBUG_LOG_ENABLED', 'false'
manifestPlaceholders = [crashlyticsEnabled: false]
}
}
productFlavors {
develop {
buildConfigField 'boolean', 'LOG_EVENTS_TO_REMOTE_ENABLED', 'false'
buildConfigField 'boolean', 'FIREBASE_CRASH_ENABLED', 'false'
buildConfigField 'String', 'SPECIAL_VALUE', '"11"'
}
production {
buildConfigField 'boolean', 'LOG_EVENTS_TO_REMOTE_ENABLED', 'true'
buildConfigField 'boolean', 'FIREBASE_CRASH_ENABLED', 'true'
buildConfigField 'String', 'SPECIAL_VALUE', '"22"'
}
beta {
buildConfigField 'boolean', 'LOG_EVENTS_TO_REMOTE_ENABLED', 'true'
buildConfigField 'boolean', 'FIREBASE_CRASH_ENABLED', 'true'
buildConfigField 'String', 'SPECIAL_VALUE', '"33"'
}
roboTest {
minSdkVersion 18
buildConfigField 'boolean', 'LOG_EVENTS_TO_REMOTE_ENABLED', 'false'
buildConfigField 'boolean', 'FIREBASE_CRASH_ENABLED', 'true'
buildConfigField 'String', 'SPECIAL_VALUE', '"44"'
}
}
testOptions {
unitTests.returnDefaultValues = true
}
lintOptions {
abortOnError false
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
And my library build.gradle:
android {
compileSdkVersion 28
flavorDimensions "default"
buildToolsVersion '28.0.3'
publishNonDefault true
defaultConfig {
minSdkVersion 22
targetSdkVersion 28
multiDexEnabled true
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":
"$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {}
debug {}
unitTest { }
}
productFlavors {
develop {
buildConfigField 'boolean', 'LOG_EVENTS_TO_REMOTE_ENABLED', 'false'
buildConfigField 'boolean', 'FIREBASE_CRASH_ENABLED', 'false'
buildConfigField 'String', 'SPECIAL_VALUE', '"1"'
}
production {
buildConfigField 'boolean', 'LOG_EVENTS_TO_REMOTE_ENABLED', 'true'
buildConfigField 'boolean', 'FIREBASE_CRASH_ENABLED', 'true'
buildConfigField 'String', 'SPECIAL_VALUE', '"2"'
}
beta {
buildConfigField 'boolean', 'LOG_EVENTS_TO_REMOTE_ENABLED', 'true'
buildConfigField 'boolean', 'FIREBASE_CRASH_ENABLED', 'true'
buildConfigField 'String', 'SPECIAL_VALUE', '"3"'
}
roboTest {
minSdkVersion 18
buildConfigField 'boolean', 'LOG_EVENTS_TO_REMOTE_ENABLED', 'false'
buildConfigField 'boolean', 'FIREBASE_CRASH_ENABLED', 'true'
buildConfigField 'String', 'SPECIAL_VALUE', '"4"'
}
}
testOptions {
unitTests.returnDefaultValues = true
}
lintOptions {
abortOnError false
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
ext {
google_services_version = '16.0.7'
}

Related

How to use BuildConfig value from the other modules?

I have to value in the app module BuildConfig, but I want to use this value from the other module. how can I do it?
1-This is app Gradle:
applicationVariants.all { variant ->
def privacyUrl = "https://www.google.com/privacy-policy"
def termsUrl = "https://www.gogle.com/terms-of-service/"
variant.buildConfigField "String", "URL_PRIVACY_POLICY", "\"${privacyUrl}\""
variant.buildConfigField "String", "URL_TERMS_OF_USE", "\"${termsUrl}\""
if (variant.name.startsWith("development")) {
def domain = "https://api.dev.google.com/"
def countryCode = "+63"
variant.buildConfigField "String", "BASE_URL", "\"${domain}\""
variant.buildConfigField "String", "COUNTRY_CODE", "\"${countryCode}\""
} else if (variant.name.startsWith("staging")) {
def domain = "https://api.stage.google.com/"
def countryCode = "+0000"
variant.buildConfigField "String", "COUNTRY_CODE", "\"${countryCode}\""
variant.buildConfigField "String", "BASE_URL", "\"${domain}\""
}
//production
else {
def domain = "https://api.google.com/"
def countryCode = "+000"
variant.buildConfigField "String", "BASE_URL", "\"${domain}\""
variant.buildConfigField "String", "COUNTRY_CODE", "\"${countryCode}\""
}
}
2-This is profile module gradle:
android {
compileSdk Versions.compile_sdk_version
defaultConfig {
minSdk Versions.min_sdk_version
targetSdk Versions.target_sdk_version
versionCode Versions.version_code
versionName Versions.version_name
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
enableCompose(this)
}
I want to use privacyUrl and termsUrl in the profile module but I don't have access to the app module BuldConfig

How to get versionName inside buildTypes gradle Android

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

How can I create a unique variable for each build in Android Studio?

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

How to call a gradle task from productflavor

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

What is correct way to define different keys for different mode like release and debug

buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildTypes.each {
it.buildConfigField 'String', 'GOOGLE_ANALYTIC_KEY', GoogleTracker_KEY_RELEASE
it.buildConfigField 'String', 'PARSE_APPLICATION_ID', PARSE_RELEASE_APPLICATION_ID
it.buildConfigField 'String', 'PARSE_CLIENT_ID', PARSE_RELEASE_CLIENT_ID
it.buildConfigField 'Boolean', 'BRANCH_IO_MODE_IS_LIVE', BRANCH_IO_MODE_LIVE
}
}
debug {
versionNameSuffix "-DEBUG"
applicationIdSuffix ".debug"
buildTypes.each {
it.buildConfigField 'String', 'GOOGLE_ANALYTIC_KEY', GoogleTracker_KEY_DEBUG
it.buildConfigField 'String', 'PARSE_APPLICATION_ID', PARSE_DEBUG_APPLICATION_ID
it.buildConfigField 'String', 'PARSE_CLIENT_ID', PARSE_DEBUG_CLIENT_ID
it.buildConfigField 'Boolean', 'BRANCH_IO_MODE_IS_LIVE', BRANCH_IO_MODE_TEST
}
}
}
But it is not working and always generating debug keys even in release mode.
(All variable value i am providing from gradle.properties)
It is mistake of buildTypes.each It is getting executed every-time and creating only one key for both debug of release(Either Debug key or Release key based on sequence written in build.gradle I think)
This is how I resolved this
Created two different type of build buildTypes.debug and buildTypes.release
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
/*While app is in debug mode disable crashlytics*/
debug {
versionNameSuffix "-DEBUG"
applicationIdSuffix ".debug"
}
}
buildTypes.debug {
it.buildConfigField 'String', 'GOOGLE_ANALYTIC_KEY', GoogleTracker_KEY_DEBUG
it.buildConfigField 'String', 'PARSE_APPLICATION_ID', PARSE_DEBUG_APPLICATION_ID
it.buildConfigField 'String', 'PARSE_CLIENT_ID', PARSE_DEBUG_CLIENT_ID
it.buildConfigField 'Boolean', 'BRANCH_IO_MODE_IS_LIVE', BRANCH_IO_MODE_TEST
}
buildTypes.release {
it.buildConfigField 'String', 'GOOGLE_ANALYTIC_KEY', GoogleTracker_KEY_RELEASE
it.buildConfigField 'String', 'PARSE_APPLICATION_ID', PARSE_RELEASE_APPLICATION_ID
it.buildConfigField 'String', 'PARSE_CLIENT_ID', PARSE_RELEASE_CLIENT_ID
it.buildConfigField 'Boolean', 'BRANCH_IO_MODE_IS_LIVE', BRANCH_IO_MODE_LIVE
}

Categories

Resources