Generate Google API Key For Xamarin Android Applications

Introduction

Android allows us to integrate Google maps in our applications, so we need to generate an API key, using Google developer account. This article shows you how to generate Google map API key from Google developer account .


Setup new Xamarin forms project

We need to associate a package name to Google API, so first create API key. Let's start creating new Xamarin forms project in Visual Studio.

Open Run ➔ Type Devenev.Exe and enter ➔ New Project (Ctrl+Shift+N)➔ select Blank Xamarin.Forms Portable template.


It will automatically create multiple projects like Portable, Android, iOS, UWP. You can refer to my previous article for more.

Register Google Maps Android API

I have shown the steps given below to register Google maps for an Android application.

Step 1

Navigate to Google developer API account here.

Step 2

If you are asked to sign in, provide Google user Id, password and click Sign in.

Step 3

Select My project list and select the existing project or create new project.



Step 4

Provide your project name and project name must be between 4 and 30 characters and click Create button.



Step 5

Make sure that your project is selected in the header, as shown in step 3 and click Google Maps Android API.
Step 6

Click Enable button for the access.


Step 7

Click Credentials to get API key.


Step 8

On credentials page, click What Credentials do I need?


Step 9 

You can get public API key or if you want to restrict the limit for which Web sites, IP address, and mobile apps can call this API key, then you can click Restrict Key.


Step 10

Change API name as your Application's name (Demo App Name DevenvExeMyLocation ) > Select Application type as an Android App. Click '+ Add Package name and fingerprint'.


Step 11

Add your package name and SHA-1 signing-certificate fingerprint to restrict the usage to your Android apps. Follow the steps given below to get the package name and SHA-1 fingerprint.

Step 12
 
How to get Package Name?

Go back to your Xamarin Application and select Droid project from Solution Explorer > Right click and select as Property > Click Android Manifest and change or add the package name> select location and map permission.


Step 13

How to generate SHA-1 certificate fingerprint?

The SHA1 signature of a Xamarin.Android app depends on the .keystore file, whichwas used to sign the APK. Typically, a debug build will use a different. keystore file than a release build.

  • Navigate to AppData folder
    Click Windows key + R ( Run ) > type %AppData% and click Ok > Goto AppData folder > Local > Xamarin >Mono for Android and note the debug.keystore file location - C\Users\<username>\AppData\Local\Xamarin\Mono for an Android.
  • Find KeyStore.exe for execution
    Keystore fill is run by Keytool. Keytool is a free certificate tool provided by Oracle as a part of Java software. If you have Java installed on your Windows computer, you can find it using these.

    Go to C\Program Files (x86)\Java > Select Java Version (jdk1.7.0_55) > Select bin folder > Find Keytool.exe file.

  • Now, you can open CMD and navigate keytool.exe folder path, as shown below.

    Goto C\Program Files (x86)\Java > Select Java Version (jdk1.7.0_55) > Select bin folder > Press Alt + D or select Address > replace location path to CMD > press enter


  • run keystore.exe from following cmd
    keytool.exe -list -v -keystore "%LocalAppData%\Xamarin\Mono for Android\debug.keystore" -alias androiddebugkey -storepass android -keypass android


  • The result looks, as shown below.


Step 14

Now, go back to Google developer Website, update the package name and SHA1 key > click Save.


Step 15

In Visual Studio Project, go to your Xamarin Droid project. Under property folder > open AndroidManifest.xml and add the line of code given below with your API key.

<meta-data androidname="com.google.android.geo.API_KEY" androidvalue="API Key” />

I will share Google map implementation in my next article. If you have any questions or feedback, please share in the comment box.


Similar Articles