BakeNative is under active development. APIs may change.
Skip to Content
BakeNative

App packages

Ship each BakeNative app as its own framework, with its own native modules, on one shared runtime, and check that JS and native still fit before every release.

An app package is one BakeNative app as a native part: its JS bundle, its first screen, and the native modules it uses. A host app adds the shared runtime once and any number of packages next to it. The host calls the package entry point and injects any host capabilities the package explicitly declares.

PartiOSAndroid (planned)How many
Runtime: Rust core, Hermes, the UIKit / Android hostBakeNative.xcframeworkthe runtime AAR (dev.bakenative:bakenative)one per host app, versioned
Package: one app's code, bundle and modules<App>.xcframework (static)<app>.aarone per app

Every app shares one runtime, so adding an app adds only its own code and bundle, and the Rust and Hermes symbols exist once in the process.

1. Declare the package

In the app's package.json:

{
  "name": "@acme/norrow",
  "version": "1.0.0",
  "bakenative": {
    "package": {
      "name": "Norrow",
      "id": "dev.bakenative.example.norrow",
      "modules": "native/ios",
      "schema": "native-modules.json",
      "requires": [],
      "runtime": ">=0.0.1 <0.1.0"
    }
  }
}
FieldMeaningDefault
nameSwift module and entry point (Norrow.makeViewController())from name
idPackage id: the OTA bundle must name it; also the Storage scopedev.bakenative.app.<name>
modulesDirectory of the app's own native modules' Swift implementationsnone
schemaSignatures of modules without a spec (a schema a host wrote); the specs in native/<name>/ give the restnative-modules.json
requiresHost capabilities the app may use[]
runtimeRuntimes the package runs on; when declared, its OTA bundles require it toothe series it is built with, e.g. >=0.0.1 <0.1.0
nativeBuildRaise when native behavior changes behind unchanged signatures (step 5)0

2. Add a native module

bake native new NorrowShare --platform apple --target Norrow

Declare the module in native/norrow-share/NorrowShare.native.ts and implement the generated NorrowShareSpec in the modules directory (Native modules). bake package generates the contract into the package, and the spec gives the signatures the package provides; bake native types writes the JS types.

import { native, hasNative } from '@bake-native/octane';

// In a handler, not during render: the snapshot bake has no native modules.
if (hasNative('NorrowShare')) await native.NorrowShare.share(product.name, product.url);

Keep native calls out of render, and guard them with hasNative when the same source also runs on hosts that do not ship the module (the web, other hosts).

3. Build the package

bake package --target ios           # in the app directory; --runtime <BakeNative.xcframework> outside the monorepo
dist/ios-package/
  Norrow.xcframework
    ios-arm64/Norrow.framework            and ios-arm64-simulator/
      Norrow                              static archive: NorrowShareModule + the generated entry point
      Modules/Norrow.swiftmodule/         .swiftinterface only (library evolution)
      BakeNativeApp/
        app.hbc  app.baked  app.css        the bundle (baked first screen)
        manifest.json                     the bundle manifest, with what the bundle requires
        BakeNativePackage.json               what the package provides (below)
        assets/                           the app's assets/ directory, if any
  BakeNativePackage.json                     a copy for CI

bake package builds the target with --bake, checks the native module uses (step 5), and compiles the module sources and the entry point against the runtime. The package links the runtime; it does not contain it.

4. Add packages to a host

Add BakeNative.xcframework and each package to the app target, all with Embed & Sign. Embedding a static package copies its BakeNativeApp/ resources into the app's Frameworks/, and Xcode links its code into the app binary. With XcodeGen:

dependencies:
  - framework: Frameworks/BakeNative.xcframework
    embed: true
  - framework: Frameworks/Norrow.xcframework
    embed: true
  - framework: Frameworks/Showcase.xcframework
    embed: true

The host code:

import Norrow
import Showcase

navigationController?.pushViewController(Norrow.makeViewController(), animated: true)

// Prepare the first screens while the host's screen is up (no JS), so a push shows them at once.
Norrow.prewarm()
Showcase.prewarm()

In SwiftUI, use NorrowView(). examples/package-host-ios is a complete host: an empty app with two buttons (./build.sh && xcodegen generate).

The generated entry point of a package:

APIWhat it does
<App>.makeViewController(capabilities:hydration:)the app's screen, with the package's modules
<App>View(capabilities:hydration:)the same in SwiftUI
<App>.prewarm()prepares the first screen without JS
<App>.use(update:)offers an OTA bundle directory; returns .accept or .reject(reason)
<App>.packagethe BakeNativePackage: manifest, embedded and active bundle

Isolation

  • Modules. Every screen of a package registers the package's own modules and the runtime modules (Platform, Storage, Haptics, Clipboard, Linking, Share). The host's modules are never visible to it. Give the package's modules names of their own: a runtime module's name is left out of the fingerprint.
  • Storage. Storage is scoped by the package id, so two packages never share keys.
  • Capabilities. A host feature a package needs (a login token, navigation) is a native module the host injects: Norrow.makeViewController(capabilities: [AuthModule()]). The package must list it in requires. The package does not start (it shows the reason instead) when a required capability is missing, when the host passes one that is not declared, or when one has the name of a package or runtime module. bake build fails when the JS uses native.X and X is neither a package module, a runtime module nor a declared capability.

5. OTA compatibility: requires and provides

JS and native ship separately: the JS bundle can be updated over the air, the native code only with an app release. Whether a bundle may run in an installed package is decided item by item, not by comparing one hash of everything: the bundle lists what it requires, the runtime and the package list what they provide, and the bundle runs when every requirement is provided with the same shape.

So a change breaks an existing bundle only when the bundle used what changed:

Change in the app releaseBundles built before it
A native function added to a module, or a new modulestill run
Native code changed behind the same signaturesstill run
A native function the bundle does not call removed or changedstill run
A runtime that adds renderer ops, events or CSS properties, or renumbers opsstill run
A native function the bundle calls removed, or its signature changedrefused
A renderer op, event or CSS property the bundle uses removed or reshapedrefused
Another packagerefused

Each mini-app team owns its range: it declares what its bundles need (its own modules' signatures, optionally a runtime range and a nativeBuild) and gates its OTA releases with bake release check. The host team owns the shared runtime: when it ships another runtime, every package's bundles are compared against it automatically, and a bundle that no longer fits falls back to the package's embedded bundle on the device.

What a bundle requires

bake build writes requires into the bundle's manifest.json (see the manifest reference):

"requires": {
  "format": 1,
  "batch": {
    "stream": "df3b82fcd9fa877c",
    "seed": { "count": 52, "hash": "…" },
    "ops": { "create": { "tag": 1, "shape": "ceedecf0869e4cc1" }, "…": {} }
  },
  "events": ["click", "input"],
  "style": ["background-color", "color", "padding"],
  "package": "dev.bakenative.example.norrow",
  "native": { "NorrowShare.share": "d6ac2802a0846f1f" }
}
  • batch: the renderer batch stream its app.hbc writes. stream hashes what every op shares (encoding, header, interned tables), each op has its tag and a hash of its fields (shape, the tag left out), and seed names the built-in strings it knows. Generated from batch.oschema (packages/core/batch-contract.json).
  • events: the events its markup listens to (onClick → click), and style the CSS properties it declares (in app.css and literal inline styles). Only what the runtime of the building toolchain provides is listed; anything else is already a build error or ignored on native.
  • package and native, in a packaged app: the package id and the signature of every native member the sources call. A dynamic access (native.NorrowShare[name], native.NorrowShare passed around) requires every member of that module, native[name] every member of every package module.
  • nativeBuild and runtime, only when the app declares them (below).

A member signature is FNV-1a 64 of <Module>.<member>:<kind>(<type>[?],...)-><returns>. Parameter names are not part of it. Changing a parameter's type or optionality, the return type, or the kind (sync, async, constant) changes it.

What a package provides

BakeNativePackage.json (format 2) has provides:

"provides": {
  "package": "dev.bakenative.example.norrow",
  "nativeBuild": 0,
  "native": { "NorrowShare.available": "dcb4ce32defa1c74", "NorrowShare.share": "d6ac2802a0846f1f" },
  "hermesBytecode": 96,
  "modules": ["Share"],
  "runtime": { "format": 1, "runtime": "0.0.1", "hostProtocol": 4, "batch": { "…": {} }, "events": ["…"], "style": ["…"] }
}

views (when the bundle renders a native view, such as <video>'s player) maps each view to its spec signature, as native does for module members. runtime is what the runtime it was packaged with provides (bake release check compares against it). On the device the core compares against the runtime the host actually links.

modules lists the first-party modules compiled into the package: the ones its bundle uses (requires.modules), and no others (System modules). An update that uses another one is refused (module \Clipboard`: the package does not include it`).

Escape hatches

  • nativeBuild (package.json "bakenative": { "package": { "nativeBuild": 1 } }, default 0): raise it when native behavior changes behind unchanged signatures. The package provides it, and bundles built from then on require at least it, so they are refused by older packages.
  • runtime: when the app declares a runtime range, its bundles require it too (runtime 0.5.1 is outside the bundle's range >=0.4.0 <0.5.0).

Where it is checked

  1. bake build. Every native.<Module>.<member> the bundled sources use must exist in the package's schema, and every native.<Module> must be a package module, a runtime module or a declared capability. Otherwise the build fails with the file and line.

  2. bake release check <bundle> --against <package>, in CI before an OTA release:

    bake release check dist/ios --against Norrow.xcframework
    # ✗ dist/ios/manifest.json must not ship to Norrow 1.0.0 (the device would reject it and run the embedded bundle):
    #     native.NorrowShare.share: signature changed (bundle d6ac2802a0846f1f, package efede852341469fa)

    It exits with 1 when the bundle would be refused and prints every reason. --json prints the result as JSON. On Android, --against also takes the package AAR or the app's APK (it reads assets/bakenative/<package id>/BakeNativePackage.json). On the device, Android's BakeNativePackage provides the first-party modules and native views the app links (the generated BakeNativeAppModules / BakeNativeAppViews from bake native link), so a bundle that uses one the app lacks is refused.

  3. On the device. The Rust core decides, the same way on every platform: when a root opens a bundle it checks the runtime's half (renderer ops, events, style properties, runtime range) and refuses the bundle with the reasons. A package host also passes its own half to bake_bundle_check before it launches a downloaded bundle and keeps the embedded bundle when the answer is not ok.

Reasons name each missing or changed item:

renderer batch op `mount-template`: this runtime no longer reads it
renderer batch op `create`: its fields changed (bundle ceedecf0869e4cc1, runtime 1b2c…)
event `tabselect`: this runtime no longer dispatches it
style property `gap`: this runtime no longer supports it
native.NorrowShare.share: the package no longer provides it
native.NorrowShare.share: signature changed (bundle d6ac2802a0846f1f, package efede852341469fa)
package: the bundle is for dev.bakenative.example.norrow, this package is dev.bakenative.example.showcase
nativeBuild: the bundle needs 2 or later, the package is 1

The rules live in native/rust/crates/bake-runtime/src/compat.rs (device) and packages/core/compat.mjs (bake release check); tests/compat/cases.json holds the cases both must answer identically.

Renumbered renderer ops

A bundle's batches carry op tags, and its manifest the tag of each op name. When the runtime's batch.oschema numbers an op differently, the core maps the bundle's tags to its own by name while it reads, so renumbering or adding ops breaks nothing. Seed strings may only be appended; reordering them, or changing the shared encoding, refuses every bundle built before.

Bundles from before requires

A manifest without requires is judged by what it carries: its batchDigest must equal the runtime's exactly (it cannot be matched by name), and an old native block's package and uses (member signatures, the same hashes as requires.native) are checked by name. Its native.fingerprint is ignored. Rebuild such a bundle to get the by-name rules for the renderer stream too.

The host contract (C ABI)

Include bakenative_package.h from native/rust/apple/Sources/CBakeNativeRust/include. The Rust core implements both calls for every platform.

FunctionUse
bake_bundle_check(manifest, len, package, len, reason, cap, &written)BAKE_PACKAGE_OK allows launch. BAKE_PACKAGE_UNSUPPORTED means launch the embedded bundle; reason holds the reasons, one per line. package is the package half of provides (package, nativeBuild, native) as JSON, or NULL for the runtime's half only.
bake_runtime_provides(out, cap, &written)the runtime's half as JSON (version, host protocol, batch ops, events, style properties)

A package host builds its half from the modules it actually registers (<Module>.<member> → signature, the same canonical line as above), its package id and nativeBuild:

  • iOS: BakeNativePackage.check(update:) (and use(update:), bundle(preferring:)) first checks the bundle files (manifest, Hermes bytecode version), then calls bake_bundle_check with BakeNativePackage.provides, built live from the package's modules. A rejected update leaves the embedded bundle; the reason names each missing or changed item. schemaDrift warns when the compiled modules differ from the signatures in BakeNativePackage.json (a stale native-modules.json).
  • Android: dev.bakenative.BakeNativePackage (fromAssets / fromDirectory, check, use, bundle) calls the same function over JNI (BakeNativeBundleChecker.Core). Android has no native module registry yet, so its provides take the signatures from BakeNativePackage.json provides.native.

Runtime range

A package declares the runtimes it runs on (runtime, by default the series it was built with: >=0.0.1 <0.1.0). When the host links a runtime outside that range, the package does not start: makeViewController() shows the reason and logs [BakeNative] package cannot start: Norrow needs BakeNative runtime >=0.0.1 <0.1.0 (built with 0.0.1); this app links BakeNative.xcframework 0.2.0 .... BakeNativeRuntime.version is the linked runtime's version.

6. Third-party libraries

Two packages that each carry the same third-party library (Lottie, Firebase, OkHttp, SQLite) break one host: duplicate symbols when a static iOS host links them, two copies of one ObjC class in a dynamic one, resource and manifest merge conflicts on Android, and a crash when two libc++_shared.so from different NDKs meet. So a package declares every third-party library it uses, and chooses per library how the host gets it:

ModeThe host getsPossible for
shared (default)one copy, linked by the host; every package states the versions it acceptseverything
isolatedeach package's own copy, renamed into the package (relocated Java/Kotlin packages, renamed .so, prefixed C symbols)Java/Kotlin, .so, C. Not Swift modules or ObjC classes: a process has one module or class of each name

Third-party code a package carries without declaring it is a packaging error in either mode.

Declare them

In bakenative.package of the app's package.json:

{
  "bakenative": {
    "package": {
      "name": "Norrow",
      "id": "dev.bakenative.example.norrow",
      "android": { "package": "dev.bakenative.example.norrow", "resourcePrefix": "norrow_", "soPrefix": "libnorrow" },
      "thirdParty": [
        {
          "name": "lottie",
          "version": "^6.4.0",
          "mode": "shared",
          "android": { "packages": ["com.airbnb.lottie"], "maven": "com.airbnb.android:lottie" },
          "ios": { "language": "swift", "modules": ["Lottie"] }
        },
        {
          "name": "sqlite",
          "version": "3.45.1",
          "mode": "isolated",
          "platforms": ["android", "ios"],
          "android": { "so": ["libsqlite3.so"] },
          "ios": { "language": "c", "symbols": ["sqlite3_"] }
        }
      ]
    }
  }
}
FieldMeaningDefault
namethe library; the host check matches packages by itrequired
versionthe versions the package works with: >=6.4.0 <7.0.0, ^6.4.0, ~6.4.1, 6.4.x, 6.4.1, alternatives with ||required
modeshared or isolatedshared
platformsandroid, iosthe platforms with a block below
android.packagesits Java/Kotlin packages (original names)one of packages / so
android.soits .so files (original names)
android.maventhe coordinate the host adds for a shared library (informational)none
ios.languageswift, objc or crequired for iOS
ios.modules / ios.classes / ios.symbolsits Swift/clang modules, ObjC class prefixes, C symbol prefixesthe one for its language

android says where the package's own Android code lives; everything outside it is third-party:

FieldMeaningDefault
packageJava/Kotlin package prefix of its classes and its manifest namespacethe package id
resourcePrefixprefix of every Android resource name (AGP resourcePrefix)<name>_, lowercased
soPrefixprefix of its .so files; isolated copies are <soPrefix>_<name>.solib<name>, lowercased

A library may be declared once per platform, so it can be isolated on Android and shared on iOS (two entries, each with its own platforms). bake package and both checks below refuse a declaration they cannot use: no identity, an unreadable range, or isolated for Swift / ObjC. BakeNativePackage.json carries thirdParty and android, so a host checks packages without their sources.

Check a package: bake package check

bake package check norrow.aar     # reads assets/bakenative/<id>/BakeNativePackage.json, else --root's package.json

The check reads the AAR itself (zip, ELF, manifest XML; no Android tools) and fails on:

CodeWhat it foundFix
undeclared-classesclasses (in classes.jar or a packed libs/*.jar) outside the package's Java package that no entry claimsdeclare the library; shared: compileOnly, isolated: relocate under the package
undeclared-soa .so outside soPrefix, or one a package .so needs (DT_NEEDED) that is not the package's, the runtime's, a system library or a shared declarationdeclare it; isolated: rename it
shared-bundleda shared library's classes, .so or manifest components inside the AARmake the dependency compileOnly; the host links one copy
isolated-not-renamedan isolated library under its original package or file name, or a .so linked against the original namerelocate (Shadow / jarjar), or rename the .so and its SONAME
runtime-bundledclasses in dev.bakenative.*, or libbakenative.so / libbakehermes.so / libhermesvm.sodepend on the runtime with compileOnly
cxx-shared-ndka libc++_shared.so from an NDK outside the runtime's series (read from its .note.android.ident), or without the notebuild with the runtime's NDK (ndkVersion = "28.2.13676358", range >=28.0.0 <29.0.0)
sonamea package .so whose SONAME is not its file name-Wl,-soname,<file>
resource-prefixa resource (file, values entry or styleable attr) not starting with resourcePrefixrename; set AGP resourcePrefix
asset-pathassets outside assets/bakenative/<id>/move them
manifest-namespacea manifest package outside the package's Java packagenamespace = ...
manifest-application<application> attributes (name, theme, icon, allowBackup, …)remove: the host owns <application>
manifest-overridetools:replace / tools:remove / tools:noderemove: a package does not rewrite the host's manifest
manifest-componentan activity, service, receiver or provider class outside the packagedeclare its library
provider-authorityan authority without ${applicationId} and the package's name${applicationId}.<package>.<name>
manifest-permissiona <permission> outside the packagename it under the package
min-sdkminSdkVersion above the runtime's (29)lower it
kotlin-moduleMETA-INF/<module>.kotlin_module without the package name (two app_release modules fail the host build)set the Kotlin moduleName
proguard-globalconsumer rules with global options (-dontobfuscate, -repackageclasses, …)keep to -keep rules for the package

Warnings (exit 0): a .so for an ABI the runtime does not ship, a .so that exports symbols besides JNI that no other package .so links against (statically linked C code shows up there; -fvisibility=hidden), un-namespaced <meta-data>, a jar packed into the AAR.

✗ sneaky.aar (Sneaky, dev.bakenative.example.sneaky): 1/2 classes own, 3 .so, 3 resources
  ✗ [undeclared-classes] classes.jar: com.acme.vendor.* (1 file, e.g. com/acme/vendor/Vendor.class) is not Sneaky's code (dev.bakenative.example.sneaky.*) and no thirdParty entry claims it
      fix: declare it in package.json "bakenative.package.thirdParty" (…). shared: drop its classes from the AAR (compileOnly / no jniLibs copy) so the host links one copy. isolated: rename it into Sneaky's namespace (…)
  ✗ [cxx-shared-ndk] jni/arm64-v8a/libc++_shared.so comes from NDK r27-beta1 (27.0.0); the shared runtime fixes NDK >=28.0.0 <29.0.0. …

bake package --target android (planned, below) will run the same check on the AAR it writes before it is written.

Check a host: bake package host-check

bake package host-check norrow.aar showcase.aar dist/ios-package/ [--provide lottie=6.5.0] [--platform android]

Each argument is an AAR (its embedded BakeNativePackage.json, plus the package check), an BakeNativePackage.json or its directory, or an app directory. Per platform and library, the shared ranges must overlap: the host links one version inside all of them (--provide checks a version the host already links). When they do not overlap, the error names each package's range and the way out: make all but one isolated or, for Swift / ObjC, align the versions. It also fails when two packages share an id, overlapping Java packages, a resource or .so prefix, a .so file name, a provider authority, a Kotlin module file or a manifest namespace.

✗ 2 packages in one host: Alpha, Gamma
  android vendor: no common version — Alpha >=1.2.0 <2.0.0, Gamma ^2.1.0
  ✗ android vendor: the shared version ranges do not overlap, so the host cannot link one copy:
      Alpha: >=1.2.0 <2.0.0
      Gamma: ^2.1.0
    Align the ranges, or make all but one of them "mode": "isolated" (…)

tests/package-third-party builds such packages with AGP and the NDK (run.mjs; the test runs it with BAKE_ANDROID_E2E=1).

The iOS check (contract)

The same rules for <App>.xcframework, checked on macOS by bake package --target ios (planned; BakeNativePackage.json already carries the declarations, and host-check already checks the iOS ranges):

  • Swift modules: every .swiftmodule / .swiftinterface in the xcframework is the package's own module, and every module a .swiftinterface imports is the package's, a runtime module (BakeNative), an SDK module or a shared library's ios.modules. A shared library's module inside the xcframework is shared-bundled; any other foreign module is undeclared-module. Swift and ObjC libraries cannot be isolated.
  • ObjC classes: every _OBJC_CLASS_$_<Name> the archive defines (nm -gU, each slice) is the package's own (its module's mangling for Swift classes, its prefix for ObjC). One with a shared library's ios.classes prefix is shared-bundled; any other is undeclared-objc-class.
  • C symbols: every defined global symbol is the package's, or, for an isolated C library, carries the package's prefix (norrow_sqlite3_open, not sqlite3_open): an ios.symbols prefix without it is isolated-not-renamed, in a shared library shared-bundled. Undefined references to a shared library's symbols are expected: the host links it.
  • Linking: packages stay static (or mergeable); a dynamic package that embeds a library a second package also embeds is the duplicate-class case and fails the same way.
  • Errors use the Android check's codes and message / fix shape.

Android (planned)

The same contract, as Android libraries:

  • The runtime AAR (dev.bakenative:bakenative, Android AAR): one libbakenative.so (Rust core and Hermes linked statically with c++_static and hidden symbols, so it never clashes with a React Native host's libhermes.so, libjsi.so or libc++_shared.so) plus the Kotlin host.
  • bake package --target android → <app>.aar: the app's Kotlin modules, the bundle under assets/bakenative/<package id>/ (namespaced per package), BakeNativePackage.json, and one entry point: Norrow.view(context) / Norrow.fragment(), Norrow.prewarm(context), Norrow.use(update).
  • The same requires and provides: member signatures from the Kotlin module schema with the same canonical lines, checked by the same Rust core (bake_bundle_check over JNI), so one bake release check serves both platforms.

Limits

  • The JS check reads native.<Module>.<member> in the source text; the binding must be named native.
  • native-modules.json is written by running the package. The package compares it with the compiled modules when its first screen opens and logs [BakeNative] warning: ... when they differ; extracting it at build time is planned.
  • Capabilities are checked by name, not by signature.
  • Third-party libraries are declared, never carried unannounced: 6. Third-party libraries.

On this page