Search code examples
androidgoogle-signingoogle-people-api

Google People API doesn't work in release mode android


I am using google people API to sign in and retrieve person data of google account. Here's my code for fetching person data:

    protected Void getData(GoogleSignInAccount acct) {
                profile.setEmail(acct.getEmail());
                profile.setName(acct.getDisplayName());

                GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
                        context, Collections.singleton(Scopes.PROFILE)
                );
                credential.setSelectedAccount(new Account(acct.getEmail(), "com.google"));
                People service = new People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                        .setApplicationName(context.getString(R.string.application_name))
                        .build();

                Person meProfile = null;
                try {
                    //here is the problem in release
                    meProfile = service.people().get("people/me").execute();

                } catch (IOException e) {
                    Logger.d(TAG, e);// it returns 404 Not Found
                    Logger.writeLog(LoginActivityNew.this, TAG, "e msg - " + e.getMessage());
                }

                if (meProfile != null) {
                    if (UtilCommon.isEmpty(profile.getName())) {
                        List<Name> names = meProfile.getNames();
                        if (names != null) {
                            for (Name name : names) {
                                if (!UtilCommon.isEmpty(name.getDisplayName())) {
                                    profile.setName(name.getDisplayName());
                                    break;
                                }
                            }
                        }

                    }

                    List<Birthday> birthdays = meProfile.getBirthdays();
                    if (birthdays != null && birthdays.size() > 0) {

                        for (Birthday bDay : birthdays) {
                            if (bDay == null || bDay.getDate() == null) continue;
                            Date date = bDay.getDate();
                            Calendar calendar = Calendar.getInstance();
                            calendar.set(Calendar.DAY_OF_MONTH, date.getDay());
                            calendar.set(Calendar.MONTH, date.getMonth());
                            calendar.set(Calendar.YEAR, date.getYear());
                            calendar.set(Calendar.HOUR_OF_DAY, 0);
                            calendar.set(Calendar.MINUTE, 0);
                            calendar.set(Calendar.SECOND, 0);
                            calendar.set(Calendar.MILLISECOND, 0);
                            profile.setDOB(calendar.getTime());

                            break;
                        }
                    }

                    List<Gender> genders = meProfile.getGenders();
                    if (genders != null && genders.size() > 0) {

                        profile.setSex(Profile.SEX_NONE);
                        for (Gender genderObj : genders) {
                            if (genderObj == null || UtilCommon.isEmpty(genderObj.getValue())) continue;
                            String gender = genderObj.getValue();
                            if (gender.equals("male"))
                                profile.setSex(Profile.SEX_MALE);
                            else if (gender.equals("female"))
                                profile.setSex(Profile.SEX_FEMALE);
                            else if (gender.equals("unknown"))
                                profile.setSex(Profile.SEX_NONE);
                            else
                                profile.setSex(Profile.SEX_OTHERS);

                            break;
                        }
                    }

                    Logger.writeLog(LoginActivityNew.this, TAG, "name - " + profile.getName());
                    Logger.writeLog(LoginActivityNew.this, TAG, "gender - " + profile.getSex());
                    if (profile.getDOB() != null)
                        Logger.writeLog(LoginActivityNew.this, TAG, "dob - " + profile.getDOB());
                }
            }

Here in meProfile = service.people().get("people/me").execute(); block an interesting thing is happening though Google sign in process is working fine.

It works perfectly while in debug mode but not in release mode.

While using release mode this block returns an exception : 404 Not Found

The requested URL was not found on this server.

I checked the google console project and provided valid SHA-1 certificate for debug and release keys.

Anyone knows about this issue? I am stuck.


Solution

  • Generate signed APK by disabling proguard if you have enabled it. You can do this by setting minifyEnabled = false in your build.gradle file.

    If the issue is solved, then its a proguard related issue.

    You can solve this buy including the people API related classes in proguard file using keep class attribute.