简体   繁体   English

如何从 build.gradle 配置变量?

[英]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.假设我有一个适用于公司 A(名为“Apple”)的应用程序,它也适用于公司 B(名为“Microsoft”),我希望他们拥有相同的应用程序,只是公司名称写在主页。

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.我在这里需要的是在我的 build.gradle 中有 2 个不同的构建,其中写有公司名称,我可以在 OnCreate 上使用与在 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.您可以在 gradle 文件中创建 productFlavors 并定义不同的源集。 You will get detailed help in the official page.您将在官方页面中获得详细的帮助。 https://developer.android.com/studio/build/build-variants#workBuildVariants 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.如果它只是一个字符串的问题,并且上面有很多工作,那么看看https://www.tanelikorri.com/tutorial/android/set-variables-in-build-gradle/这是一篇不错的文章。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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