“signInSilently(): 失败"尝试使用 LibGDX 登录 GooglePlay 游戏服务时

2022-01-12 00:00:00 android java libgdx google-play-games

在过去的 3 天里,我一直在尝试将 Google 游戏服务 添加到我的 LibGDX 项目中,起初我尝试了 LibGDX 教程,但它们似乎都已过时.然后我被建议使用谷歌游戏服务官方代码

LibGDX:如何实现 Google Play 游戏服务?

我导入了示例项目 TypeANumber 并尝试将代码添加到我的项目中,但是当我尝试登录时,我收到 "signInSilently(): failure" 错误,当我尝试在调试和打开排行榜和成就时崩溃签名的APKS.

这是我的代码:

Android 启动器:

 private void signInSilently() {Log.d(TAG, "signInSilently()");mGoogleSignInClient.silentSignIn().addOnCompleteListener(这个,新的 OnCompleteListener() {@覆盖公共无效 onComplete(@NonNull 任务<GoogleSignInAccount> 任务) {if (task.isSuccessful()) {Log.d(TAG, "signInSilently(): 成功");onConnected(task.getResult());} 别的 {Log.d(TAG, "signInSilently(): 失败", task.getException());onDisconnected();}}});}@覆盖公共无效showLeaderboards(){runOnUiThread(new Runnable() {公共无效运行(){onShowLeaderboardsRequested();}});}@覆盖公共无效showAchievements(){runOnUiThread(new Runnable() {公共无效运行(){onShowAchievementsRequested();}});}

清单:

<?xml version="1.0" encoding="utf-8"?><清单 xmlns:android="http://schemas.android.com/apk/res/android"包="com.harrybanda.blaster" ><使用-sdk android:minSdkVersion="14" android:targetSdkVersion="27"/><使用权限android:name="android.permission.INTERNET"/><使用权限 android:name="android.permission.ACCESS_NETWORK_STATE"/><元数据 android:name="com.google.android.gms.games.APP_ID"android:value="@string/app_id"/><元数据 android:name="com.google.android.gms.version"android:value="@integer/google_play_services_version"/><应用机器人:allowBackup="真"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/GdxTheme" ><活动android:name="com.google.android.gms.ads.AdActivity"android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"android:theme="@android:style/Theme.Translucent"/><活动android:name="com.harrybanda.blaster.AndroidLauncher"android:label="@string/app_name"安卓:screenOrientation="人像"android:configChanges="keyboard|keyboardHidden|orientation|screenSize"><意图过滤器><action android:name="android.intent.action.MAIN"/><类别 android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动></应用程序></清单>

在我的 build.gradle 中我添加了:

 编译com.google.android.gms:play-services-games:11.8.0"编译com.google.android.gms:play-services-auth:11.8.0"

Logcat:

01-07 09:56:30.011 618-618/?D/Blaster:onResume()01-07 09:56:30.011 618-618/?D/Blaster:signInSilently()01-07 09:56:30.512 618-618/?D/Blaster:signInSilently():失败com.google.android.gms.common.api.b: 4:在 com.google.android.gms.common.internal.y.a(未​​知来源)在 com.google.android.gms.common.internal.ae.a(未知来源)在 com.google.android.gms.common.internal.af.a(未知来源)在 com.google.android.gms.common.api.internal.BasePendingResult.c(未知来源)在 com.google.android.gms.common.api.internal.BasePendingResult.a(未知来源)在 com.google.android.gms.auth.api.signin.internal.g.a(未知来源)在 com.google.android.gms.auth.api.signin.internal.r.onTransact(未知来源)在 android.os.Binder.execTransact(Binder.java:451)01-07 09:56:30.512 618-618/?D/Blaster:onDisconnected()

解决方案

从 1st 看你的问题,似乎你将元数据保留在应用程序标签之外,但应该在应用程序内AndroidManifest.xml 文件中的标记

像这样:

 <应用程序机器人:allowBackup="真"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/GdxTheme" ><活动android:name="com.google.android.gms.ads.AdActivity"android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"android:theme="@android:style/Theme.Translucent"/><活动android:name="com.harrybanda.blaster.AndroidLauncher"android:label="@string/app_name"安卓:screenOrientation="人像"android:configChanges="keyboard|keyboardHidden|orientation|screenSize"><意图过滤器><action android:name="android.intent.action.MAIN"/><类别 android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动><元数据 android:name="com.google.android.gms.games.APP_ID"android:value="@string/app_id"/><元数据 android:name="com.google.android.gms.version"android:value="@integer/google_play_services_version"/></应用程序>

<块引用>

StatusCode 4 (SIGN_IN_REQUIRED) 错误表示客户端试图连接到服务,但用户未登录.

<小时>

这是我的 AndroidLauncher

public class AndroidLauncher extends AndroidApplication implements MyServices {私人 GoogleSignInClient mGoogleSignInClient;私有 LeaderboardsClient mLeaderboardsClient;私人 PlayersClient mPlayersClient;私有静态最终字符串 TAG=AndroidLauncher.class.getSimpleName();私有静态最终 int RC_SIGN_IN = 9001;私有静态最终 int RC_UNUSED = 5001;私有静态最终 int RC_LEADERBOARD_UI = 9004;私人字符串 greetingMsg="欢迎,";私人布尔问候显示;@覆盖protected void onCreate (Bundle savedInstanceState) {super.onCreate(savedInstanceState);问候显示=假;AndroidApplicationConfiguration 配置 = 新 AndroidApplicationConfiguration();初始化(新的 GdxGame(这个),配置);mGoogleSignInClient = GoogleSignIn.getClient(这个,新的 GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build());}@覆盖公共布尔 isSignedIn() {返回 GoogleSignIn.getLastSignedInAccount(this) != null;}私人无效signInSilently(){mGoogleSignInClient.silentSignIn().addOnCompleteListener(这个,新的 OnCompleteListener() {@覆盖公共无效 onComplete(@NonNull 任务<GoogleSignInAccount> 任务) {if (task.isSuccessful()) {greetingMsg="欢迎回来,";onConnected(task.getResult());} 别的 {onDisconnected();}}});}私人无效 onConnected(GoogleSignInAccount googleSignInAccount) {mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);mPlayersClient.getCurrentPlayer().addOnCompleteListener(new OnCompleteListener<Player>() {@覆盖公共无效 onComplete(@NonNull 任务<玩家>任务) {字符串显示名称;if (task.isSuccessful()) {displayName = task.getResult().getDisplayName();} 别的 {异常 e = task.getException();处理异常(e,getString(R.string.players_exception));显示名称 = "???";}如果(!问候显示)欢迎消息(显示名称);}});}私人无效welcomeMessage(字符串名称){Toast toast = Toast.makeText(this, greetingMsg + name, Toast.LENGTH_LONG);toast.setGravity(Gravity.TOP, 0, 0);查看视图 = toast.getView();TextView text = (TextView) view.findViewById(android.R.id.message);toast.show();问候显示=真;}@覆盖公共无效 startSignInIntent() {startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);}@覆盖protected void onActivityResult(int requestCode, int resultCode, Intent intent) {super.onActivityResult(requestCode, resultCode, intent);if (requestCode == RC_SIGN_IN) {任务<GoogleSignInAccount>任务 = GoogleSignIn.getSignedInAccountFromIntent(intent);尝试 {GoogleSignInAccount account = task.getResult(ApiException.class);greetingMsg="欢迎,";onConnected(帐户);} 捕捉(ApiException apiException){字符串消息 = apiException.getMessage();if (message == null || message.isEmpty()) {message = getString(R.string.signin_other_error);}onDisconnected();}}}@覆盖受保护的无效 onResume() {超级.onResume();无声地签到();}私人无效签出(){如果(!isSignedIn()){Log.w(TAG, "signOut() 已调用,但未登录!");返回;}mGoogleSignInClient.signOut().addOnCompleteListener(这个,新的 OnCompleteListener<Void>() {@覆盖公共无效 onComplete(@NonNull 任务<无效>任务) {布尔成功 = task.isSuccessful();Log.d(TAG, "signOut(): " + (成功 ? "success" : "failed"));onDisconnected();}});}私人无效 onDisconnected() {mLleaderboardsClient = null;mPlayersClient = null;}@覆盖public void submitScore(int score){如果(isSignedIn())mLleaderboardsClient.submitScore(getString(R.string.leaderboard_id), score);}@覆盖公共无效showLeaderBoard(){如果(isSignedIn())mLleaderboardsClient.getLeaderboardIntent(getString(R.string.leaderboard_id)).addOnSuccessListener(new OnSuccessListener() {@覆盖公共无效onSuccess(意图意图){startActivityForResult(意图,RC_LEADERBOARD_UI);}});}私人无效句柄异常(异常e,字符串详细信息){整数状态 = 0;if (e instanceof ApiException) {ApiException apiException = (ApiException) e;状态 = apiException.getStatusCode();}String message = getString(R.string.status_exception_error, details, status, e);新的 AlertDialog.Builder(this).setMessage(消息).setNeutralButton(android.R.string.ok, null).展示();}}

MyServices接口

界面 MyServices{无效 startSignInIntent();布尔 isSignedIn();无效showLeaderBoard();无效 submitScore(int score);}

用户安装应用后我第一次自己调用接口的startSignInIntent().

I have been trying to add Google Game Services to my LibGDX project for the past 3 days now at first I tried the LibGDX tutorials but all of them seem to be outdated. Then I was advised to use the Google Game Services official code

LibGDX: How to Implement Google Play Game Services?

I imported the sample project TypeANumber and tried to add the code to my project but when I try to sign in I'm getting the "signInSilently(): failure" error and it crashes when I try to open the leaderboards and achievements on both the debug and signed APKS.

Here is my code:

AndroidLauncher:

     private void signInSilently() {
            Log.d(TAG, "signInSilently()");

            mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
                    new OnCompleteListener<GoogleSignInAccount>() {
                        @Override
                        public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                            if (task.isSuccessful()) {
                                Log.d(TAG, "signInSilently(): success");
                                onConnected(task.getResult());
                            } else {
                                Log.d(TAG, "signInSilently(): failure", task.getException());
                                onDisconnected();
                            }
                        }
                    });
        }

    @Override
    public void showLeaderboards(){
        runOnUiThread(new Runnable() {
            public void run() {
                onShowLeaderboardsRequested();
            }
        });
    }

    @Override
    public void showAchievements(){
        runOnUiThread(new Runnable() {
            public void run() {
                onShowAchievementsRequested();
            }
        });
    }

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.harrybanda.blaster" >

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="27" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <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"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >

        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />

        <activity
            android:name="com.harrybanda.blaster.AndroidLauncher"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

In my build.gradle i added:

 compile "com.google.android.gms:play-services-games:11.8.0"
 compile "com.google.android.gms:play-services-auth:11.8.0"

Logcat:

01-07 09:56:30.011 618-618/? D/Blaster: onResume()
01-07 09:56:30.011 618-618/? D/Blaster: signInSilently()
01-07 09:56:30.512 618-618/? D/Blaster: signInSilently(): failure
                                            com.google.android.gms.common.api.b: 4: 
                                                at com.google.android.gms.common.internal.y.a(Unknown Source)
                                                at com.google.android.gms.common.internal.ae.a(Unknown Source)
                                                at com.google.android.gms.common.internal.af.a(Unknown Source)
                                                at com.google.android.gms.common.api.internal.BasePendingResult.c(Unknown Source)
                                                at com.google.android.gms.common.api.internal.BasePendingResult.a(Unknown Source)
                                                at com.google.android.gms.auth.api.signin.internal.g.a(Unknown Source)
                                                at com.google.android.gms.auth.api.signin.internal.r.onTransact(Unknown Source)
                                                at android.os.Binder.execTransact(Binder.java:451)
01-07 09:56:30.512 618-618/? D/Blaster: onDisconnected()

解决方案

From 1st look of your question, It seems that you're keeping meta data outside the application tag but should be inside application tag in AndroidManifest.xml file

Like this :

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >

        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />

        <activity
            android:name="com.harrybanda.blaster.AndroidLauncher"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

      <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"/>
    </application>

StatusCode 4 (SIGN_IN_REQUIRED) error means client attempted to connect to the service but the user is not signed in.


Here is my AndroidLauncher class

public class AndroidLauncher extends AndroidApplication implements MyServices {

    private GoogleSignInClient mGoogleSignInClient;
    private LeaderboardsClient mLeaderboardsClient;
    private PlayersClient mPlayersClient;

    private static final String TAG=AndroidLauncher.class.getSimpleName();

    private static final int RC_SIGN_IN = 9001;
    private static final int RC_UNUSED = 5001;
    private static final int RC_LEADERBOARD_UI = 9004;

    private String greetingMsg="Welcome, ";
    private boolean greetingDisplayed;

   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      greetingDisplayed=false;
      AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
      initialize(new GdxGame(this), config);

      mGoogleSignInClient = GoogleSignIn.getClient(this,
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build());

   }

   @Override
   public boolean isSignedIn() {
       return GoogleSignIn.getLastSignedInAccount(this) != null;
   }

   private void signInSilently() {

      mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
            new OnCompleteListener<GoogleSignInAccount>() {
                @Override
                public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                    if (task.isSuccessful()) {
                        greetingMsg="Welcome back, ";
                        onConnected(task.getResult());
                    } else {
                        onDisconnected();
                    }
                }
            });
    }

    private void onConnected(GoogleSignInAccount googleSignInAccount) {

       mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);
       mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);

        mPlayersClient.getCurrentPlayer()
            .addOnCompleteListener(new OnCompleteListener<Player>() {
                @Override
                public void onComplete(@NonNull Task<Player> task) {
                    String displayName;
                    if (task.isSuccessful()) {
                        displayName = task.getResult().getDisplayName();
                    } else {
                        Exception e = task.getException();
                        handleException(e, getString(R.string.players_exception));
                        displayName = "???";
                    }

                    if(!greetingDisplayed)
                       welcomeMessage(displayName);
                }
            });
    }

    private void welcomeMessage(String name){

        Toast toast = Toast.makeText(this, greetingMsg + name, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 0);
        View view = toast.getView();
        TextView text = (TextView) view.findViewById(android.R.id.message);
        toast.show();
        greetingDisplayed=true;
    }

    @Override
    public void startSignInIntent() {
        startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (requestCode == RC_SIGN_IN) {
          Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(intent);

          try {
             GoogleSignInAccount account = task.getResult(ApiException.class);
             greetingMsg="Welcome, ";
             onConnected(account);
          } catch (ApiException apiException) {
             String message = apiException.getMessage();
             if (message == null || message.isEmpty()) {
                message = getString(R.string.signin_other_error);
             }

             onDisconnected();
           }
        }
    }

    @Override
    protected void onResume() {
       super.onResume();
       signInSilently();
   }

   private void signOut() {

      if (!isSignedIn()) {
          Log.w(TAG, "signOut() called, but was not signed in!");
          return;
      }

      mGoogleSignInClient.signOut().addOnCompleteListener(this,
            new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    boolean successful = task.isSuccessful();
                    Log.d(TAG, "signOut(): " + (successful ? "success" : "failed"));

                    onDisconnected();
                }
            });
   }

    private void onDisconnected() {

       mLeaderboardsClient = null;
       mPlayersClient = null;
    }

    @Override
    public void submitScore(int score){
        if(isSignedIn())
         mLeaderboardsClient.submitScore(getString(R.string.leaderboard_id), score);
    }

    @Override
    public void showLeaderBoard() {
       if(isSignedIn())
          mLeaderboardsClient.getLeaderboardIntent(getString(R.string.leaderboard_id))
            .addOnSuccessListener(new OnSuccessListener<Intent>() {
                @Override
                public void onSuccess(Intent intent) {
                    startActivityForResult(intent, RC_LEADERBOARD_UI);
                }
            });
   }

   private void handleException(Exception e, String details) {
       int status = 0;

      if (e instanceof ApiException) {
          ApiException apiException = (ApiException) e;
          status = apiException.getStatusCode();
      }

      String message = getString(R.string.status_exception_error, details, status, e);

      new AlertDialog.Builder(this)
            .setMessage(message)
            .setNeutralButton(android.R.string.ok, null)
            .show();
    }   
}

And MyServices interface

interface MyServices{

    void startSignInIntent();
    boolean isSignedIn();
    void showLeaderBoard();
    void submitScore(int score);
}

First time I call startSignInIntent() of interface by myself after user install app.

相关文章