海外SDK接入2 Firebase + googleLogin

本文档详细介绍了如何将Android应用接入Firebase和GoogleLogin,包括下载`google-services.json`文件,配置build.gradle,以及初始化和登录过程。在实施过程中遇到了权限异常、自定义视图初始化错误和API错误,通过排查解决了这些问题,确保了SDK的成功接入。
摘要由CSDN通过智能技术生成

准备工作

官方文档: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
google play service最新版本,基于firebasesdk,9.2.0.包含了 全部需要的jar,另外还有aar版本 play-services-ads-9.2.0.jar play-services-analytics-9.2.0.jar play-services-analytics-impl-9.2.0.jar play-services-appindexing-9.2.0.jar play-services-appinvite-9.2.0.jar play-services-appstate-8.4.0.jar play-services-auth-9.2.0.jar play-services-auth-base-9.2.0.jar play-services-base-9.2.0.jar play-services-basement-9.2.0.jar play-services-cast-9.2.0.jar play-services-cast-framework-9.2.0.jar play-services-clearcut-9.2.0.jar play-services-contextmanager-9.2.0.jar play-services-drive-9.2.0.jar play-services-fitness-9.2.0.jar play-services-games-9.2.0.jar play-services-gass-9.2.0.jar play-services-gcm-9.2.0.jar play-services-identity-9.2.0.jar play-services-iid-9.2.0.jar play-services-location-9.2.0.jar play-services-maps-9.2.0.jar play-services-measurement-8.4.0.jar play-services-nearby-9.2.0.jar play-services-panorama-9.2.0.jar play-services-places-9.2.0.jar play-services-plus-9.2.0.jar play-services-safetynet-9.2.0.jar play-services-tagmanager-9.2.0.jar play-services-tagmanager-api-9.2.0.jar play-services-tasks-9.2.0.jar play-services-vision-9.2.0.jar play-services-wallet-9.2.0.jar play-services-wearable-9.2.0.jar play-services-ads-lite-9.2.0.jar firebase-analytics-9.2.0.jar firebase-analytics-impl-9.2.0.jar firebase-auth-9.2.0.jar firebase-auth-common-9.2.0.jar firebase-auth-module-9.2.0.jar firebase-common-9.2.0.jar firebase-config-9.2.0.jar firebase-crash-9.2.0.jar firebase-database-9.2.0.jar firebase-database-connection-9.2.0.jar firebase-iid-9.2.0.jar firebase-messaging-9.2.0.jar firebase-storage-9.2.0.jar firebase-storage-common-9.2.0.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值