我目前在 React Native 方面有些挣扎。 我想要的是一个基于 React Native 的库项目,然后我可以将其包含在我的一些应用程序中。
我尝试的第一件事是将所有基本的 React 设置代码 [1] 移动到现有应用程序内的 fragment 中。这没有任何问题。
然后我开始了一个全新的 Android 项目,其中包含一个空的 Activity。 在这个项目中,我添加了一个新的 Android Library 项目模块,它应该包含“react native fragment”。然后我在我的根项目文件夹中初始化了 React Native 项目。
我目前的项目结构是这样的:
ReactApplication
- basic files (index.android.js, index.ios.js, package.json, build.gradle, settings.gradle)
- app/ (contains the real native android app which includes the "react native fragment")
- reactlib/ (library project which contains a fragment with the code from [1])
- ios/
- node_modules/ (contains react-native)
通过 npm start
启动开发服务器没有任何问题,但是一旦我启动 Android 应用程序,我就会得到 followwing logcat 输出:
W/unknown:React(26957): Unable to dispatch keyboard events in JS as the react instance has not been attached
W/unknown:React(26957): You seem to be running on device. Run 'adb reverse tcp:8081 tcp:8081' to forward the debug server's port to the device.
E/(26957): Unable to load script from assets: index.android.bundle
E/ReactNative(26957): Got JS Exception: ReferenceError: Can't find variable: require
请注意,我执行了 adb reverse tcp:8081 tcp:8081
操作,但错误仍然出现。
稍后我想从 Android Library 项目创建一个 aar 并通过 maven 访问它。
我做错了什么?还是我想做的根本不可能?
[1] https://facebook.github.io/react-native/docs/embedded-app-android.html#add-native-code
最佳答案
在调试并尝试了一些东西之后,我发现它无法在 aar 中 bundle 时从服务器加载 bundle 文件。
如果你在 android assets 文件夹中添加 index.android.bundle,它就会开始工作。
关于带有 React Native 的 Android 库项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33254641/