2016-03-26 93 views
1

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

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

} 

代码工作,如果我在一个紧凑的活动内的本地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"); 

我都试过,包括在com.google.android.gms.play服务认证 - 8.4.0外部的classes.jar文件我在Android Studio中拥有的库,但它不起作用。我还考虑过在Unity中直接使用身份验证,而不是制作插件,但是我所看到的有关做这类事情的所有信息都与Google Play游戏有关,并且我并不想将Google Play游戏API包含在我的应用程序中,我只想让用户使用他们的Google帐户登录,以便他们可以访问S3服务器。如果任何人已经实现了与Unity相似的功能,并且知道更好的方式来做到这一点,那么我就会全神贯注。我愿意使用不同的方式在我的应用中启用Google身份验证,唯一的必要条件是它必须在Unity中完成。

在此先感谢!

回答

0

我使用此代码对用户进行认证解决我的问题:

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-play-services
+1

你能告诉我你究竟在哪里放置了这段代码吗?我遇到了完全相同的问题:试图让用户使用他们的Google帐户登录Amazon S3。你是如何得到这个工作的?我将非常感谢帮助! –