When developing Lynx applications, you may encounter scenarios where you need to interact with native platform APIs not covered by Lynx. Or, you might want to reuse existing native platform code in your Lynx application. Regardless of the reason, you can use Native Modules to seamlessly connect your JavaScript code with native code, allowing you to call native platform functions and APIs from your JavaScript code. The following will detail how to write a native module.
The basic steps for writing a native module are as follows:
Use TypeScript to declare your typed interface specification.
Use your interface specification to write your Lynx application code.
Follow your interface specification to write your native platform code and connect your native code to the Lynx runtime environment.
Next, this guide will demonstrate these steps through an example of building a native module.
This guide aims to show you how to write a local persistent storage module that enables your Lynx application to use JavaScript code to store data persistently locally.
To implement local persistent storage on mobile devices, you need to use the native APIs of Android and iOS:
The interface specification of a native module serves as a bridge between the native code and the Lynx JavaScript runtime, defining the methods and data types passed between them.
The steps to declare an interface specification are as follows:
Create a Lynx project: Refer to the Create a Lynx Project guide to create your Lynx project.
Create a new type declaration file: Create a new file named src/typing.d.ts in your Lynx project.
Implement the interface specification: Implement the interface specification of the native module in the typing.d.ts file.
INFO
You can view the types available in the specification and their corresponding native types in the Type Mapping Table.
The following is the implementation of the interface specification for the local persistent storage module:
NativeModules is a global built-in object provided by Lynx in the JavaScript runtime. It serves as the access point for all native modules, and all native module declarations must be defined within it.
Next, write your application code in src/App.tsx within your Lynx project.
The following is the App.tsx for the local persistent storage module. It includes an area to display the content read from local storage and three buttons for reading, writing, and clearing local storage.
First, follow the Build Lynx Explorer for iOS guide to create a Lynx Explorer project locally and open it with Xcode.
Next, right-click on the modules folder in the Lynx Explorer project and select New File... to create the header and source files for the native module.
Then, use the Cocoa Touch Class template.
Name the class NativeLocalStorageModule. You can choose to create it in either Objective-C or Swift. Then click Next to complete the file creation.
You need to implement an additional static method name in the native module to return the exported name of your native module. Also, implement an additional static method methodLookup in the native module to map the names of the methods to be exported to their corresponding selectors.
Next, you need to register your native module into the Lynx runtime environment.
Add the following registration code to the setupLynxEnv method in the explorer/darwin/ios/lynx_explorer/LynxExplorer/LynxInitProcessor.m file of the Lynx Explorer project to register your native module with the global configuration of the Lynx runtime environment.
Next, create a new NativeLocalStorageModule.java or NativeLocalStorageModule.kt file in the explorer/android/lynx_explorer/src/main/java/com/lynx/explorer/modules/ path of the Lynx Explorer project. Then, inherit from LynxModule to implement the NativeLocalStorageModule native module.
INFO
You need to add the @LynxMethod annotation to the methods that need to be exported in the native module.
Next, you need to register your native module with the Lynx runtime environment.
Add the following registration code to the Init method in the explorer/android/lynx_explorer/src/main/java/com/lynx/explorer/modules/LynxModuleAdapter.java file of the Lynx Explorer project to register your native module with the Lynx runtime environment. Here, you need to specify the name of the native module you are exporting, which must be consistent with your interface specification.
You will see a QR code in the console. Use Lynx Explorer to scan the QR code to open the page.
Congratulations! You have successfully created a native module in Lynx Explorer! If you want to create a native module in your application, you first need to integrate Lynx by referring to the Integrate with Existing Apps guide, and then follow the steps above to create the native module.
Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.