谷歌认证的Andr​​oid中的Unity插件 [英] Google Authentication in Android Unity Plugin

查看:1391
本文介绍了谷歌认证的Andr​​oid中的Unity插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,包括与亚马逊S3服务器之间的通信。该应用程序在Unity被开发,我想包括系统,使用户可以通过他们的谷歌帐户进行身份验证,然后使用这些凭据通过Cognito访问S3服务器。要做到这一点,我需要在统一实行谷歌身份验证系统,而我有一个很难搞清楚如何做到这一点。我目前的做法包括使用Android Studio中创建一个插件来访问谷歌登录API,但我每次执行程序时,它抛出一个NoClassDefFoundError的异常。这是我的logcat:

I'm working on an Android application that includes communication with the Amazon S3 servers. The app is being developed in Unity and I would like to include a system so that the users can authenticate with their Google Accounts and then use those credentials to access the S3 server through Cognito. To do that, I need to implement a Google Authenticator system in Unity, and I am having a hard time figuring out how to do it. My current approach consists in creating a Plugin with Android Studio to access the Google Sign In API, but every time I execute the program, it throws a NoClassDefFoundError exception. Here is my 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

这是我的Andr​​oid code(UnityPlayer活动)的相关部分:

Here is the relevant part of my Android code (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);

}

如果我在一个紧凑的活动里土生土长的APK执行它,但是当我把它变成一个插件,以及团结运行它,我得到错误的code工作。在团结,我称之为这个code中的身份验证()方法:

The code works if I execute it in a native APK inside a Compact Activity, but when I make it into a plugin and run it with Unity, I get the error. In Unity, I call the authenticate() method with this code:

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

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

我已经试过,包括我有Android Studio中的com.google.android.gms.play服务-AUTH-8.4.0外部库的classes.jar文件,但没有奏效。我也曾考虑推行直接在统一的认证而不是一个插件,但所有的信息,我看到这样做类似的东西涉及到谷歌玩游戏,我不感兴趣,包括谷歌玩游戏API在我的应用程序,我只想让用户与他们的谷歌帐户登录,以便他们可以访问S3服务器。如果有人已经实施使用Unity类似的功能,知道这样做的更好的办法,我洗耳恭听。我愿意用不同的方式,使谷歌验证我的应用程序,唯一必要的是,它在统一工作要做。

I have tried including the classes.jar file in the com.google.android.gms.play-services-auth-8.4.0 external library that I have in Android Studio, but it didn't work. I have also considered implementing the authentication directly in Unity instead of making a plugin, but all the information that I saw about doing something like that relates to Google Play Games, and I am not interested in including the Google Play Games API in my application, I just want to let the users log in with their Google account so that they can access the S3 server. If anyone has implemented a similar feature with Unity and knows a better way of doing this, I'm all ears. I am open to using a different way to enable Google Authentication in my app, the only requisite is that it has to be done in Unity.

在此先感谢!

推荐答案

我解决了使用这种code用户进行身份验证我的问题:

I solved my problem using this code to authenticate the user:

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文件的形式:

Also, I had to add the following libraries in form of JAR files:


  • Android的支持-V4

  • 的Google Play服务

这篇关于谷歌认证的Andr​​oid中的Unity插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆