使用React Native的Android库项目

ast*_*etz 7 android react-native

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

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

然后我开始了一个全新的Android项目,其中包含一个空的Activity.在这个项目中,我添加了一个新的Android库项目模块,该模块应包含"react native fragment".我初始化了根项目文件夹中的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)
Run Code Online (Sandbox Code Playgroud)

通过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
Run Code Online (Sandbox Code Playgroud)

请注意我做了这adb reverse tcp:8081 tcp:8081件事,错误仍然出现.

后来我想从Android库项目中创建一个aar并通过maven访问它.

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

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

小智 5

在调试并尝试了几件事之后,我发现当将其捆绑在aar中时,它无法从服务器加载捆绑文件。

如果您在android资产文件夹中添加index.android.bundle,它将开始工作。

  • 如果index.android.bundle不存在? (2认同)