Arthy Shankari M
Arthy Shankari M

Reputation: 93

Is this the right approach to hide my API key in my android project when using gitignore?

I am trying to learn the basics of git and github along with Android programming so that I can show my work to employers in future.

I have done some projects which requires API keys.I've understood that I have to remove my API key before sharing code publicly in github. I've also learnt that .gitignore can be used in the git command line to ignore files that we do not want to be upload.

I read a lot of stack overflow posts and other Q&A sites to exactly see where I have to store my API KEY so that I can ignore that file using .gitignore. There were many suggestions and I am following one of the suggestions from here to store my API key in gradle.properties like

MY_PRIVATE_API_KEY=12345

and then in build.gradle like

buildTypes {    
            debug{  
             buildConfigField("String", "FORECAST_VERIFICATION_API_KEY", "\"" + MY_PRIVATE_API_KEY +"\"")  
            minifyEnabled false  
            applicationIdSuffix ".debug"  
            }  
        } 

and finally in the code to reference it like

String api_key = BuildConfig.FORECAST_VERIFICATION_API_KEY;

Now I want to make sure if this aproach is correct before uploading my project publicly.

My .gitignore file is like this

.gitignore

I see the .gitignore has .gradle in it. Does this mean the gradle.properties where I have stored my API key will be ignored?I am totally confused.Am I following the right approach?

Upvotes: 1

Views: 840

Answers (1)

pbsugg
pbsugg

Reputation: 26

I'm not familiar with the specific software package you're using, but you can just use git status to see what is being tracked. If the gradle.properties file disappears from the untracked files section you have done it right.

I don't think .gradle will work in the .gitignore if your file is calledgradle.properties. Usually people will use *.example if they want to ignore one whole class of file extension. Just name the full file in your .gitignore, gradle.properties and it should disappear from git tracking.

Upvotes: 1

Related Questions