For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/native-plugins/modules.md.

Write a native module

A native module is a Swift or Kotlin class your Angular code calls like the runtime's own modules. Add it to a native plugin, then call it with getNativeModule().

Write the module

A module's methods take and return what JSON can hold, and functions for callbacks.

Lynx calls them on a thread of its own. So a method that touches the UI moves to the platform's UI thread first.

On iOS

A module is a Swift class that adopts LynxModule:

native/greeter/ios/GreeterModule.swift
import Lynx
import UIKit

final class GreeterModule: NSObject, LynxModule {
  static var name: String { "Greeter" }

  static var methodLookup: [String: String] {
    ["greet": NSStringFromSelector(#selector(greet(_:)))]
  }

  private let host: AngularLynxPluginHost?

  override init() {
    host = nil
    super.init()
  }

  // Swift can't import Lynx's optional initWithParam: as optional.
  init(param: Any) {
    host = param as? AngularLynxPluginHost
    super.init()
  }

  @objc func greet(_ name: String) -> String {
    "Hello, \(name), from iOS \(UIDevice.current.systemVersion)"
  }
}
  • methodLookup maps each method's JavaScript name to its selector.
  • The shell creates it with an AngularLynxPluginHost as its param. The host's viewController is the view controller showing the app. Use it for a module that presents a screen of its own.
  • The host holds the controller weakly. Lynx keeps a module's param as long as the view, which the controller keeps.

On Android

A module extends LynxModule and marks its methods @LynxMethod. The shell creates it with its activity as the param:

native/greeter/android/com/example/greeter/GreeterModule.kt
package com.example.greeter

import android.content.Context
import android.os.Build
import com.lynx.jsbridge.LynxMethod
import com.lynx.jsbridge.LynxModule

class GreeterModule(context: Context, param: Any?) : LynxModule(context, param) {
    @LynxMethod
    fun greet(name: String): String = "Hello, $name, from Android ${Build.VERSION.RELEASE}"
}

Register it

Name the module and its class in the plugin's modules, for each platform:

angular.json
"ios": { "modules": { "Greeter": "GreeterModule" } },
"android": {
  "packageName": "com.example.greeter",
  "modules": { "Greeter": "GreeterModule" }
}

See Make a plugin for the rest of the entry, and the names you can't use.

Call it

Describe the module in NativeModuleMap, and getNativeModule() types it:

src/app/greeter.ts
export {};

declare module '@blotch/angular-lynx' {
  interface NativeModuleMap {
    Greeter: { greet(name: string): string };
  }
}

Import the file where you call the module, so it's in the compilation. From Angular's code, each call returns a promise:

import './greeter';

// undefined where the host has no Greeter module: no module, or a call
// that rejects.
const greeting = await inject(LynxNativeModule)
  .getNativeModule('Greeter')
  ?.greet('Ada')
  .catch(() => undefined);

Handle a missing module. Lynx Explorer and the web preview don't have your plugin. There, getNativeModule() gives undefined, or a module whose calls reject. The ?. and .catch() above cover both. See Elsewhere, your plugin isn't there.