在Flutter应用中从源代码管理中隐藏Google Maps API密钥 [英] Hide Google Maps API key from source control in a Flutter app
问题描述
要启用Google Maps SDK,您必须更新: 适用于Android的 android/app/src/main/AndroidManifest.xml , 和 ios/Runner/AppDelegate.m for iOS with your API key
To enable the Google Maps SDK you must update: android/app/src/main/AndroidManifest.xml for android, and ios/Runner/AppDelegate.m for ios with your API key.
我不想将我的API密钥签入源代码管理中.
I don't want to check in my API key into source control.
是否可以用.env
文件和.gitignore
隐藏该键?
Is there a way to hide this key with a .env
file and .gitignore
?
这里的最佳做法是什么?
What is best practice here?
推荐答案
您可以将api密钥设置为环境变量,这些变量只能在本地计算机上的构建过程中读取.创建一个环境变量MAPS_API_KEY ="your-key-here",然后将这些行添加到 [您的项目]/android/app/build.gradle
You can set your api keys as environment variables which can be read during build on your local machine only. Create an env variable MAPS_API_KEY="your-key-here", then add these lines to [your-project]/android/app/build.gradle
defaultConfig {
manifestPlaceholders = [mapsApiKey: "$System.env.MAPS_API_KEY"]
}
然后您可以使用mapsApiKeys在AndroidManifest.xml中传递您的api密钥
and then you can use the mapsApiKeys to pass your api keys in AndroidManifest.xml
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="${mapsApiKey}"/>
对于ios,将它们添加到AppDelegate.m
For ios, add these to AppDelegate.m
NSString* mapsApiKey = [[NSProcessInfo processInfo] environment[@"MAPS_API_KEY"];
[GMSServices provideAPIKey:mapsApiKey];
这篇关于在Flutter应用中从源代码管理中隐藏Google Maps API密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!