Override buildConfigFields library in application

I am creating an application that uses the Android library as the main source of code. Then I have several applications using this library; these applications will have the same code, but different configuration fields, such as endpoints, etc.

To achieve this, I thought I could have buildConfigField in my build.gradle library and then override this value in my build.gradle application with the same name, for example:

build.gradle in the library:

 buildConfigField "String", "API_BASE_URL", "\"http://arandomapibaseurl.com\"" 

build.gradle in the application:

 buildConfigField "String", "API_BASE_URL", "\"http://myrealapi.com\"" 

Any ideas on how to do this?

Thanks!

+6
source share
1 answer

Unfortunately, you cannot override configuration configurations. You can override the resource values ​​specified in resValue . An application can redefine a library, and not vice versa. You just need a context to pull them out, and you can do this when you start the application to bind them to a java object, which you can use as a singleton or whatever.

I have used this trick in the past so that applications can enter the library and override its default values. You can read about it here .

+7
source

All Articles