Google Authentication in Android Unity Plugin

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

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 

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

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

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.

Thanks in advance!

回答1:

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

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

  • android-support-v4
  • google-play-services


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!