Linking a your own library with native code on React Native - react-native

I build my own library for react-native and I want to make it available to the public. However, my library have some native implementation.
I want to use make the linking through react-native link avaialable, but I don't know how to start.
I have been looking on the RPNM project but I can't figure how to add it.
Today I basically have to do everything manually. Adding the compile dependencies on the build.gradle files.
How to automate my library linking using react-native link?

Related

react native how to know if a repo I am importing is a native module

my question might be a little bit silly or ambiguous since I am fairly new to react native.
I'm trying to use the following repository for my react-native project https://github.com/smekalka/react-native-universal-pedometer. I have noticed that the repo is implemented in .java with platform folder unlike the regular .js or .ts files I used to see. Is this repository considered native module as react native doc describe?
Or in general how I can tell the whatever lib I am using is a native module.
The project is previously tested are under the support of expo-cli. I experienced the error null is not n object while using this repo. If so, I am probably going to eject the expo-cli and rewrite my code so I can use and even create own native-module for full control, some core implementations that written in other languages or expo-cli does not support.
Yes, the android and ios directories in the repository contain the 'native' code used to implement the platform-specific hooks that the Javascript will be able to pick up. Expo is not able to use these native modules or native code so your assumption is correct; you will need to eject your app in order to use this module.
If your app is below version 0.60 of React Native, after installing the module you will need to run react-native link react-native-universal-pedometer to link the native code to the Javascript runtime. If you're above 0.60, it will link automatically when installed.

When to use react-native link?

I understand that react-native link (see post) is an automatic way to install native dependencies. The post above explain how to use the link command but lacking the detail of when to use it. Should it be used after adding a component, every code change or after introducing new module to the app?
Why we use react-native-link??
In this post, I will explain why we use react native link command and when we need to use this command or not?
First, we will understand what is native module??
Native Modules
Native modules are usually distributed as npm packages, apart from the typical javascript files and resources they will contain an Android and ios library project. 
React Native provides an impressive amount of native modules that give you direct access to core platform APIs on both Android and IOS.
For example react-native-maps, react-native-firebase, react-native-socketio etc.
These modules or packages contained both platform (Android and Ios) code.
Now coming on to the react native link
Those libraries which are use native code, in that case you'll have to add these files to your app. For linking those library with react native code need to run react-native-link
Here are the few steps to link your libraries that contain native code
Automatic linking
Step 1
Install a library with native dependencies:
$ npm install --save
Step 2
Link your native dependencies:
$ react-native link
Done! All libraries with native dependencies should be successfully linked to your iOS/Android project.
Where we don't use react-native-link??
Those components which are only written in javascript they are not using any native code (Android and Ios). There is no need to run react-native-link.
For example rn-modal-picker, react-native-check-box etc.
You should check out this other answer: The use of react-native-link command?
You only need to run react-native link NAME_OF_PACKAGE when you install a new package that has a native codebase, or without arguments if you want to do it for multiple of them.

Is it necessary to link every react-native library after installation?

There are number of react-native libraries which makes developer job easy and it's a common practice to link library after it's installation. But for some libraries linking is not done(As those library developers are not asking to do so). For example react-native-swipeable for which linking was not asked on their git hub page. Another package native-base for which linking is mandatory but there was an error when i tired to do so, But still package is working fine.
Can somebody let me know the consequences that we face if any package is not linked?
Some libraries make use of native code, so linking should add to your project all native dependecies. For instance, react-native-swipeable might be a pure JS library and no linking is used. Which means no native code is used by react-native-swipeable.
For more information on linking please refer to: https://facebook.github.io/react-native/docs/linking#docsNav

Using React-Native library inside another React-Native library

I created a React-Native application
Then I created a React-Native Library using this seed github.com/frostney/react-native-create-library
Now, I need to use this package https://github.com/toystars/react-native-geo-fence
Inside of the Library.
Problem is: The library is not a react-native application, and the geo-fence lib needs linking.
I cant change the React-Native App and can not use the geo-fence directly inside of it, of course.
The logic will need to stay inside the Library i created.
When i linked the Library i created (with the geo fence package on it already), with the App i created, the geo-fence gradle settings and android configurations didn't happen at the APP.
this concerns Android only
It is not possible to link a react-native library to another react-native library.
So the correct procedure is to add any third party react-native library, that requires link, that you want to use inside your own react-native library, as peerDependency on package.json.
This way the third party lib will be installed directly at the app, and will be linked, necessarily to the app, but the logic using the third party lib will be inside your own.
So you can create a lib, add react-native-geo-fence as peerDependency of your lib, then code as you wish.
After this, the app using your lib, will have to install it and link manually the react-native-geo-fence, then that's it.

How include aar whitch has JNI so file in React Native project

I have wrote a project by React Native,now I need to reference a android SDK to implement some functions.
the android sdk is a aar file,and it contains JNI,now it reports couldn't find "libjniload.so", the "libjniload.so" is in the aar.
If you want to reference an Android SDK, you must add the files to your android project in your react native project. That means, you must add the dependencies to your build.gradle, and copy the files to you libs folder etc.. This is not any different than adding .aar files to an Android project. After this, you will need to implement a native module to call its functions from react-native. This is the general procedure, what exact step is causing error?