第2步,添加依赖项
(1)、在项目根目录 (Project) build.gradle 中添加 google()
确保buildscript和allprojects 部分中都包含 Google 的 Maven 存储库() 。
buildscript { repositories { google() mavenCentral() } ... } allprojects { repositories { google() mavenCentral() jcenter() } }
(2)、在 app 级 build.gradle 中添加库
implementation 'com.google.android.gms:play-services-auth:19.2.0' • 1
(3)、java 代码实现 google 登录封装
package org.cocos2dx.javascript.tools; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.util.Log; import com.google.android.gms.auth.api.signin.GoogleSignIn; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInClient; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; import com.google.android.gms.common.api.ApiException; import com.google.android.gms.tasks.Task; import org.cocos2dx.javascript.Native; import java.util.HashMap; import java.util.Map; public class GoogleUtils { private String m_actTag ="GoogleUtils"; private String m_googleLoginCallBack=""; private String client_id = ""; private Activity m_activity=null; private static final int SIGN_LOGIN = 901; private GoogleSignInClient mGoogleSignInClient; private static GoogleUtils g_Instace = null; public static GoogleUtils getInstance() { if (null == g_Instace) { g_Instace = new GoogleUtils(); } return g_Instace; } public Intent getGoogleIntent() { Intent signInInten; signInInten = mGoogleSignInClient.getSignInIntent(); return signInInten; } public void onActivityResult(int requestCode, int resultCode, Intent data) { if(mGoogleSignInClient != null) { switch (requestCode) { case SIGN_LOGIN: Log.d(m_actTag,"setActivityResultGoogle"); Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); if (task == null) { Log.d(m_actTag,"task:null"); } try { GoogleSignInAccount account = task.getResult(ApiException.class); Log.d(m_actTag,"Id:" + account.getId() + "|Email:" + account.getEmail() + "|IdToken:" + account.getIdToken()); String personName = account.getDisplayName(); String personGivenName = account.getGivenName(); String personFamilyName = account.getFamilyName(); String personEmail = account.getEmail(); String personId = account.getId(); String token = account.getIdToken(); Uri personPhoto = account.getPhotoUrl(); Map<String,String> map = new HashMap<String, String>(); map.put("result", "22"); map.put("info", "google info success"); map.put("id", personId); map.put("token", token); map.put("Email", personEmail); map.put("firstName", personGivenName); map.put("lastName", personFamilyName); map.put("userName", personName); Native.nativeToLogic(m_googleLoginCallBack,map); } catch (ApiException e) { e.printStackTrace(); Log.d(m_actTag,"ApiException:" + e.getMessage()); Log.e(m_actTag, "google login error:" + e.getMessage()); Map<String,String> map = new HashMap<String, String>(); map.put("result", "21"); map.put("info", e.getMessage()); Log.d(m_actTag,"error!!!!!"); Native.nativeToLogic(m_googleLoginCallBack,map); } break; } } } public void loginGoogle(final String callback){ m_googleLoginCallBack = callback; m_activity.startActivityForResult(getGoogleIntent(), SIGN_LOGIN); } public void logOutGoogle() { if(mGoogleSignInClient != null) { mGoogleSignInClient.signOut(); } } public void initSDK(final Activity activity){ m_activity = activity; client_id = activity.getResources().getString(R.string.google_client_id); if (mGoogleSignInClient == null) { GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions .DEFAULT_SIGN_IN) .requestEmail() .requestIdToken(client_id) .build(); mGoogleSignInClient = GoogleSignIn.getClient(m_activity, gso); } } }
(4)、res/values/strings.xml 中配置 Web客户端ID
<string name="google_client_id">200615557981-4mgn8ufi9chaj6gm135aaq6oul01mbph.apps.googleusercontent.com</string>
(5)、java 封装 google sdk 初始化,处理返回,并提供登录、登出接口 供 js、ts层调用
public class AppActivity extends Cocos2dxActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GoogleUtils.getInstance().initSDK(this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { GoogleUtils.getInstance().onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data); } public static void loginGoogle(final String callback) { GoogleUtils.getInstance().loginGoogle(callback); } public static void logOutGoogle() { GoogleUtils.getInstance().logOutGoogle(); } }
(6)、ts 或js 代码封装 java 接口
export class Native { public static loginGoogle(func: any,funcParams:string): void { if (cc.sys.isNative) { let cbKey: string = "loginGoogle" window[this.CallBackKey][cbKey] = func let ret = 0; if (cc.sys.os == cc.sys.OS_ANDROID) { ret = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "loginGoogle", "(Ljava/lang/String;)V", this._callBackPrefix + cbKey + funcParams); } else if (cc.sys.os == cc.sys.OS_IOS) { ret = jsb.reflection.callStaticMethod(Native.ApiIOS, "loginGoogle:", this._callBackPrefix + cbKey); } } } //登出FB public static logOutGoogle() { if (cc.sys.isNative) { console.log("logOutGoogle!!!!") if (cc.sys.os == cc.sys.OS_ANDROID) { jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logOutGoogle", "()V"); } else if (cc.sys.os == cc.sys.OS_IOS) { jsb.reflection.callStaticMethod(Native.ApiIOS, "logOutGoogle", null); } } } ...... }
(7)、ts 或js 代码调用google 登录接口
loginGoogle(){ let self = this; Native.loginGoogle((ret: any) => { self.googleSdkBack(ret) },"(%s)") } //请求SDK返回 googleSdkBack(backInfo:any){ console.log("googleSdkBack:",JSON.stringify(backInfo)) if(backInfo){ if(backInfo.result == "21"){//GG登录失败 }else if(backInfo.result == "22"){//GG登录成功并返回用户数据 } } }
注意事项:
1、需要使用在同意屏幕时填写的 google 测试账号进行登录测试。
2、需要使用Web客户端ID才能成功。