Missing api_key/current key with Google Services 3

2019-03-12 04:43发布

问题:

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

回答1:

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": "" }]



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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



回答6:

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