嗨,我对 React Native Firebase 有疑问

Posted

技术标签:

【中文标题】嗨,我对 React Native Firebase 有疑问【英文标题】:Hi, I have a problem with React Native Firebase 【发布时间】:2021-10-28 04:52:46 【问题描述】:

我按照网站https://rnfirebase.io/ 上的说明做了所有事情 但是当应用程序启动时,错误会导致图像崩溃

错误:您试图通过调用 firebase.app() 来使用未安装在您的 android 项目上的 firebase 模块。

确保您有:

    在“MainApplication.java”文件中导入了“io.invertase.firebase.app.ReactNativeFirebaseAppPackage”模块。

    在 RN 'getPackages()' 方法列表中添加了 'new ReactNativeFirebaseAppPackage()' 行。

有关完整设置说明,请参阅 http://invertase.link/android。

import database from '@react-native-firebase/database'


export default function MainPage() 
    const reference = database().ref('/dialogs');
    console.log(reference)
    return (
        <View style=styles.mainPage>
         <Text>Hello</Text>
        </View>
    );

我的文件 MainApplication.java

package noorsoftMobile.com;

import android.app.Application;
import android.content.Context;
import android.net.Uri;

import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import noorsoftMobile.com.generated.BasePackageList;

import org.unimodules.adapters.react.ReactAdapterPackage;
import org.unimodules.adapters.react.ModuleRegistryAdapter;
import org.unimodules.adapters.react.ReactModuleRegistryProvider;
import org.unimodules.core.interfaces.Package;
import org.unimodules.core.interfaces.SingletonModule;
import expo.modules.updates.UpdatesController;

import com.facebook.react.bridge.JSIModulePackage;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;


public class MainApplication extends Application implements ReactApplication 
  private final ReactModuleRegistryProvider mModuleRegistryProvider = new ReactModuleRegistryProvider(
    new BasePackageList().getPackageList()
  );

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) 
    @Override
    public boolean getUseDeveloperSupport() 
      return BuildConfig.DEBUG;
    

    @Override
    protected List<ReactPackage> getPackages() 
      List<ReactPackage> packages = new PackageList(this).getPackages();
      packages.add(new ModuleRegistryAdapter(mModuleRegistryProvider));
      return packages;
    

    @Override
    protected String getJSMainModuleName() 
      return "index";
    

    @Override
    protected JSIModulePackage getJSIModulePackage() 
      return new ReanimatedJSIModulePackage();
    

    @Override
    protected @Nullable String getJSBundleFile() 
      if (BuildConfig.DEBUG) 
        return super.getJSBundleFile();
       else 
        return UpdatesController.getInstance().getLaunchAssetFile();
      
    

    @Override
    protected @Nullable String getBundleAssetName() 
      if (BuildConfig.DEBUG) 
        return super.getBundleAssetName();
       else 
        return UpdatesController.getInstance().getBundleAssetName();
      
    
  ;

  @Override
  public ReactNativeHost getReactNativeHost() 
    return mReactNativeHost;
  

  @Override
  public void onCreate() 
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);

    if (!BuildConfig.DEBUG) 
      UpdatesController.initialize(this);
    

    initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  

  /**
   * Loads Flipper in React Native templates. Call this in the onCreate method with something like
   * initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
   *
   * @param context
   * @param reactInstanceManager
   */
  private static void initializeFlipper(
      Context context, ReactInstanceManager reactInstanceManager) 
    if (BuildConfig.DEBUG) 
      try 
        /*
         We use reflection here to pick up the class that initializes Flipper,
        since Flipper library is not available in release mode
        */
        Class<?> aClass = Class.forName("noorsoftMobile.com.ReactNativeFlipper");
        aClass
            .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
            .invoke(null, context, reactInstanceManager);
       catch (ClassNotFoundException e) 
        e.printStackTrace();
       catch (NoSuchMethodException e) 
        e.printStackTrace();
       catch (IllegalAccessException e) 
        e.printStackTrace();
       catch (InvocationTargetException e) 
        e.printStackTrace();
      
    
  

我的 package.json


  "scripts": 
    "start": "react-native start",
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "web": "expo start --web",
    "eject": "expo eject"
  ,
  "dependencies": 
    "@react-native-firebase/app": "^12.7.3",
    "@react-native-firebase/auth": "^12.7.3",
    "@react-native-firebase/database": "^12.7.3",
    "expo": "~42.0.1",
    "expo-splash-screen": "~0.11.2",
    "expo-status-bar": "~1.0.4",
    "expo-updates": "~0.8.1",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-native": "~0.63.4",
    "react-native-gesture-handler": "~1.10.2",
    "react-native-reanimated": "~2.2.0",
    "react-native-screens": "~3.4.0",
    "react-native-unimodules": "~0.14.5",
    "react-native-web": "~0.13.12"
  ,
  "devDependencies": 
    "@babel/core": "^7.9.0"
  ,
  "private": true,
  "name": "noorsoft-intership-development-version-mobile",
  "version": "1.0.0"

【问题讨论】:

Please do not upload images of code/errors when asking a question. 那么您是否按照说明进行操作?你做了两次吗? @JoachimSauer 我做了很多尝试 酷酷酷酷。你能展示其中任何一个吗?你的MainApplication.java 文件是什么样的? @JoachimSauer 是的,我在上面添加了它 【参考方案1】:

您似乎正在使用 react-native-firebase 包的 v5。你能检查一下你的 package.json 看看是不是这样吗?

根据您的错误消息,它说您需要修改 MainApplication.java 文件,根据文档,该文件是库的 v5 中的 firebase/database 所必需的,但 v6 中不需要。

因此,如果您使用的是 v5,则可以关注库文档中的 migrating to v6。

【讨论】:

我在问题本身中添加了包 json @Peoples 你能同时包含你的 build.gradle 文件和 settings.gradle 吗? 我的项目github.com/MRXOneX/noorsoft-mobile 几乎干净 @Peoples 找到了。尝试在“app/build.gradle”中删除这两行。 implementation platform('com.google.firebase:firebase-bom:28.4.0') implementation 'com.google.firebase:firebase-analytics' 然后,运行 gradle clean (rnfirebase.io/migrating-to-v6#cleaning-gradle)

以上是关于嗨,我对 React Native Firebase 有疑问的主要内容,如果未能解决你的问题,请参考以下文章

react-native-firebase_messaging:compileReleaseJavaWithJavac

getInitialLink 不起作用,但 onLink 在 react-native-firebase v6 中工作

React Native - FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created

如何缩小 React-Native 地图

React Native:悬停图片onPress

用于 android 的 React Native Charts