Skip to content

Instantly share code, notes, and snippets.

@Ahmedbadereldin
Created September 4, 2020 11:48
Show Gist options
  • Save Ahmedbadereldin/92626449166567ecdf307900732feea6 to your computer and use it in GitHub Desktop.
Save Ahmedbadereldin/92626449166567ecdf307900732feea6 to your computer and use it in GitHub Desktop.
Hiding API keys in local.properties

Hiding API keys in local.properties

  1. Add the API key to your local.properties file:
apiKey=<value>
serverKey=<value>
mapKey=<value>
  1. Add to the root level of your app-level build.gradle file:
def localProperties = new Properties()
localProperties.load(new FileInputStream(rootProject.file("local.properties")))
  1. Add to the android { defaultConfig { } } block of your app-level build.gradle file:
android {
    // ...
    
    defaultConfig {
        // ...
        buildConfigField "String", "API_KEY", localProperties['apiKey']
        buildConfigField "String", "SERVER_KEY", localProperties['serverKey']
        buildConfigField "String", "MAP_KEY", localProperties['mapKey']
    }
    
    // ...
    
}
  1. Sync Gradle and build the project. You can now reference the key:
val apiKey = BuildConfig.API_KEY
val serverKey = BuildConfig.SERVER_KEY
val mapKey = BuildConfig.MAP_KEY
String apiKey = BuildConfig.API_KEY
String serverKey = BuildConfig.SERVER_KEY
String mapKey = BuildConfig.MAP_KEY
@rishav2404
Copy link

Please update this file for Kotlin too

@Ahmedbadereldin
Copy link
Author

@rishav2404
Ok, I'll do this soon and tell you.

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