简体   繁体   中英

How to configure a variable from build.gradle?

know might be pretty simple, but i can't figure out which could be the best approach here.

Assuming i have an app which works for company A ( named "Apple") and which is perfect also for company B (named "Microsoft"), and i want them to have the same app, just with the company name written on the main page.

What I need here is to have 2 different builds in my build.gradle in which, having the company name written there, i can value on the OnCreate a parameter with the same value specified in the build.gradle.

Which is the best approach here?

EDIT:

I tried putting this line in the release buildtypes:

buildConfigField "String", "company", "\"APPLE\""

but i can't retrieve value using

BuildConfig.company

which doesn't exists

You can create productFlavors in gradle file and define different source sets. You will get detailed help in the official page. https://developer.android.com/studio/build/build-variants#workBuildVariants

If it is just a matter of one string, and above is a lot of work then look at https://www.tanelikorri.com/tutorial/android/set-variables-in-build-gradle/ It's a nice article. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM