Google Android Unity 插件中的身份验证

Google Authentication in Android Unity Plugin

我正在开发一个 Android 应用程序,其中包括与 Amazon S3 服务器的通信。该应用程序是在 Unity 中开发的,我想包含一个系统,以便用户可以使用他们的 Google 帐户进行身份验证,然后使用这些凭据通过 Cognito 访问 S3 服务器。为此,我需要在 Unity 中实现一个 Google 身份验证器系统,我很难弄清楚如何去做。我目前的方法是使用 Android Studio 创建一个插件来访问 Google 登录 API,但每次我执行该程序时,它都会抛出一个 NoClassDefFoundError 异常。这是我的 logcat:

03-25 20:45:34.968 25581-25610/? D/MainActivity: Authenticating...
03-25 20:45:35.086 25581-25610/? I/Unity: AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/auth/api/signin/GoogleSignInOptions$Builder;
                                          java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/auth/api/signin/GoogleSignInOptions$Builder;
                                              at com.unityplugin.MainActivity.authenticate(MainActivity.java:55)
                                              at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
                                              at com.unity3d.player.UnityPlayer.a(Unknown Source)
                                              at com.unity3d.player.UnityPlayer$b.run(Unknown Source)
                                           Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.auth.api.signin.GoogleSignInOptions$Builder" on path: DexPathList[[zip file "/data/app/com.unityplugin-2/base.apk"],nativeLibraryDirectories=[/data/app/com.unityplugin-2/lib/arm, /data/app/com.unityplugin-2/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
                                              at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                              at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                            at java.lang.ClassLoader.loadCla

这是我的 Android 代码(UnityPlayer Activity)的相关部分:

public void authenticate() {

    Log.d(TAG, "Authenticating...");
    // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
    //HERE IS THE ERROR (LINE 55)
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(Constants.GOOGLE_CLIENT_ID)
            .requestEmail()
            .build();

    // Build a GoogleApiClient with access to the Google Sign-In API and the
    // options specified by gso.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addConnectionCallbacks(new ConnCallBack())
            .addOnConnectionFailedListener(new FailedListener())
            .build();

    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);

}

如果我在 Compact Activity 内的本机 APK 中执行该代码,则该代码有效,但是当我将其制作成插件并使用 Unity 运行 时,我收到错误。在 Unity 中,我使用以下代码调用 authenticate() 方法:

//Get Activity
    AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");

    //Call function authenticate
    currentActivity.Call("authenticate");

我已经尝试将 classes.jar 文件包含在 Android Studio 中的 com.google.android.gms.play-services-auth-8.4.0 外部库中,但没有成功.我也考虑过直接在 Unity 中实现身份验证而不是制作插件,但我看到的所有关于做类似事情的信息都与 Google 玩游戏有关,我对包括 Google 在我的应用程序中玩游戏 API,我只想让用户使用他们的 Google 帐户登录,以便他们可以访问 S3 服务器。如果有人用 Unity 实现了类似的功能并且知道更好的方法,我会洗耳恭听。我愿意使用不同的方式在我的应用程序中启用 Google 身份验证,唯一的要求是它必须在 Unity 中完成。

提前致谢!

我使用此代码对用户进行身份验证解决了我的问题:

GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
String token = GoogleAuthUtil.getToken(getApplicationContext(), accounts[0].name,
        "audience:server:client_id:YOUR_GOOGLE_CLIENT_ID");
Map<String, String> logins = new HashMap<String, String>();
logins.put("accounts.google.com", token);
credentialsProvider.setLogins(logins);

此外,我还必须以 JAR 文件的形式添加以下库:

  • android-支持-v4
  • google-播放服务