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
UISwitchand an AndroidSwitch, 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:
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:
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
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,resinsourceDirby default.modules: on iOS, a Swift class by its name alone. On Android, a class inpackageNameor a fully qualified one.- The folders,
sourceDirandresDir, are relative topath.
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
modulesorelements, nothing is registered. - Without
sourceDirorresDir, 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
AngularLynxorLynx, 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 withx-orlynx-, 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. Onlyprivateandfileprivatetypes 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.swiftbeside another plugin's, or anAppDelegate.swiftbeside 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
.lprojfolder 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. resDirnames another folder, if yours isn'tandroid/res.
A color state list in res/color/, using a color from res/values/colors.xml:
The element's code reads it through the shell's R:
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
GreeterAccentandgreeter_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.stringsor.stringsdictfile. - On Android, name a resource file in lowercase, digits and
_, asRnames it:greeter_badge.xml, notGreeterBadge.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():
- One version:
group:artifact:version, with a:classifierif 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
@aarunless 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
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 givesundefined. In a Lynx host, it gives eitherundefinedor 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
LynxModuleandLynxUIsubclass, with their@LynxMethodand@LynxPropmembers. - 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 orAndroidManifest.xml. Thepermissionsoption covers the common permissions. - Register Objective-C classes in
modulesorelements. - 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.
Related
- Write a native module
- Write a native element
- Show an element's children in a sheet
nativePluginsand the other builder options- The Native Plugin example
- Lynx's Native Modules and Custom Native Element guides
