Using google maps api key in open source GitHub repository in Flutter app

99 Views Asked by At

I am developing an open source Google Maps project by Flutter and I do not want the GMS API to appear in my repo when I integrate with Google Maps. I don't know how I can use the variable in the .env file in my AppDelegate.swift and AndroidManifest.xml files.

My AndroidManifest.XML

<application
    <meta-data android:name="com.google.android.geo.API_KEY"
               android:value="MY_API_KEY"/>
</application>

AppDelegate.swift

import UIKit
import Flutter
import GoogleMaps

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GMSServices.provideAPIKey("MY_API_KEY")
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

I added a .env file on my path and I ignore it on .gitignore. Using this method, I could easily use it in my files of lib folder by flutter_dotenv package.

1

There are 1 best solutions below

0
Munsif Ali On

For android you can try this:

  1. create a values xml file keys.xml add your key in this file like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="google_map_api_key">Your Api Key</string>
</resources>
  1. use it in AndroidManifest.xml like this:
     <meta-data
       android:name="com.google.android.geo.API_KEY"
       android:value="@string/google_map_api_key" />

and add keys.xml to .gitignore file to ignore the tracking of this file in git repository.

add this in .gitignore file

# ignore keys.xml
/android/app/src/main/res/values/keys.xml