Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

git commit without API KEYS (using Android Studio)

So I just realized while using git to backup my project (stormy weather app) that my API KEY is in main activity for everyone to see. How do I make this API KEY invisible ot git so when it pushes it will ignore it. (like a gitignore but for a string instead of a file) or whatever the best practice is....

I have to go change my API key now :(

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Git works on the file level so there isn't a way to exclude a part of a file instead of a whole file. There are a couple of ways to do it: 1) Have an ApiKey class and put that into the .gitignore file. 2) Use gradle to inject the value of the API key at build time and specify the key in your ~/.gradle/gradle.properties (C:\Users\Your_User_Name.gradle\gradle.properties on Windows) file (so it will be outside of the project) or one of the already ignored .properties files in your project. http://www.rainbowbreeze.it/environmental-variables-api-key-and-secret-buildconfig-and-android-studio/

This of course only hides the key from source control.