0

So I've been trying for a long time now to get my app the have google play games achievements and I can't get it to work. I've been following this guide: https://developers.google.com/games/services/android/signin#implementing_player_sign-in and am having no success, every time I open my app it starts the sign in process but then it stops and says it's failed. It's probably something I missed, but I still can't figure it out.

Time for some code:

build.gradle:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation "com.google.android.gms:play-services-games:12.0.1"
    implementation "com.google.android.gms:play-services:12.0.1"
    implementation 'com.android.support:multidex:1.0.3'
}

manifest:

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.one.second">
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:label="One Second"
    android:icon="@drawable/app_icon"
    android:theme="@style/AppTheme">
    <meta-data android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category   android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".LowestActivity"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="portrait"/>
</application>

The parts of main.java that are important:

private static final int RC_SIGN_IN = 9001;

@Override
protected void onCreate(Bundle _savedInstanceState) {
    ...
    signInSilently();
}

private void signInSilently() {
    GoogleSignInClient signInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
    signInClient.silentSignIn().addOnCompleteListener(this,
            new OnCompleteListener<GoogleSignInAccount>() {
                @Override
                public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "Success signInSilently");
                        GoogleSignInAccount signedInAccount = task.getResult();
                    } else {
                        Log.d(TAG, "Failed signInSilently");
                        startSignInIntent();
                    }
                }
            });
}

private void startSignInIntent() {
    Log.d(TAG, "startSignInIntent()");
    GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
            GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
    Intent intent = signInClient.getSignInIntent();
    startActivityForResult(intent, RC_SIGN_IN);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        Log.w(TAG, "signInResult:success");
    } catch (ApiException e) {
        Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
        signInSilently();
    }
}

ids.xml

...
<!-- app_id -->
<string name="app_id" translatable="false">874********</string>
...

See image:

Dracarys
  • 714
  • 7
  • 16
Matthew Vine
  • 120
  • 2
  • 16

1 Answers1

4

In my experience this error can be caused by 3 different reasons:

  1. You need to login manually before you can use silentSignIn.

  2. You haven't added the emails you are testing with to the Testers tab in Google Play Games Console (or enable all emails).

  3. Your build hasn't been signed with the same SHA1 key that your Google Play Games app id was generated with

refs:

ApiException on silent signing using GoogleSignInClient on Android

Google play game service error code 400

  • Ok, I never paid attention to the number 3. How do I do that? (detailed explanation of the third option would be very helpful :) – Matthew Vine Jun 26 '18 at 10:40
  • https://developers.google.com/games/services/console/enabling have you follow these steps? On number 3 an application ID is generated. In order to do that a SHA1 key contained in a .keystore file is needed. You need to make sure your app is signed with that same SHA1 key your application ID was generated with. Bare in mind that you might have to create differents applications IDs per environment (DEV, QA, PROD) and each of them should correspond to the SHA1 key which its build is signed with. https://developer.android.com/studio/publish/app-signing – Axel Schenkelman Jun 26 '18 at 14:34