35

my issue is quite similar to Missing api_key/current key with Google Services 3.0.0 but with a further level of complexity.

I updated my app this morning to com.google.gms:google-services:3.0.0 and all the play-services 9.0.0 dependencies

compile 'com.google.android.gms:play-services-analytics:9.0.0'
compile 'com.google.android.gms:play-services-ads:9.0.0'
compile 'com.google.android.gms:play-services-identity:9.0.0'
compile 'com.google.android.gms:play-services-gcm:9.0.0'

Now I get the infamous error

Error:Execution failed for task ':hotelsclick:processDebugGoogleServices'. > Missing api_key/current_key object

This seems to be due to a missing API key in the google-services.json file (so I was told). I got a new API key from https://developers.google.com/cloud-messaging/android/client#get-config and found the following field in it.

  "api_key": [],

I guess I should put my Google Maps API key in there, but here it comes the double issue I'm experiencing:

1- if I try and put the key inside that field, it doesn't work. I tried with both

  "api_key": [{ "current_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-AE" }],

and

  "api_key": ["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-AE"],

the error remains the same.

2- I've been using gradle in order to use dynamic building. I put the following in my module build.gradle:

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        debuggable false
        resValue "string", "google_maps_api_key", "ABCDEFGHILMNO... my maps release API key"
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
        zipAlignEnabled true
    }
    debug {
        resValue "string", "google_maps_api_key", "ZNTMRNCDNR... my google maps debug API key"
        debuggable true
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }
}

In the Androidmanifest.xml file I have

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_api_key" />

Now, it's always been working until I updated to Google Services 3.0.0, I don't get why it doesn't work now. Shouldn't it grab the right API key from the compiled build.gradle file and put it in the manifest?

So my double question is: why doesn't the dynamic API key fetching work anymore? And how can I fix this?

Thank you

Community
  • 1
  • 1
Marco Zanetti
  • 4,051
  • 6
  • 32
  • 48

6 Answers6

73

Have you tried putting an empty "current_key" as follows:

"api_key": [{ "current_key": "" }]

See how that goes.

EDIT: You should replace "api_key": [] (located in "client") with "api_key": [{ "current_key": "" }]

nutella_eater
  • 3,393
  • 3
  • 27
  • 46
Nitrodbz
  • 1,276
  • 1
  • 15
  • 25
  • Woot! Works for me. A temp solution, but does enable me to build. Seems like these updates can be so tricky with so many different dependencies and parts. – Booger May 22 '16 at 17:58
  • to get the api_key, you would probably need to import your google project into firebase from the link below. From there, you can create a google_services.json file from there. https://console.firebase.google.com/ – Nitrodbz May 24 '16 at 15:23
  • @Nitrodbz you were right, this works. I just needed to perform a clean/rebuild after adding it, a simple build was not enough. Thank you so much! – Marco Zanetti Jun 09 '16 at 11:16
  • 1
    where i should put this string? – Android Android Jul 04 '16 at 10:58
  • You should put this in google-services.json. Here, you should replace `"api_key": []` (located in "client") with `"api_key": [{ "current_key": "" }]` – Cristan Dec 01 '16 at 09:51
  • One thing that worked for me is re-downloading the latest config file from the firebase console settings and overriding the existing google-services.json file. – Red M Jan 08 '17 at 21:54
9

I was able to solve this problem with these steps that are not answered here. This answer is NOT enabling Cloud Messaging, it is only retrieving its server key. (Clarifying because my answer got deleted for being a 'duplicate' when it is not at all).

So first, go to Firebase Console, and then go to Project Settings, and then click on the tab "Cloud Messaging". Look where it says "server key" and if there is no long key displaying (not the "Sender ID" number) click "Regenerate Key" and a long key should now show. Copy that key and place it in your google-services.json file at: "api_key": [ {"current_key" : "key goes here"} ].

This method helped me while none of the others were working, so hopefully this helps someone.

David Velasquez
  • 2,346
  • 1
  • 26
  • 46
4

This problem you will face after using new build toolclasspath 'com.google.gms:google-services:3.0.0' ,

Simply recreate your "google-services.json". it will be solved hopefully.

Sadman Samee
  • 107
  • 3
  • 7
1

If you accidentally removed the key or you can't get it back create a new api credential here and name it exactly “Android key (auto created by Google Service)”. This will get your credential back to the google-services json.

droidpl
  • 5,872
  • 4
  • 35
  • 47
0

You must enable Cloud Messaging even if you don't use it and generate a new json file. When you enable Cloud Messaging you get the API key need it for it.

Maybe you need any other service, see this: https://stackoverflow.com/a/37358432/4408810

Community
  • 1
  • 1
  • but that gives you a server api key which isn't what you should put in the json file. And I did the same thing enabled gcm but there was no api_key inside it – Nitrodbz May 19 '16 at 18:47
  • Yes, I don't know why, but in my case, when enable Cloud Messaging I get this API Key in JSON file. – Rafael Gutiérrez May 19 '16 at 19:05
  • I got the API key in the JSON file by enabling the Cloud Messaging API. The api_key entry was filled. "api_key": [ { "current_key": "the_api_key" } but still doesn't work. I don't really know what's wrong with it... – Marco Zanetti May 20 '16 at 08:43
0

In Android Studio, there's the concept of build types and flavors and you can use these to get what you need. Build types are different versions of the app that are functionally identical but may differ in debugging code. By default, all Android Gradle projects have debug and release build types.

In addition, you can add Google Maps activity to your project and Android Studio will generate necessary files for you, you only have to insert your keys.

Since you are using gradle, you can do the following:

build.gradle

android {
.. .. ...
buildTypes {
debug {
resValue "string", "google_maps_api_key", "[YOUR DEV KEY]"
}
release {
resValue "string", "google_maps_api_key", "[YOUR PROD KEY]"
}
}
}

AndroidManifest.xml

meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_api_key"/>

This way you only have one AndroidManifest.xml and you set value based on your build type.

You may also check this manifest placeholder: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-Placeholder-support

Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30
  • Thank you d.datul, this is precisely what I did :-) And it always worked till yesterday. I'm starting to suspect the API Key I'm getting the error about is not the GoogleMaps one but another one, maybe the Cloud Messaging one (although I'm not using cloud messaging). As you can see in the other comments I generated a new JSON file with the Google Messaging API but didn't work. – Marco Zanetti May 20 '16 at 12:44