Skip to content

Writing Plugins

A PAM Native plugin is an ordinary Composer package with one extra file: a strict, versioned descriptor that tells the build which native code, dependencies, permissions, and resources the package brings. Every official pushinbr/pam-native-* package in the plugin catalog is built this way, and so can yours.

One package can contain any combination of:

  • PHP service providers, template tags, components, themes, and class tokens;
  • Android native modules and native view factories (Kotlin or Java);
  • iOS native modules and views (Swift), Swift Package products, and system frameworks;
  • Android Maven dependencies, local AARs, resources, assets, manifests, JNI libraries, and R8 consumer rules;
  • iOS resources, purpose strings, entitlements, and app extensions.

Discovery and autolinking happen at build time. A running app never scans classpaths, loads DEX files, or parses plugin JSON during a frame.

Put a capability in PAM Native core only when it is platform-generic, has a stable cross-application contract, and can be supported as part of the renderer compatibility matrix. Timers, files, HTTP, permissions, storage, lifecycle, and primitive media meet that bar.

Everything else — product protocols, heavy SDKs, optional hardware — belongs in a plugin. Examples: a signed-upload workflow, Pusher channel authentication, WebRTC signaling, a custom camera pipeline, a vendor analytics SDK. A plugin can still be public and reusable; living outside core means apps that do not need it do not pay for its binary size, permissions, or startup cost.

Before writing native code, check the core APIs. Delayed work should use Pam\Native\System\Timers, HTTP should use Pam\Native\Http\Http, and so on, rather than introducing a second Android Handler or HTTP stack inside a package.

acme-maps/
├── composer.json
├── pam-native.plugin.json
├── src/
│ └── MapsPluginProvider.php
├── android/
│ ├── src/main/kotlin/com/acme/maps/GeocoderModule.kt
│ ├── src/main/kotlin/com/acme/maps/RouteMapFactory.kt
│ └── consumer-rules.pro
└── ios/
└── Sources/GeocoderModule.swift
composer.json
{
"name": "acme/maps-plugin",
"description": "Route maps and geocoding for PAM Native.",
"type": "pam-native-plugin",
"license": "MIT",
"require": {
"php": "^8.5",
"pushinbr/pam-native": "^1.0"
},
"autoload": {
"psr-4": {
"Acme\\Maps\\": "src/"
}
},
"extra": {
"pam-native": {
"plugin": "pam-native.plugin.json"
}
}
}

extra.pam-native.plugin points at the descriptor, relative to the package root.

pam-native.plugin.json
{
"$schema": "vendor/pushinbr/pam-native/resources/pam-native.plugin.schema.json",
"version": 1,
"protocol": 1,
"pamNative": {
"minimum": "1.54.0",
"maximumExclusive": "2.0.0"
},
"capabilities": {
"required": ["runtime.modules.v1", "wire.binary.v1"],
"optional": []
},
"php": {
"provider": "Acme\\Maps\\MapsPluginProvider"
},
"android": {
"namespace": "com.acme.maps",
"minSdk": 24,
"permissions": [
"android.permission.ACCESS_FINE_LOCATION"
],
"dependencies": [
"com.example:maps-android:4.2.0"
],
"sourceDirs": ["android/src/main/kotlin"],
"consumerRules": "android/consumer-rules.pro"
},
"ios": {
"minimumVersion": "15.0",
"sourceDirs": ["ios/Sources"],
"frameworks": ["CoreLocation"],
"usageDescriptions": {
"NSLocationWhenInUseUsageDescription": "Show your position on the route map."
}
},
"modules": [
{
"name": "maps.geocoder",
"class": "com.acme.maps.GeocoderModule",
"iosClass": "GeocoderModule"
}
],
"views": [
{
"name": "maps.route",
"class": "com.acme.maps.RouteMapFactory"
}
]
}

Only version, protocol, and pamNative are required. The full set of keys:

Key Purpose
pamNative.minimum / maximumExclusive The closed range of PAM Native versions the plugin supports
capabilities.required / optional Host capabilities the plugin relies on (for example runtime.modules.v1, wire.binary.v1, renderer.incremental.v1, plugins.composer.v1, compiler.freeze.v1)
php.provider Optional PluginProvider class
php.components Package directory of .pam.php single-file components, registered as template tags
android.namespace, minSdk Android library namespace and minimum API level (plugins default to 24; must not exceed the app’s)
android.permissions Permissions merged into the app manifest
android.repositories, dependencies, localAars HTTPS Maven repositories, pinned Maven coordinates, bundled AARs
android.sourceDirs, resourceDirs, assetDirs, jniLibDirs, manifest, consumerRules Android sources and resources
ios.minimumVersion, sourceDirs, resourceDirs iOS sources and resources
ios.swiftPackages, frameworks Swift Package products and system frameworks
ios.usageDescriptions, infoPlist, entitlements Default purpose strings (the app’s ios.usageDescriptions overrides them), extra Info.plist values, entitlements
ios.extensions App extensions (share, widget, notification service, App Intents, Live Activity)
idl Typed bridge IDL file
modules Native modules: name, Android class, optional iosClass
views Native view factories: name, Android class
share Content accepted from other apps’ share sheets: configKey, accept (MIME types), multiple. PAM generates the Android intent filters and the iOS Share Extension activation rule; apps override them under plugins.<configKey> in pam-native.json

All paths are package-relative.

PAM canonicalizes every path and rejects at build time:

  • path traversal and symlink escapes;
  • descriptors larger than 1 MiB;
  • module or view names already used by the app or another plugin;
  • a PAM Native version outside pamNative, or an unsupported plugin protocol;
  • non-HTTPS Maven repositories and invalid Maven coordinates;
  • a plugin minSdk above the application’s minSdk;
  • iOS purpose strings that conflict with another plugin’s value (unless the app sets that key itself).

PAM deliberately does not execute package-provided Gradle scripts. The declared surface — sources, AARs, Maven dependencies, resources, manifests, JNI, R8 rules — keeps every build auditable, and pam mobile audit reports each permission, repository, dependency, purpose string, and entitlement a plugin adds. Unpinned dependencies (+, latest, SNAPSHOT) are flagged as critical.

Providers register in package-name order before the first render. register() defines tags and components; boot() starts work that depends on every plugin being registered.

src/MapsPluginProvider.php
<?php
declare(strict_types=1);
namespace Acme\Maps;
use Pam\Native\Plugin\PluginProvider;
use Pam\Native\TemplateRegistry;
use Pam\Native\UI\CustomView;
final class MapsPluginProvider implements PluginProvider
{
public function register(): void
{
TemplateRegistry::component(
'RouteMap',
static fn (array $props, array $_children, ?object $_scope): CustomView => CustomView::make(
'maps.route',
[
'routeId' => (string) ($props['routeId'] ?? ''),
'interactive' => (bool) ($props['interactive'] ?? true),
],
),
);
}
public function boot(): void
{
}
}

Applications then use the tag in templates:

src/Screens/TripScreen.pam.php (template)
<Screen>
<RouteMap :routeId="$routeId" interactive="true" class="flex-1 rounded-xl" />
</Screen>

Providers are optional: a package can be PHP-only, native-only, or both.

A native module receives a method name and a binary payload and completes exactly once. Slow work must leave the UI thread.

android/src/main/kotlin/com/acme/maps/GeocoderModule.kt
package com.acme.maps
import android.content.Context
import dev.pam.nativeapp.modules.ModuleCompletion
import dev.pam.nativeapp.modules.ModuleResultStatus
import dev.pam.nativeapp.modules.NativeModule
import dev.pam.nativeapp.protocol.WireMap
import dev.pam.nativeapp.protocol.WireValue
import java.util.concurrent.Executors
class GeocoderModule(
private val context: Context,
) : NativeModule {
private val executor = Executors.newSingleThreadExecutor()
override fun invoke(method: String, payload: ByteArray, completion: ModuleCompletion) {
when (method) {
"lookup" -> executor.execute {
val query = (WireMap.decode(payload)["query"] as? WireValue.Text)?.value.orEmpty()
// ... resolve the query ...
completion.complete(
ModuleResultStatus.SUCCESS,
WireMap.encode(mapOf("label" to WireValue.Text(query))),
)
}
else -> completion.complete(
ModuleResultStatus.FAILURE,
"Unknown method $method".toByteArray(),
)
}
}
}

Modules compile against the stable :plugin-api library. The constructor receives the Android Context; complete() may be called from any thread.

Call the module from PHP with a scalar map — no JSON involved:

src/Geocoder.php
<?php
declare(strict_types=1);
namespace Acme\Maps;
use Closure;
use Pam\Native\Modules\NativeModuleResult;
use Pam\Native\Modules\NativeModules;
final class Geocoder
{
/** @param Closure(?string, ?string): void $done label, error */
public static function lookup(string $query, Closure $done): int
{
return NativeModules::call(
'maps.geocoder',
'lookup',
['query' => $query],
static function (NativeModuleResult $result) use ($done): void {
if (!$result->succeeded()) {
$done(null, $result->message());
return;
}
$done((string) ($result->values()['label'] ?? ''), null);
},
);
}
}

Expose a typed PHP facade like this one rather than asking applications to call NativeModules directly. NativeModules::callRaw() exists for a package-owned binary protocol.

A view factory creates, updates, and releases a platform view rendered inside the PAM tree. PAM guarantees create, update, and release run on the Android UI thread.

android/src/main/kotlin/com/acme/maps/RouteMapFactory.kt
class RouteMapFactory(
@Suppress("UNUSED_PARAMETER") context: Context,
) : NativeViewFactory {
override fun create(context: Context, emit: (ByteArray) -> Unit): View =
AcmeRouteMapView(context).apply {
onRouteTap { routeId ->
emit(WireMap.encode(mapOf("routeId" to WireValue.Text(routeId))))
}
}
override fun update(view: View, properties: Map<String, WireValue>) {
(view as AcmeRouteMapView).routeId =
(properties["routeId"] as? WireValue.Text)?.value.orEmpty()
}
override fun release(view: View) {
(view as AcmeRouteMapView).close()
}
}

Keep gesture recognition, scrolling, and view-local animation on the UI thread; move network, disk, and large decoding off it. For a view that belongs to a single app, see Custom native views.

A plugin runs inside other people’s apps. Hold it to the same resource budget as the core:

  • No work when not needed. Start sensors, sockets, players, and timers only when the app asks, and stop them in release() or an explicit stop method. Never start background work from boot() “just in case”.
  • Bounded everything. Cap queues, caches, payload sizes, and retries. Drop or coalesce old events instead of buffering without limit.
  • Minimum authority. Declare only the permissions, purpose strings, and entitlements a shipped feature needs, and pin every Maven and Swift dependency to an exact version.
  • Respect safe areas. Any full-screen native UI a plugin presents must keep its content clear of the status bar, notch, gesture bar, and home indicator — see Safe areas.

Add your package to an app with a Composer path repository, then:

Terminal window
pam composer require acme/maps-plugin
pam mobile plugin:doctor
pam mobile plugin:list
pam mobile codegen
pam mobile ios:prepare

prepare, build, run, and dev also run code generation. The resolved graph is written to .pam-native/plugins.lock.json with package versions and descriptor SHA-256 hashes.

  • Descriptor versions, the wire protocol, statuses, and operation kinds are sequential integers starting at 1.
  • A plugin declares a closed PAM Native range with minimum and maximumExclusive; test against both ends.
  • Binding names are globally unique across the app and all installed plugins. Prefix them with your package (acme.maps.*).
  • A breaking plugin API change requires a protocol or compatibility-range change.