准备工作
官方文档:https://firebase.google.cn/docs/android/setup
cocos2dx demo:https://github.com/FirebaseExtended/cocos2dx-cpp-sample
前期准备
有相关人员在后台配置好数据
由后台下载 google-services.json,放到project/app/路径下
andorid接入
1 build.gradle(project级别)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.google.gms:google-services:4.2.0' // Google Services plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
2 build.gradle (app/build.gradle)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':libcocos2dx')
implementation 'com.google.firebase:firebase-core:17.0.0' // firebaes 基础
implementation 'com.google.firebase:firebase-auth:19.1.0' // firebase 身份验证
implementation 'com.google.android.gms:play-services-auth:17.0.0' // google 登陆
// implementation 'com.google.firebase:firebase-analytics:17.2.0' // firebase analytics
// implementation 'com.google.firebase:firebase-messaging:20.0.0' // firebase messaging
}
apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
3.1 初始化
(这里的参数是google-server.json自动生成的,无需额外增加)
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(mActivity.getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(context,gso);
// firebase 身份验证
mFirebaseAuth = FirebaseAuth.getInstance();
3.2 登陆
public void signGoogle(final String args,final int luaFunc){
Log.d(TAG,"========= signGoogle === reqcode ="+SDKHelper.REQ_CODE_GOOGLE_LOGIN);
mGoogleCallbackFunc = luaFunc;
Intent signIntent = mGoogleSignInClient.getSignInIntent();
mActivity.startActivityForResult(signIntent,SDKHelper.REQ_CODE_GOOGLE_LOGIN);
}
3.3 登出
public void signOutGoogle(final String args,final int luaFunc) {
Log.d(TAG,"========= signOutGoogle === ");
mGoogleCallbackFunc = luaFunc;
// Firebase sign out
mFirebaseAuth.signOut();
// Google sign out
mGoogleSignInClient.signOut().addOnCompleteListener(mActivity,
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code",0);
String retStr = jsonObject.toString();
SDKHelper.runOnGL(mGoogleCallbackFunc,retStr);
} catch (JSONException e) {
e.printStackTrace();
}
mGoogleCallbackFunc = -1;
}
});
}
android代码
GoogleLogin.java
public class GoogleLoginHelper {
private final String TAG = getClass().getSimpleName();
private Cocos2dxActivity mActivity = null;
private GoogleSignInClient mGoogleSignInClient = null;
private FirebaseAuth mFirebaseAuth = null;
private int mGoogleCallbackFunc = -1;
public void initSDK(Cocos2dxActivity context){
mActivity = context;
// google 登陆
Log.d(TAG,"client_id == "+mActivity.getString(R.string.default_web_client_id));
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(mActivity.getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(context,gso);
// firebase 身份验证
mFirebaseAuth = FirebaseAuth.getInstance();
}
public void onStart(){
// FirebaseUser user = mFirebaseAuth.getCurrentUser();
// updateUI(user);
}
public void checkSign(final String args,final int luaFunc){
Log.d(TAG,"=========checkSign === ");
mGoogleCallbackFunc = luaFunc;
FirebaseUser user = mFirebaseAuth.getCurrentUser();
updateUI(user);
}
public void signGoogle(final String args,final int luaFunc){
Log.d(TAG,"========= signGoogle === reqcode ="+SDKHelper.REQ_CODE_GOOGLE_LOGIN);
mGoogleCallbackFunc = luaFunc;
Intent signIntent = mGoogleSignInClient.getSignInIntent();
mActivity.startActivityForResult(signIntent,SDKHelper.REQ_CODE_GOOGLE_LOGIN);
}
public void signOutGoogle(final String args,final int luaFunc) {
Log.d(TAG,"========= signOutGoogle === ");
mGoogleCallbackFunc = luaFunc;
// Firebase sign out
mFirebaseAuth.signOut();
// Google sign out
mGoogleSignInClient.signOut().addOnCompleteListener(mActivity,
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code",0);
String retStr = jsonObject.toString();
SDKHelper.runOnGL(mGoogleCallbackFunc,r