Your Friend Ken
Your Friend Ken

Reputation: 8872

Hide Google Maps API key from source control in a Flutter app

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.

The problem:

I don't want to check in my API key into source control.

Is there a way to hide this key with a .env file and .gitignore?

What is best practice here?

Upvotes: 10

Views: 8596

Answers (5)

timlg07
timlg07

Reputation: 626

@Kushal Billaiya's solution didn't work for me, as it overrides existing manifestPlaceholders. What I did instead was the following:

First, still set your Google Maps API Key as an environment variable named MAPS_API_KEY. I do this via the run configuration, but other ways exist.

Then in the android/app/build.gradle file (Important: There are 2 build.gradle files. You want the one inside the android > app folder), add one line to the defaultConfig:

defaultConfig {
    ...
    manifestPlaceholders["mapsApiKey"] = "$System.env.MAPS_API_KEY"
}

And in the android/app/src/main/AndroidManifest.xml file, you can add this line inside the <application> tag:

<meta-data android:name="com.google.android.geo.API_KEY" android:value="${mapsApiKey}"/>

Upvotes: 1

Bartek Pacia
Bartek Pacia

Reputation: 1695

I've been living with my Google Maps API key hardcoded in my Flutter app for iOS for a long time, but today I decided to solve it once and for all, but even after reading many articles and blogposts I didn't find a solution that is simple enough.

Then I thought that I can just read the API_KEY from GoogleService-Info.plist file in my AppDelegate.swift, like this:

if let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") {
    let nsDictionary = NSDictionary(contentsOfFile: path)
        
    if let apiKey = nsDictionary?["API_KEY"] as? String {
        print("AppDelegate: API_KEY found")
        GMSServices.provideAPIKey(apiKey)
    }
     

Upvotes: 0

tatsuDn
tatsuDn

Reputation: 2329

Starting from Flutter 1.17 you can use compile-time variables for this. Just use --dart-define key in flutter run or flutter build commands. Then you can use them in your iOS or Android code. Here is an article with some explanation and samples

Upvotes: 4

Kushal Billaiya
Kushal Billaiya

Reputation: 531

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"]
    }

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}"/>

For ios, add these to AppDelegate.m

NSString* mapsApiKey = [[NSProcessInfo processInfo] environment[@"MAPS_API_KEY"];

[GMSServices provideAPIKey:mapsApiKey];

Upvotes: 21

Kolineal
Kolineal

Reputation: 465

You can save your keys in a separate file, and add that file to .gitignore. Then if you push your files to the repository, that file will be ignored.

In case you have a new colleague, who needs to start working on the same project, you will need to share this file with them. After checking out the project from the repository, they will need to place that file in the same directory as it was originally.

Upvotes: 1

Related Questions