Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving the API Key in gradle.properties

Tags:

android

api

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"

And then in build.gradle(module:app) I am adding the following lines :

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

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()); 

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 ?

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

like image 979
arqam Avatar asked Mar 01 '16 12:03

arqam


People also ask

How to add an API key to a Gradle project?

One is to saving the api key in gradle.properties. Before add secret to this file. you need add gradle.properties to .gitignore file. If you already commited this file to your git. remove gradle.properties from git first by this command: Modify build.gradle and add buildConfigField for your api key

How to set properties in Android Gradle?

For instance, in android plugin you can set properties in ~/.gradle/gradle.properties file: And in project itself you use BuildConfig.API_KEY string constant.

How do I use Google Maps in Gradle?

To use Google Maps, the developer should have an associated API Key. Let’s see how we can achieve this by using “local.properties” file. Now, let’s extract this value and use it in our app-level Gradle file through a variable.

What is the use of Gradle buildconfig?

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.


1 Answers

  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.
like image 185
Egor Avatar answered Sep 26 '22 03:09

Egor