Skip to content

Instantly share code, notes, and snippets.

@curioustechizen
Created April 6, 2015 07:37
Show Gist options
  • Save curioustechizen/9f7d745f9f5f51355bd6 to your computer and use it in GitHub Desktop.
Save curioustechizen/9f7d745f9f5f51355bd6 to your computer and use it in GitHub Desktop.
Android: Loading API Keys and other secrets from properties file using gradle
buildTypes {
applicationVariants.all { variant ->
variant.buildConfigField "String", "API_KEY", "\""+getApiKey()+"\""
}
}
def getApiKey(){
def Properties props = new Properties()
props.load(new FileInputStream(new File('secrets.properties')))
return props['API_KEY']
}
API_KEY=my_awesome_api_key
String apiKey = BuildConfig.API_KEY
@lnfn
Copy link

lnfn commented Sep 10, 2017

use Firebase remote config

@curioustechizen
Copy link
Author

@yakubpashask The difference is using the above method your key does not end up in a public github repo :)

@ckdevrel
Copy link

ckdevrel commented Feb 7, 2018

How to access API_KEY in XML. I am struck, please help me.

@curioustechizen
Copy link
Author

@TakeoffAndroid you can use resValue instead of buildConfigField - this will generate a string resource. You can then use it in XML. See here for example usage.

@rituapplocum
Copy link

Map API key and fabric API key that we have to pass in Android Manifest at the time of reverse engineering we are unable to hide these api keys even though we declare them in buid gradle.
.

@curioustechizen
Copy link
Author

Map API key and fabric API key that we have to pass in Android Manifest at the time of reverse engineering we are unable to hide these api keys even though we declare them in buid gradle.
.

Correct. See this comment

@rituapplocum
Copy link

Map API key and fabric API key that we have to pass in Android Manifest at the time of reverse engineering we are unable to hide these api keys even though we declare them in buid gradle.
.

Correct. See this comment

What is the solution for that?

@curioustechizen
Copy link
Author

@ritualapplocum Did you read the whole conversation? Your question is answered in the comments above.

@rituapplocum
Copy link

@ritualapplocum Did you read the whole conversation? Your question is answered in the comments above.

yes I have already implemented it in build gradle and using those values in manifest but again when we decompile the code manifest is still displaying those api keys

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