Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override library buildConfigFields in the application

I'm building an application that uses an Android library as the primary source of code. Then I have multiple apps using that library; these apps will have the same code, but different configuration fields, such as endpoints and so.

To accomplish this, I thought I could have a buildConfigField in my library's build.gradle file, and then override that value in my app's build.gradle by using the same name, like this:

build.gradle in library:

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

build.gradle in app:

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

Any ideas on how to accomplish this?

Thanks!

like image 853
Jorge Avatar asked Feb 26 '16 22:02

Jorge


1 Answers

You can't override build configs unfortunately. You can override resource values specified with resValue. The app can override the library, not the other way around. You just need a context to pull them out, and you can do that at app launch to stick them into a java object that you can share as a singleton or whatever.

I have used this trick a lot in the past to let apps pull in a library and override its defaults. You can read about it here.

like image 67
Doug Stevenson Avatar answered Oct 21 '22 00:10

Doug Stevenson