Use API codes in Android projects

10 Mai 2016
by Artjom König

Sometimes you have to use secret keys in your code, e.g. an API key. If it is your private repository, you can just check your key in e.g as a string resource. But if you don’t want expose your secrets, here is one possible solution:

Define a property in your file (do not check in this file, include it in your .gitignore). Now you can use this property in your build.gradle.


This expression will add your API key as a string resource named api_key to every build type of your project. This way you can even use distinct keys for debug and release builds.

You should also provide a hint for other developers, that they have to define this property, because otherwise the build will fail without a useful error message. Just include the following lines in your build.gradle:

Appcube © 2017