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:
