1

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.

3
  • you want to upload that project to github? Commented Feb 27, 2024 at 10:04
  • @MunsifAli yes, I want to upload this project to GitHub Commented Feb 29, 2024 at 4:37
  • try the following solution it will work on android. Commented Feb 29, 2024 at 4:38

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.