Patrick Sturm Patrick Sturm - 4 months ago 18
JSON Question

Value from google-services.json is returned wrong

when i try to get the gcm_defaultSenderId for a specific android flavor at runtime it does not deliver the correct value.

For example: i got the flavor tst and the google-services.json looks like this

{
"project_info": {
"project_number": "12345678910", 12345678910
"project_id": "api-project-12345678910"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:12345678910:android:xxxxxxxxxxxxx",
"android_client_info": {
"package_name": "do.something.app"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "XXXxxxx-XXXXxxxXXXXXxxxxXXXx"
}
],
"services": {
"analytics_service": {
"status": 1
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 1
}
}
}
],
"configuration_version": "1"
}


the json-file is placed in tst/google-services.json. When i try to access the gcm_defaultSenderId with R.string.gcm_defaultSenderId at runtime the returned value is : 9876543 . I expected the return value to be the project_number which is set at build to the gcm_defaultSenderId like its written here: https://developers.google.com/android/guides/google-services-plugin . Has anybody a idea why the value is not the same as in the google-service.json?

Answer

I fixed it. I tried to get the gcm_defaultSenderId like this:

R.string.gcm_defaultSenderId 

The value there is a hex value like 0X23923912 which seems to me is a pointer to the correct value which is set at build time (?). Now it seems like Java is converting this value to a int (?) and returns the number 9876543. I fixed this by using the current Context:

context.getResources().getString(R.string.gcm_defaultSenderId

Now the correct SenderId is returned.