Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

1. Store api keys in a xml file

Put xml file "api_keys.xml" in the directory "res/value/".

api_keys.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="THE_MOVIE_DB_API_TOKEN">XXXXX</string>
</resources>

use api keys in java code

getString(R.string.THE_MOVIE_DB_API_TOKEN);

2.1 Store api keys with help of gradle and the gradle.properties file (Java)

Example_0 Example_1 Example_2

Add the following line to [USER_HOME]/.gradle/gradle.properties

For Windows OS, example for Denis user:

C:\Users\Denis\.gradle

gradle.properties

MyTheMovieDBApiToken="XXXXX"

Add the following code to the build.gradle file

build.gradle

apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...
    }
    buildTypes {
        release {
            ...
        }
        buildTypes.each {
            it.buildConfigField 'String', 'THE_MOVIE_DB_API_TOKEN', MyTheMovieDBApiToken
        }
    }
}

use api keys in java code

BuildConfig.THE_MOVIE_DB_API_TOKEN)

2.2 Store api keys with help of gradle and the gradle.properties file (Java + XML)

gradle.properties

AppKey="XXXX-XXXX"

build.gradle

buildTypes {
//...
    buildTypes.each {
        it.buildConfigField 'String', 'APP_KEY_1', AppKey
        it.resValue 'string', 'APP_KEY_2', AppKey
    }
}

Usage in java code

Log.d("UserActivity", "onCreate, APP_KEY: " + getString(R.string.APP_KEY_2));

BuildConfig.APP_KEY_1

Usage in xml code

<data android:scheme="@string/APP_KEY_2" />

3. Store api keys with help of gradle and the system path variable

Example_0

Add new system PATH variable THE_MOVIE_DB_API_TOKEN="XXXXX":

For Windows OS:

  • open system
  • advanced system settings
  • environment variables
  • add new variables to the user variables

Add the following code to the build.gradle file

build.gradle

apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...
    }
    buildTypes {
        release {
            ...
        }
        buildTypes.each {
            it.buildConfigField 'String', 'THE_MOVIE_DB_API_TOKEN', "$System.env.THE_MOVIE_DB_API_TOKEN"
        }
    }
}

use api keys in java code

BuildConfig.THE_MOVIE_DB_API_TOKEN)
@kicklassl

This comment has been minimized.

Copy link

@kicklassl kicklassl commented Dec 12, 2016

Can "Store api keys in a xml file" prevent decompilation?
Imho, xml files are exposed when someone decompiles APK file.
However, using Gradle file works well! It can also prevent decompilation of APK file. Thanks!

@VDenis

This comment has been minimized.

Copy link
Owner Author

@VDenis VDenis commented Feb 15, 2017

@kicklassl Glad it was helpful.

@upendrashahasticsinc

This comment has been minimized.

Copy link

@upendrashahasticsinc upendrashahasticsinc commented Nov 14, 2017

Can we use java static variable name from .class file to build.gradle ??

@codinginflow

This comment has been minimized.

Copy link

@codinginflow codinginflow commented Aug 8, 2020

Thanks, very helpful

@jamilxt

This comment has been minimized.

Copy link

@jamilxt jamilxt commented Aug 12, 2020

Thank you

@misbagas

This comment has been minimized.

Copy link

@misbagas misbagas commented Aug 19, 2020

I have created that variable inside that build.gralde but when I call that variable is not work

@misbagas

This comment has been minimized.

Copy link

@misbagas misbagas commented Aug 25, 2020

thank you for you answer and its works. 👍 💯 🥇 @VDenis

@misbagas

This comment has been minimized.

Copy link

@misbagas misbagas commented Sep 1, 2020

hey @VDenis

@VDenis

This comment has been minimized.

Copy link
Owner Author

@VDenis VDenis commented Sep 2, 2020

@misbagas hello

@misbagas

This comment has been minimized.

Copy link

@misbagas misbagas commented Sep 3, 2020

@VDenis

This comment has been minimized.

Copy link
Owner Author

@VDenis VDenis commented Sep 13, 2020

@misbagas answer on the stackoverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment