Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Library Project with 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.

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.

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)

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

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

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

like image 330
astuetz Avatar asked Oct 21 '15 08:10

astuetz


1 Answers

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

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

like image 65
Shailesh Avatar answered Sep 20 '22 15:09

Shailesh