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/index.md.

Add native code

A native plugin adds your own Swift and Kotlin to the apps ng build --platform makes, without ejecting. Use one for what the runtime doesn't have:

  • A native module, which your code calls like the runtime's own. For example, a credential store with your own Keychain settings, or a scanner. See Write a native module.
  • A native element, drawn by the platform's own control. For example, a UISwitch and an Android Switch, a map or a camera preview. See Write a native element.

The builder still generates the native projects, with the shell: the Swift and Kotlin app around your bundle. On every build, it copies your sources into the projects and registers what they declare. So a new AngularLynx or Lynx version still arrives with an update.

Make a plugin

A plugin is a folder of platform sources. Put it anywhere in your project:

my-app/
  angular.json
  native/greeter/
    ios/GreeterModule.swift
    ios/NativeSwitch.swift
    ios/Greeter.xcassets/
    android/com/example/greeter/GreeterModule.kt
    android/com/example/greeter/NativeSwitch.kt
    android/res/values/colors.xml

Then describe it in the build target's nativePlugins. Each entry names the plugin's folder. For each platform, it says where the sources are and what to register from them:

angular.json
"options": {
  "nativePlugins": [
    {
      "path": "native/greeter",
      "ios": {
        "sourceDir": "ios",
        "modules": { "Greeter": "GreeterModule" },
        "elements": { "native-switch": "NativeSwitch" }
      },
      "android": {
        "packageName": "com.example.greeter",
        "sourceDir": "android",
        "modules": { "Greeter": "GreeterModule" },
        "elements": { "native-switch": "NativeSwitch" }
      }
    }
  ]
}

nativePlugins takes objects, as Angular's own assets and fileReplacements do. Because it's a builder option:

  • The builder's schema gives your editor completion and checks for it.
  • A build configuration (ng build --configuration) can override it, as it does any other option.

The keys

KeyWhat it is
pathThe plugin's folder, relative to the project root
sourceDirA folder inside the plugin that holds the platform's sources: ios or android by default
packageNameAndroid only: the Java package of the plugin's classes. A class named without a package is in it.
resDirAndroid only: the plugin's resources, laid out as a module's src/main/res
dependenciesAndroid only: the Maven libraries the plugin's sources use
repositoriesAndroid only: the Maven repositories they come from, beyond Google's and Maven Central
modulesThe name your code calls each module by, and its class
elementsThe tag your templates use for each element, and its LynxUI class
  • path: the folder's name is also the name of the folder its sources go in, in the generated projects. So two plugins need folders of different names.
  • sourceDir: every file in it goes into the generated project: Swift on iOS, Kotlin and Java on Android.
  • resDir: a folder inside the plugin, res in sourceDir by default.
  • modules: on iOS, a Swift class by its name alone. On Android, a class in packageName or a fully qualified one.
  • The folders, sourceDir and resDir, are relative to path.

The platform keys are those of Lynx Autolink's lynx.lib.json, plus modules and elements. So sourceDir and packageName mean what they mean there.

Autolink finds a library's modules and elements from markers in its sources (@LynxNativeModule, @LynxElement). Swift can't carry them, so a plugin names its classes in modules and elements instead.

Leave out what you don't need

  • An entry can leave out a platform. That platform then gets nothing from the plugin.
  • Without modules or elements, nothing is registered.
  • Without sourceDir or resDir, they take their defaults.
  • Without packageName, every Android class needs its package.

ng build --platform checks each entry before it builds. It names the entry's folder and the key of anything wrong.

Names that are taken

  • A module's name can't start with AngularLynx or Lynx, the prefixes of the shell's and Lynx's own modules.
  • An element's tag needs a hyphen, as a custom element's does. It takes lowercase letters, digits and hyphens only, and starts with a letter.
  • An element's tag can't be one already drawn by Lynx, XElement or the shell (scroll-view, list-item, blur-view, …). It also can't start with x- or lynx-, which Lynx's own elements use.

Name the plugin's types after it

Your Swift is compiled into the app with the shell's, as one module. A type with the same name as one of the shell's (HapticsModule, ShareModule, BlurView, DevServer, …) or another plugin's would clash. The build stops and names the file and the type.

  • Give every type a prefix of its own, helpers too, such as Greeter. Only private and fileprivate types can share a name, in different files.
  • Give Swift files names of their own too, whatever folder they're in. Swift takes a file's name once in a module. So a plugin can't have a Module.swift beside another plugin's, or an AppDelegate.swift beside the shell's.
  • On Android, keep the plugin's classes in a package of its own, never the shell's dev.angularlynx.shell.

Add resources

A plugin can bring colors, images, fonts and other files for its code, beside its sources.

On iOS

Put them in the iOS folder. Xcode builds what's there as it builds the app's own files:

  • An asset catalog (Greeter.xcassets, with color sets, image sets and data sets) is compiled with the app's into one. UIColor(named: "GreeterAccent") finds its color set, light and dark.
  • Any other file that isn't a source, such as a font or a JSON file, is copied to the top of the app, out of its folders. Bundle.main.url(forResource:withExtension:) finds it.
  • A file in an .lproj folder is localized as usual.

On Android

Put them in android/res, laid out as a module's src/main/res: values/colors.xml, values-night/colors.xml, color/, drawable/, font/, xml/, …

  • The builder copies the folder into the generated project as a resource folder of the app's, beside the shell's own.
  • So they're in the app's R, whose package is the shell's.
  • resDir names another folder, if yours isn't android/res.

A color state list in res/color/, using a color from res/values/colors.xml:

native/greeter/android/res/color/greeter_switch_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/greeter_accent" android:state_checked="true" />
    <item android:color="#FFECECEC" />
</selector>

The element's code reads it through the shell's R:

native/greeter/android/com/example/greeter/NativeSwitch.kt
import androidx.core.content.ContextCompat
import dev.angularlynx.shell.R

override fun createView(context: Context): Switch =
    Switch(context).apply {
        thumbTintList = ContextCompat.getColorStateList(context, R.color.greeter_switch_thumb)
    }

Name the resources after the plugin

An app has one asset catalog, one top folder and one R. Some names are already used by the shell (LaunchBackground, Info.plist, Assets.car, window_background, app_name, …) or by another plugin. A color set, file or resource with one of those names would replace it or fail the build.

  • The build stops before then, and names the file and the name.
  • Give them a prefix of your own, such as GreeterAccent and greeter_accent.
  • A plugin can define its own resource again under other qualifiers. That's how it has a night color.
  • On iOS, names differing only in capitals clash too, as a Mac's disk doesn't tell them apart.
  • On iOS, a string catalog (Greeter.xcstrings) can't share its table's name with a .strings or .stringsdict file.
  • On Android, name a resource file in lowercase, digits and _, as R names it: greeter_badge.xml, not GreeterBadge.xml.

Use a library on Android

List the Maven libraries your Kotlin uses in android.dependencies, as you'd write them in Gradle's implementation():

{
  "path": "native/pay",
  "android": {
    "packageName": "com.example.pay",
    "modules": { "Pay": "PayModule" },
    "dependencies": ["com.squareup.retrofit2:retrofit:2.11.0"],
    "repositories": ["https://maven.example.com/releases"]
  }
}
  • One version: group:artifact:version, with a :classifier if the library needs one. A dynamic version (3.+, latest.release) or a range is refused, as it would build a different app each time a new version came out.
  • The app depends on them. The plugin's sources are compiled into the app, so they see the libraries, and the libraries' own dependencies, as the shell's code sees its own.
  • Leave out @aar unless the library publishes no metadata. With it, Gradle fetches only that file and none of the library's own dependencies: list each of those yourself.
  • repositories: https URLs, for a library that Google's Maven repository and Maven Central, which the app already searches, don't have. They're searched after those two.
  • Two plugins can share a library at the same version. At different versions the build refuses them, rather than let Gradle build both with the higher one.

The shell's own libraries stay at their versions: Lynx, Fresco, OkHttp, SoLoader and Media3, with every other module of Fresco, OkHttp, SoLoader and Media3. A library that needs a newer one, itself or through its own dependencies, fails the build with Gradle's "Component is the target of multiple version constraints with conflicting requirements", naming the library. Use a version of the library that works with the shell's, which the generated app/build.gradle.kts lists. A library that asks for an older one gets the shell's, as Gradle gives any library the highest version asked for.

Swift packages on iOS come next.

Build and run

ng build --platform ios --run
ng build --platform android --run

What the next build does depends on what you changed:

  • A plugin's file: it writes the plugins into the generated project again and compiles what changed. It keeps the rest of the project, and Xcode's and Gradle's incremental state.
  • angular.json's native options, or adding or removing a plugin: it generates the project again.

The same is true for ng serve --platform the next time you start it.

Elsewhere, your plugin isn't there

Lynx Explorer and the web preview don't have it:

  • getNativeModule() may give you nothing. In the web preview, which isn't Lynx, it gives undefined. In a Lynx host, it gives either undefined or a module whose calls reject, as for any module the host lacks. Handle both, as the call does.
  • On Android, a registered tag with no element behind it stops the elements after it from rendering.
  • On the web, it's a plain inline element that shows its children.

Keep the screens that use your elements to the apps you build.

Android's release build runs R8

R8 removes and renames code it can't see used.

  • Lynx's own keep rules already keep every LynxModule and LynxUI subclass, with their @LynxMethod and @LynxProp members.
  • Code your plugin reaches only by name needs rules of its own, which plugins can't add yet.

What plugins can't do yet

  • Add native dependencies on iOS: Swift packages or frameworks. On Android, Maven libraries work.
  • Add entries to Info.plist, entitlements or AndroidManifest.xml. The permissions option covers the common permissions.
  • Register Objective-C classes in modules or elements.
  • Give an element a shadow node, so it measures itself.
  • Come from an npm package.

For any of those today, eject. An ejected project keeps your plugins' files and registration, and you take them on from there. nativePlugins no longer applies to it, and the build warns if it's still set.