I have the same issue, I'm using Android RN application but this could help to any mobile app (Native or React native), the issues come up with you are hardcoding your API KEY (this case google places API into your source code), in order to avoid it you should remove it and add it as System/environment variable, depend of what OS you using OS MAC or Windows, or you CI/ CD if you are delivery you app through it. I solved with the steps below:
I have to setup my System Environment variables in my local
environment in my case I'm using MacOS, Open terminal and run
export GOOGLE_PLACES_ANDROID_API_KEY=Insert_API_KEY_here
(optional but this is better)or copy it in ~/.bash_profile
Make sure my system/environment variable is there- use in the
terminal "printenv" -this will list those and there should be your
api key
GOOGLE_PLACES_ANDROID_API_KEY
Add android/gradle.properties the following line
# GOOGLE PLACES (we will replace this value DON'T COPY YOUR API KEY HERE)
GOOGLE_PLACES_ANDROID_API_KEY=HiHackerNoMyKey
- Add the following lines in app/build.gradle inside defaultConfig
defaultConfig {
buildConfigField("String", "GOOGLE_PLACES_ANDROID_API_KEY", "\"${GOOGLE_PLACES_ANDROID_API_KEY}\"")
}
in my case I'm using Appcenter as CI so I added this line if we are building in CI and my CI should have my APIKEY in my system/environment variable already setup
[buildConfigField("String", "GOOGLE_PLACES_ANDROID_API_KEY", "\"${System.env.GOOGLE_PLACES_ANDROID_API_KEY}\"")](url)
and you can call it using java
Log.i(TAG, BuildConfig.GOOGLE_PLACES_ANDROID_API_KEY);
more reference
https://developer.android.com/studio/build/gradle-tips#simplify-app-development
I hope this helps!!