使用React Native的Android库项目

时间:2015-10-21 08:41:03

标签: android react-native

我目前正在与本地人做出反应。 我想要的是一个基于react native的库项目,然后我可以将其包含在我的一些应用程序中。

我尝试的第一件事就是将所有基本的反应设置代码[1]移动到现有应用程序内的Fragment中。这没有任何问题。

然后我开始了一个全新的Android项目,其中包含一个空的Activity。 在这个项目中,我添加了一个新的Android库项目模块,该模块应包含"反应原生片段"。我在我的根项目文件夹中初始化了react本机项目。

我目前的项目结构如下:

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应用程序,我就会得到以下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库项目中创建一个aar并通过maven访问它。

我做错了什么?或者我想做什么根本不可能?

[1] https://facebook.github.io/react-native/docs/embedded-app-android.html#add-native-code

1 个答案:

答案 0 :(得分:5)

经过调试和尝试了几件事之后,我发现在捆绑到aar时它无法从服务器加载捆绑文件。

如果你在android资源文件夹中添加index.android.bundle,它就会开始工作。