Is there a safe way to manage API keys?

前端 未结 4 1806
死守一世寂寞
死守一世寂寞 2020-12-07 09:18

I am using an API within my app. I currently manage the API key from a java interface

public interface APIContract {
    //The API KEY MUST NOT BE PUBLISH. I         


        
4条回答
  •  一个人的身影
    2020-12-07 09:29

    I put them on the global gradle.properties file on the dev machine or ci server. This way is not part of your project so you shouldn't worry about checking it in your repo, but it's still easily accessible from your gradle files. Global gradle.properties file should be located inside ˜/.gradle on Mac for instance - btw, you should create it if it doesn't exist. Then on your build.gradle file you can reference those properties and, for instance, expose them as buildConfigFields using the same name you used in your gradle.properties file

    buildConfigField 'String', 'API_KEY', maps.api.key
    

    Then on your Java/Kotlin code you can access them as BuildConfig.API_KEY

提交回复
热议问题