带有React Native的Android库项目 [英] Android Library Project with React Native

查看:69
本文介绍了带有React Native的Android库项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在与本机反应方面有些挣扎.我想拥有一个基于react native的图书馆项目,然后可以将其包含在我的某些应用中.

I'm currently struggling a bit with react native. What I want to have is a library project based on react native which I can then include in some of my apps.

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

First thing I tried was to move all the basic react setup code [1] into a Fragment inside an existing app. This worked without any problems.

然后,我用一个空的Activity启动了一个全新的Android项目.在这个项目中,我添加了一个新的Android Library项目模块,其中应包含反应本机片段".然后,我在根项目文件夹中初始化了react native项目.

Then I started a completely new Android project with an empty Activity. Into this project I added a new Android Library project module which should contain the "react native fragment". I initialised the react native project in my root project folder then.

我当前的项目结构如下:

My current project structure looks like this:

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输出:

Starting the dev-server via npm start works without any problems but as soon as I start the Android app I get the folowwing logcat outputs:

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反向tcp:8081 tcp:8081 事情,该错误仍然显示.

Please note that I did the adb reverse tcp:8081 tcp:8081 thing and the error is still showing up.

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

Later I want to create an aar out of the Android Library project and access it via maven.

我做错了什么?还是我想做的事根本不可能?

What am I doing wrong? Or is what I want to do simply not possible?

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

推荐答案

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

After debugging and trying out couple of things, i figured that it fails to load the bundle file from server when bundled in aar.

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

If you add index.android.bundle in android assets folder, it starts working.

这篇关于带有React Native的Android库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆