How React Native Link Command Works and Implementation in mand-mobile-rn

The article details the inner workings of the react-native link command and the rnpm system, illustrates the process with react-native-code-push, and explains how mand-mobile-rn links three custom Android modules by using postlink scripts, applyPatch.js, natives.json, and the required Gradle and package registrations.

Didi Tech
Didi Tech
Didi Tech
How React Native Link Command Works and Implementation in mand-mobile-rn

This article explains how the React Native link command works and how mand-mobile-rn implements multiple Android module linking. It covers the traditional React Native library linking mechanism, the rnpm (react-native package manager) system, and how mand-mobile-rn uses similar principles to link multiple native Android modules.

The article is structured in four main parts:

1. How the react-native link command works: It explains that the link command only looks for android/app or android folders in library projects to provide external dependencies. The source code from react-native-cli shows that it first checks for android/app, then android folder.

2. React Native Code Push as an example: The article uses react-native-code-push as an example of how rnpm works. It shows how the package.json includes rnpm configuration with commands like postlink that can execute scripts after linking. This allows for custom code injection during the linking process.

3. mand-mobile-rn implementation: The article explains how mand-mobile-rn implements linking for three native Android modules (ImagePicker, RefreshControl, NumberKeyboard). Since these modules are not in the traditional android folder structure, they use the postlink command to write code to necessary files.

4. Summary: The article concludes by explaining how rnpm simplifies React Native library development by allowing complex dependency management through scripts.

The implementation details include:

- Three steps to register a native module: adding to settings.gradle, declaring dependency in build.gradle, and registering in ReactNativeHost.getPackages()

- Using applyPatch.js as a utility to write code to files

- Using natives.json to declare module information

- Using postlink.js to sequentially write to settings.gradle, app/build.gradle, and MainApplication

The article provides practical insights into React Native library development and module linking mechanisms.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Android DevelopmentReact Nativemand-mobile-rnModule LinkingNative Modulesrnpm
Didi Tech
Written by

Didi Tech

Official Didi technology account

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.