繁体   English   中英

Android Unity插件中的Google身份验证

[英]Google Authentication in Android Unity Plugin

我正在开发一个包含与Amazon S3服务器通信的Android应用程序。 该应用程序是在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活动)的相关部分:

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");

我尝试在Android Studio中将class.jar文件包含在com.google.android.gms.play-services-auth-8.4.0外部库中,但是没有用。 我还考虑过直接在Unity中实现身份验证,而不是制作一个插件,但是我看到的有关执行此类操作的所有信息都与Google Play游戏有关,而且我不希望在应用程序中包含Google Play Games 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
  • 谷歌播放服务

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM