-3

My android App written in java android studio. My app is connected to our server for getting data and customizing. I need to get Google map api key dynamically based on user account signed in to our server. When building app I don't want to provide api key. User have to set it in server then I get it and use it in app.

How to do this pls Help

I already tried adding in build config. But it cant update dynamically after building.

Harsha
  • 1
  • 1
  • why do you need to get several API Key? you just let users sign in and authenticate their account. then let them use your app. Anyway you can try this; https://stackoverflow.com/questions/31342359/dynamically-set-googleapi-key – Elias Fazel Mar 14 '23 at 14:57
  • This same app will be distributed to different clients. We cant charge them for maps usage. They will set their own key and use it – Harsha Mar 14 '23 at 17:04
  • The link you gave me is for Web but i need android java code to set api key before initialization – Harsha Mar 14 '23 at 17:07
  • You only need YOUR own API key on your app. Your users should not be required to have an API key if they want to use your app with Google Maps API. – Yrll Mar 15 '23 at 03:06
  • if i use my api key i have to pay the usage cost right ? – Harsha Mar 15 '23 at 04:25

2 Answers2

0

What I can suggest to you is that in your StartActivity (Launcher activity) make a REST call to your server to get the MAPS key and then store it in SharedPreferences to use it everywhere in the app.

Important : make sure to handle errors when getting the key especially in the first run of the app.

Ryad Amir
  • 21
  • 4
0

This can be accomplished using android flavors, you can create a new flavor for each client, this means that you can have a separate strings.xml for each flavor and have different values for the map API key strings,

MAPS_API_KEYS_FLAVOR_1

MAPS_API_KEYS_FLAVOR_2

This value will be picked up in the manifest when the app is built. Unsure, if this will solve your problem, but this is how i solved it in my own whitelabeled app, with different clients and we charged them for maps API cost.

Nikhil
  • 1
  • 2