Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...