Google Maps Android API v2 Authorization failure
Asked Answered
A

18

100

My steps:

  • got SHA1 code from debug.keystore
  • create app in google apis console
  • enabled google map api v2
  • input SHA1;my.package.name
  • get API key
  • created AndroidManifest file:

        <permission
                android:name="my.package.name.permission.MAPS_RECEIVE"
                android:protectionLevel="signature"/>
    
        <uses-permission android:name="my.package.name.permission.MAPS_RECEIVE"/>
    
        <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
    
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    
        <uses-feature android:glEsVersion="0x00020000" android:required="true"/>
    
        <application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:hardwareAccelerated="true">
    
            <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="MY_API_KEY_HERE" />
    
            <activity android:name="MyActivity"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
        </application>
    </manifest>
    
  • created layout

  • put "google-play-services.jar" to libs

After compilation I've got crash:

  ERROR/AndroidRuntime(10182): FATAL EXCEPTION: main
                java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
                at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
                at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
                at android.app.Activity.onCreateView(Activity.java:4716)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
                at android.app.Activity.setContentView(Activity.java:1881)
                at com.example.gm2.MyActivity.onCreate(MyActivity.java:16)
                at android.app.Activity.performCreate(Activity.java:5104)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                at android.app.ActivityThread.access$600(ActivityThread.java:141)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:5039)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                at dalvik.system.NativeStart.main(Native Method)

After that I've changed layout to:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/map"
             android:layout_width="match_parent"
             android:layout_height="match_parent" />

and changed MyActivity to

  super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();

    transaction.add(R.id.map, MapFragment.newInstance());           
    transaction.commit();

As result, the application was started, but I didn't see the map.

Console log:

ERROR/Google Maps Android API(10369): Authorization failure.
Amery answered 4/12, 2012 at 5:26 Comment(1)
OMG! i had always the same error R$stylable class not found. Now i changed my code to your programmatic way to implement the MapFragment and it works! Thanks! But you need to create you key like described belowAuspicious
A
108

Steps:

  • to ensure that device has Google Play services APK
  • to install Google Play Service rev. more than 2

enter image description here

  • to create project at https://code.google.com/apis/console/
  • to enable "Google Maps Android API v2" enter image description here
  • to register of SHA1 in project (NOW, YOU NEED WRITE SHA1;your.app.package.name) at APIs console and get API KEY
  • to copy directory ANDROID_SDK_DIR/extras/google/google_play_services/libproject/google-play-services_lib to root of your project
  • to add next line to the YOUR_PROJECT/project.properties

android.library.reference.1=google-play-services_lib

  • to add next lines to the YOUR_PROJECT/proguard-project.txt

.

-keep class * extends java.util.ListResourceBundle {

   protected Object[][] getContents();

}

Now you are ready to create your own Google Map app with using Google Map APIs V2 for Android.

If you create application with min SDK = 8, please use android support library v4 + SupportMapFragment instead of MapFragment.

Amery answered 5/12, 2012 at 9:9 Comment(5)
These instructions didn't do the whole job. I needed to copy proguard.cfg from the maps API sample application. Only then it started to work.Anthocyanin
Also, I noticed that the Signature Algorithm MUST be SHA1withRSA and NOT SHA256withRSA (the default created by eclipse for the debug key).Anthocyanin
@YoelGluschnaider : How can i change default algorithm from SHA256withRSA to SHA1withRSA ?Inner
Why do i need to install Google Play Services in Android Studio in order to run it on my phone? What's the relation?Boote
Can someone update this for 2020? All the images no longer apply Google changed their tools .Beals
B
36

Also check this post: Google Map Android Api V2 Sample Code not working, if you are completely sure you did the right steps then follow the second answer, the authentication gets cached somewhere, try to uninstall the application manually (just like you do with a normal application) then "Run" again the project

Blastoderm answered 30/9, 2013 at 7:24 Comment(4)
I cannot believe that just worked. Thanks @Alejandro del Rio :)Asch
I bet, just like me, 90% of the people here really want this answer and not the selected one (although helpful as well)Rafiq
In my case the cache (?) was updated while I was reading Stackoverflow with the authentication error message on log. The app was running with while screen, after a few minutes the map was loaded and no more auth errors. I guess a reinstall would also have done it.Osmose
For future readers. Delete bin and gen. Clean, rebuild and the cached credentials are renewed.Pallas
K
20

Since I just wasted a lot of time getting the API to work, I will try to give a step-by-step validation for the Map API v2:

Step 1: Apply for your API key

If you are unfamiliar with the Google API console, read the very good answer of Rusfearuth above.

Step 2: Check you SHA Hash (in this case I use the debug key of eclipse):

On a Windows machine got to your user directory on a command prompt:

C:\Users\you>keytool -list -alias androiddebugkey -keystore .android\debug.keyst
ore -storepass android -keypass android

You will get something like:

androiddebugkey, 15.10.2012, PrivateKeyEntry,
Zertifikat-Fingerprint (SHA1): 66:XX:47:XX:1E:XX:FE:XX:DE:XX:EF:XX:98:XX:83:XX:9A:XX:23:A6

Then look at your package name of the map activity, e.g. com.example.mypackagename

You combine this and check that with your settings in the Google API console:

66:XX:47:XX:1E:XX:FE:XX:DE:XX:EF:XX:98:XX:83:XX:9A:XX:23:A6;com.example.mypackagename

where you get your API-key:

AZzaSyDhkhNotUseFullKey49ylKD2bw1HM

Step 3. Manifest meta data

Check if the meta-data are present and contain the right key. If you release your app, you need a different key.

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AZzaSyDhkhNotUseFullKey49ylKD2bw1HM" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Step 4. Manifest features:

You need this entry as the map API requires some grapics support:

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

Do not worry, 99.7% of devices support this.

Step 5. Manifest library:

Add the google library.

    <uses-library
        android:name="com.google.android.maps"
        android:required="false" /> // This is required if you want your app to start in the emulator. I set it to false also if map is not an essential part of the application.

Step 6. Manifest permissions:

Check the package name twice: com.example.yourpackage

<permission
    android:name="com.example.yourpackage.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.yourpackage.permission.MAPS_RECEIVE" />

Add the following permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

The following permissions are optional and not required if you just show a map. Try to not use them.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Step 7. Include the map fragment into your layout:

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    class="com.google.android.gms.maps.SupportMapFragment"
    map:cameraTargetLat="47.621120"
    map:cameraTargetLng="-122.349594"
    map:cameraZoom="15" />

If your release to 2.x Android versions you need to add support in your Activity:

import android.support.v4.app.FragmentActivity;

For the map: entries to work include

xmlns:map="http://schemas.android.com/apk/res-auto"

in your activity layout (e.g. LinearLayout).

In my case I have to clean the project each time I change something in the layout. Seems to be a bug.

Step 8: Use Eclipse - Project - Clean.

Enjoy!

Kyd answered 22/4, 2013 at 10:5 Comment(2)
i am following your steps but error in api key ,any solution ?Eyeopener
+1 for <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AZzaSyDhkhNotUseFullKey49ylKD2bw1HM" />Creon
H
16

You need to use the library project located at: ANDROID-SDK-DIRECTORY/extras/google/google_play_services/libproject

This will fix your first issue.

I'm not sure about your second, but it may be a side effect of the first. I am still trying to get the map working in my own app :)

Haldane answered 4/12, 2012 at 6:15 Comment(7)
I copied extras/google/google_play_services/libproject/libs/google-play-services.jar to MY_PROJECT/libs.Amery
That is not good enough. You need to use the entire library project, as it includes many resources. In Eclipse, you can import a new Android project, and just point it at the libproject directory. Then in your own project's properties, add a reference to it.Haldane
I don't use Eclipse (use Idea)Amery
I'm not sure how library projects work in Idea, but blogs.jetbrains.com/idea/2010/09/… should have that information.Haldane
I compiled jar from google-play-services_lib. It made work my app, but I still have bug 2. =/Amery
I've found why It didn't work. I turned on Google Map API v2 instead of Google Map Android API v2 service. =( Shame on me....Amery
Scott is right! I have just created an easy to understand tutorial: goo.gl/OQRpITelford
R
5

Today I faced with this problem. I used Android Studio 2.1.3, windows 10. While debugging it works fine, but if I update to release mode it does not work. I cleared all proguard conditions, updated, but this was not solution.

The solution is related with project structure. The google_maps_api.xml file was different between app\src\debug\res and app\src\release\res. I did manual copy paste from debug to release folder.

Now it works.

Rausch answered 17/10, 2016 at 15:37 Comment(0)
A
5

I followed most, if not all, of Gunnar Bernstein's suggestions mentioned above and it still didn't work. However, it started to work after I followed his suggestions AND the following:

  1. I created a new MapActivity by using Android Studio's right click list of options: New -> Google -> Google Maps Activity

  2. I then opened the google_maps_api.xml file that automatically gets generated and used the stated link to create a new API KEY. I did all the steps and saved my new key under my current project. I then removed my old registered API KEY as it was no longer required.

  3. Under the Manifest file I replaced the old API KEY value with the string shortcut created by this new XML file: android:value="@string/google_maps_key", instead of stating the KEY directly.

  4. Finally, remove the new MapActivity, but keep the xml file that was created in that process.

Note: By creating the API KEY in this way the Restrictions column, under Credentials, now stated "Android apps". Earlier, when it didn't work the column stated "Android apps, 1 API" or something similar. I do not know if this makes a difference or not. Note: No, that just means that I do not have a specific API selected for this API key (Console -> API key -> Key restrictions -> API restrictions).

Note: It looks like the meta-data tag under the Manifest file:

android:name="com.google.android.maps.v2.API_KEY

has been replaced by:

android:name="com.google.android.geo.API_KEY"

I guess that both can be used but it's better from the start using the latter.

Note: The value stated, in the build.gradle file, under android -> defaultConfig -> applicationId has to match the Package name text under the Credentials page.

Apache answered 17/1, 2018 at 14:58 Comment(2)
This what exactly what happened to me and this solution fixed the warning from the console. Somehow the Ionic build changed it.Clypeus
After searching for 8 hrs, I look back and apply this solution. Solved!!!Katabasis
H
4

Just Add this into your manifest file:

**<uses-library android:name="com.google.android.maps" />**

For Example...

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <uses-library android:name="com.google.android.maps" />

    <activity
        android:name=".California"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

And please update your google play service lib...Go to Window -> Android sdk manager->update google play service...installed it..delete old one and keep this one. Take this from ANDROID-SDK-DIRECTORY/extras/google/google_play_services/

Thanks..Please vote up

Hhour answered 26/7, 2013 at 6:51 Comment(0)
I
3

I solved this error by checking the package name in manifest and app build.gradle.

In manifest:

I am using a package name as:

com.app.triapp

In app build.gradle:

 defaultConfig {

        applicationId "com.app.live"
    }

After changed to com.app.triapp in build.gradle, solved my issue.

Iatrics answered 29/6, 2016 at 11:47 Comment(2)
Thanks a lot. You save my ton of time.Adaxial
Excellent Bro. I have everything OK, instead of this oneVasta
W
2

I am migrating from V1 to V2 of Google Maps. I was getting this failure trying to run the app via Eclipse. The root cause for me was using my release certificate keystore rather than the Android debug keystore which is what gets used when you run it via Eclipse. The following command (OSX/Linux) will get you the SHA1 key of the debug keystore:

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

If you are using Windows 7 instead, you would use this command:

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

It is probably best to uninstall your app completely from your device before trying with a new key as Android caches the security credentials.

Waits answered 21/3, 2014 at 23:44 Comment(0)
H
1

"java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable"

A had this error too, you need to: File -> import -> Existing Android Code Into Workspace -> Browse -> Android sdks -> extras -> google -> google_play_services -> lib_project -> google_play_services_lib and OK. Check the project and Finish.

The project goes to Package Explorer. Now go to your project (with error) -> properties -> Android and in Library click Add... the google_play_services_lib should be there! add and try again :)

Hobbledehoy answered 15/4, 2013 at 1:52 Comment(0)
T
1

I had same issue but it turned out that it was because I created two different map API keys with same SHA-1 fingerprint with nearly similar package name com.bla.bla and the other com.bla.bla.something.

Twylatwyman answered 23/8, 2016 at 7:49 Comment(0)
C
1
  • Make sure Maps SDK for Android is enabled in API console.
  • Also you might need to add your package name and SHA-1 signing-certificate fingerprint to restrict usage for your key to be fully enabled.

enter image description here

Cauliflower answered 17/6, 2020 at 18:15 Comment(0)
A
0

I had the exact same issue and i just needed to set the SDK for the library project of the MapsApiV2 to Android 2.3.3 (API lvl 10)

Auspicious answered 3/7, 2013 at 8:43 Comment(1)
I can't remember this stuffAuspicious
F
0

I had the same issue. After about two hours of googling, retries, regenerating API Key many times, etc. i discovered that i enabled the wrong service in the Google APis Console. I enabled Google Maps API v2 Service, but for Android Apps you have to use Google Maps Android API v2. After enabling the right service all started working.

Flinders answered 3/10, 2013 at 16:27 Comment(0)
S
0

YOUR ECLIPSE MAYBE CHANGED SHA1 KEY, so you must regen your google key with new SHA1 key in here: https://console.developers.google.com/project/watchful-net-796/apiui/credential After that, copy new key into manifest and reload this google page some times, and your key will be updated, rebuild your project, it will work. P/S: For my error, I deleted .android folder so eclipse regen SHA1.

Scleroma answered 25/1, 2015 at 9:15 Comment(0)
L
0

I followed the same step provided in the Google Doc but i was getting the same error. Later i realized that i was signing my app with my app.keystore and the SHA key that i was using to generate the API key was the debug one.

So i simply removed the signingConfig signingConfig.generic from by build.gradle file and my map started to work fine.

Hope this helps someone.

Leyba answered 2/6, 2016 at 6:16 Comment(0)
T
0

I faced the same issue.

If you issued an api for your release version of the application. Then that api will only work for the release version not for the debug version. So make sure you are using the release version in your phone or emulator .

Telluride answered 5/7, 2017 at 20:12 Comment(0)
L
0

For me, everything was working both in an emulator and on a physical device.

However, it stopped working for me when I set up a second machine to debug the same app in an emulator with a newer API level.

I did not realize that the fingerprint for that machine changed. After seeing the authentication failure error log in the Device Log I closely compared the fingerprint in my Google Developer Console with the one in the error message. That's when I saw the difference. After adding the new fingerprint to the Developer Console (in combination with the package name) my app started working as expected in the emulator on the new system.

So if you set up a new system, check your fingerprints again!

Longeron answered 2/11, 2018 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.