google 登录崩溃 Apk(React Native)

Posted

技术标签:

【中文标题】google 登录崩溃 Apk(React Native)【英文标题】:google Sign-in Crashing Apk(React Native) 【发布时间】:2021-12-29 10:19:00 【问题描述】:

我正在尝试在 React Native 中实现 Google 登录。 expo 不支持我实现登录的方式,它仅在生产中受支持,但是当我构建 apk 并尝试在我的 android 应用程序上运行它时,应用程序仍然崩溃,有没有办法查看与崩溃相关的错误?

这也是我的代码,以防有人发现错误:

import React,  useState, useEffect  from "react";
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as GoogleSignIn from "expo-google-sign-in";
import axios from "axios";
import  StyleSheet, Text, View, Platform  from "react-native";
import RootStack from "./app/navigators/RootStack";
import LoginScreen from "./app/screens/LoginScreen";

export default function App() 
  const [login, setLogin] = useState(0);
  const [alertmebad, setalertmebad] = useState("");
  const [alertmegood, setalertmegood] = useState("");
  const [googleSubmitting, setgoogleSubmitting] = useState();
  //setting the login key
  const storeData = async (value) => 
    try 
      await AsyncStorage.setItem("@login_key", value);
     catch (e) 
      // saving error
    
  ;

  // THIS IS WHERE THE CODE STARTS______________________________________________________
  useEffect(() => 
    initAsync();
    checklogin();

  );
  const androidClientId =
    "MY_ANDROID_API_KEY";
  const iosClientId =
    "MY_IOS_API_KEY";

  const initAsync = async () => 
    try 
      await GoogleSignIn.initAsync(
        clientId: Platform.os === "android" ? androidClientId : iosClientId,
      );
      getUserDetails();
     catch (e) 
      console.log(e);
    
  ;

  const getUserDetails = async () => 
    const user = await GoogleSignIn.signInSilentlyAsync();

    
  ;

  const handleGoogleSignin = async () => 
    try 
      await GoogleSignIn.askForPlayServicesAsync();
      const  type, user  = await GoogleSignIn.signInAsync();
      if (type == "success") 
        // getUserDetails();
        setalertmegood("Signin was success");
        setalertmebad("");
        const  email, name, photoUrl  = user;
        signup( name: name, email: email, password: "sfgfgsfg" );

        setTimeout(() => 
          storeData(JSON.stringify( email, name, photoUrl ));
          // console.log(JSON.stringify( email, name, photoUrl));
          // navigation.navigate("HomeScreen",  email, name, photoUrl );
          checklogin();
        , 1000);
       else 
        setalertmebad("Signin had an error");
        setalertmegood("");
      
     catch (error) 
      setalertmebad(error);
      setalertmegood("");
    
  ;

  // THIS IS WHERE THE CODE ENDS______________________________________________________

  const signup = (credentials) => 
    console.log(credentials);
    const url = "https://blooming-escarpment-74540.herokuapp.com/user/signup";
    axios
      .post(url, credentials)
      .then((response) => 
        const result = response.data;
        const  message, status, data  = result;

        if (status !== "SUCCESS") 
          console.log("if: " + message, status);
         else 
          // console.log(...data[0]);
          console.log("else: " + message, status);
        
      )
      .catch((error) => 
        console.log("catch:" + error);
      );
  ;


  // to check if someone is logged in already
  const checklogin = async () => 
    try 
      const jsonValue = await AsyncStorage.getItem("@login_key");
      jsonValue != null ? setLogin(1) : setLogin(0);
     catch (e) 
      console.log(e);
    
    // storeData(null)
    // setLogin(0);
  ;

  
  return login == 1 ? (
    <RootStack />
  ) : (
    <LoginScreen
      onPress=() => handleGoogleSignin()
      alertmeBad=alertmebad
      alertmeGood=alertmegood
    />
  );


const styles = StyleSheet.create(
  container: 
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  ,
);

【问题讨论】:

将“await”添加到代码的第 36 行。您必须等待 Async 完成然后调用其他函数。init 应用仍然崩溃 你的 app.json 配置对了吗? configure 。白屏通常表示代码中有错误。查看如何在生产中调试production-errors 另外,您可以使用sentry 进行崩溃报告 你能在这里附上apk吗?我要测试 【参考方案1】:

当我们使用 google 登录时,我们收到此错误 Error: Objects are not valid as a React child (found: Error: GoogleSignIn.null: DEVELOPER_ERROR).

世博解决方案:

问题已解决。

    从 Expo 获取 Android 哈希
expo fetch:android:hashes
    复制Google Certificate Fingerprint并保存在app.json

    "expo": 
        "android": 
            "config": 
                "googleSignIn": 
                    "certificateHash": "<Google Certificate Fingerprint>"
                
            
        
    

    在 Firebase 控制台的 Android 应用设置中添加相同的指纹

React Native 解决方案:

您需要将 SHA1 密钥添加到 Firebase 控制台配置。 你可以这样做:

Firebase 控制台 (https://console.firebase.google.com) -> 您的项目 -> 配置 -> 滚动到

您可以在 Android Studio 中找到运行“签名报告”的 SHA1 密钥:

然后,查看“运行选项卡”并单击按钮:

我认为这是更简单的方法。希望对您有所帮助。

如果您想要更多解决方案,可以查看Thread

【讨论】:

以上是关于google 登录崩溃 Apk(React Native)的主要内容,如果未能解决你的问题,请参考以下文章

React Native 版本 .apk 在更高版本的 android 设备中崩溃,地理定位权限被拒绝错误

react-native APK 崩溃并出现“未知”错误

使用 Google Maps API v2 时,签名的 APK 崩溃,未签名的 APK

Expo React Native Bare 发布 APK 在启动时立即崩溃

询问位置权限时,React Native Expo 应用程序在 android apk 文件上崩溃

将 React Native 发布到 Google Play 控制台会生成旧的 apk