Hiding Api Keys in Android App.

To avoid not having your API-KEY in your code, one of the ways is to have API-KEY in gradle.properties of your home directory.

Create gradle.properties file, if you dont find one. This Key resides in your local machine.

gradle.properties (Global Properties)



build.gradle(Module: app)

apply plugin: 'com.android.application'

def SERVERNAMEDB_API_KEY = <API-Key-Name> ?: "Enter API-KEY in gradle.properties"

android {
    compileSdkVersion xx

    defaultConfig {

       each { type ->
          type.buildConfigField 'String', "ApiKey", SERVERNAMEDB_API_KEY

          type.resValue 'string', "api_key", SERVERNAMEDB_API_KEY

    buildTypes {

“buildConfigField” field will configure the variable to be accessable.

With the above changes,

If you need to access api-key in Java use BuildConfig

Example: String api_key = BuildConfig.ApiKey

If you need to access api-key in any Resources use @string/api_key

Example: = "@string/api_key"

Below are the few References which discuss various ways:

You might also enjoy