将API密钥保存在gradle.properties中 [英] Saving the API Key in gradle.properties

查看:204
本文介绍了将API密钥保存在gradle.properties中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,正在从事一个项目,在该项目中,我看到的API密钥保存在gradle.properties中为:

I am new to android and working on a project where I see that the API key that I got is saved in gradle.properties as :

MyOpenWeatherMapApiKey="1c3ae96f93a0094e8a7chsjdgfid04aed3f10"

然后在build.gradle(module:app)中添加以下行:

buildTypes.each {
            it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
      }

所以,在我的主程序中,我正在使用此api来访问数据,该api的URL是通过这段代码获得的:

So, in my main program I am accessing the data using this api whose URL is got by this piece of code :

final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?";
            final String QUERY_PARAM = "q";
            final String FORMAT_PARAM = "mode";
            final String UNITS_PARAM = "units";
            final String DAYS_PARAM = "cnt";
            final String APPID_PARAM = "APPID";
            Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
                    .appendQueryParameter(QUERY_PARAM, params[0])
                    .appendQueryParameter(FORMAT_PARAM, format)
                    .appendQueryParameter(UNITS_PARAM, units)
                    .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
                    .appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY)
                    .build();
            URL url = new URL(builtUri.toString());

所以,我的问题是,为什么要承担所有更改的紧张工作,以将appid存储在gradle部分中.我们不能仅在主程序中直接访问吗?

So, my question is that why taking all the tension of doing changes to store the appid in the gradle part. Can't we access directly in the main program only ?

我的问题的第二部分是gradle部分实际上发生了什么,尤其是buildTypes.each{}块?

And the second part of my question is what is actually happening in the gradle part especially with the buildTypes.each{} block ?

推荐答案

  1. 这种间接的想法是允许您将API密钥存储在未上载到版本控制的文件中:gradle.properties是本地文件,不应存储在版本控制下,而BuildConfig是生成的类,因此它将仅在构建时创建.将API密钥作为纯字符串存储在某个地方肯定更容易,但是您必须将其提交到存储库中.
  2. 在构建期间,Gradle将生成BuildConfig文件来存储一些与构建相关的常量.您可以使用buildConfigField命令指示Gradle将自定义字段添加到BuildConfig中.构建项目后,您可以在源代码中引用这些常量.
  1. The idea of this indirection is to allow you to store API key in files that are not uploaded to the version control: gradle.properties is a local file and should not be stored under the version control, and BuildConfig is a generated class, so it will only be created at build time. It's definitely easier to store the API key somewhere as a plain String, but then you'll have to commit it to the repo.
  2. During build time, Gradle will generate the BuildConfig file to store some build-related constants. You can use buildConfigField command to instruct Gradle to add custom fields into BuildConfig. After the project is built, you can reference these constants inside your source code.

这篇关于将API密钥保存在gradle.properties中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆