Katharine Osborne
Katharine Osborne

Reputation: 7039

How to include an external library in a React native project in Android Studio?

I'm porting a web app over to a React app in Android Studio. I'm using an external library (this one: https://trackingjs.com/). And as I'm fairly new to Android Studio and React, I'm sure how to do it correctly. In my webapp I'm just including it with a script tag.

I have the library saved into my project folder, in the same directory as my index.android.js file. I would prefer not to alter the code in tracking.js in any way. I'm not sure how to proceed. This must be a common problem but I've had little luck finding anything that works.

Upvotes: 1

Views: 4344

Answers (2)

Katharine Osborne
Katharine Osborne

Reputation: 7039

Based on @liminal18's comments, this is what worked for me, using Tracking.js:

$ npm install tracking

on the command line, then in index.android.js

import {
Tracking
} from 'tracking';

This was just to get the library included without errors. Since Tracking.js is web-based, there might be issues actually using it, but doing the above solved the problem of including an external library.

Upvotes: 1

liminal18
liminal18

Reputation: 563

If it is available from npm you can do npm install packageName --save and then use import Package from 'packages.js'

Please do keep in mind react native is not quite javascript and some browser specific functions will not transpile in other words it is possible the plugin you are using will not work in react native.

Btw no need for android studio except for starting the emulated device to run react native in. You should be able to react-native run-android and provided you set up the emulated device correctly it will start the package manager and install your app on it and then refresh on save. Are you using the react native cli?

Btw in your web app you should be able to include it using let tracking = require("tracking.js") or using the import style above. If you do it should be concatenated into your main.js file and then minified which might help. Better to have one download than several.

Upvotes: 2

Related Questions