<!--{ "pin": "top", "order": 2, "description": "tosijs-native build makes an app's iOS, macOS and Android apps from its config and web build, and never edits either." }-->

# Building an app

`tosijs-native build` makes an app's native shells from its config. The app is a folder
holding `tosijs-native.config.ts` and a web build; the command wraps what is there and
never edits it.

**Status (2026-11-30, board #4247, #4435):** it builds for iOS, macOS and Android. Every build it
makes has run in the Simulator, in the emulator or on this Mac, and the Android builds of the
examples install and open on a Samsung Galaxy A17 (with the debug key), and the iOS device
builds on an iPhone 14 Pro and 17 Pro Max (with a team development profile), 2026-12-03.
On those phones the owner saw Notes' own icon and name on the iPhone, and the Material look
on the A17 and Glass on the iPhones, in light and dark.
None has been signed for a store. The package is on npm (`bun add tosijs-native`). While
working on tosijs-native itself, link it into an app (`bun link` in this repo, then
`bun link tosijs-native` in the app) rather than installing it from a folder: a `file:`
dependency is a copy, and the command refuses to build from a stale one.

## The shortest app

```typescript
// tosijs-native.config.ts
import { defineConfig } from 'tosijs-native'

export default defineConfig({
  appId: 'net.example.notes',
  name: 'Notes Example',
  shortName: 'Notes',
  products: [],
  achievements: [],
  targets: ['web', 'ios', 'mac-direct', 'android'],
  capabilities: { files: true, share: true },
  icon: { square: 'icon.svg', background: '#1d3557' },
})
```

```bash
bun build.ts                        # your own web build, into dist/ (whatever builds it)
bunx tosijs-native doctor           # what this machine is missing, for the config's targets
bunx tosijs-native build            # every target the config lists that can be made here
bunx tosijs-native build --target ios
bunx tosijs-native run --target ios # build, install and open it in the Simulator
```

`examples/notes` is this app, whole. `examples/feed` checks a site every 15 minutes in the
background and notifies on a change, `examples/radio` plays on in the background with
the system's media controls, `examples/trail` records a route, in the background too, and
`examples/catalog` shows the native controls the examples share (below).
The config's fields are documented in [`src/config.tjs`](https://github.com/tonioloewald/tosijs-native/blob/main/src/config.tjs) (the Config page
on the site), and typed in the package's `index.d.ts`.

The examples are built with tosijs and tosijs-ui, and look native where they run:
Material on Android, Glass on iOS and macOS, chosen from `native.target` by
`examples/shared/look.ts` (each passes its icon's colour as the accent). That file is the
examples' own, not part of the package: an app chooses its look. On the web, `?look=glass`
or `?look=material` previews another platform's look in a browser.

`examples/shared/controls` holds native controls written as tosijs-ui components, offered to
it (tosijs-ui #4684): a page with its top bar, a stack of pages pushed and popped as each
platform does it (iOS's edge swipe, Material's shared axis; every push a history entry, so
Android's Back goes back a page), a bottom tab bar, and list rows for tosijs's virtual lists.
Each follows the platform its look is drawn for, read from `--tosi-nav-style`. A stack can
show several pages side by side (`columns`), as iPadOS's split view, the Mac's sidebar and
Material's list-detail layout do: as many as the width fits, so the same stack pushes on a
phone held upright and keeps its list beside the detail sideways, on an iPad or in a wide
window. The catalog's Lists tab shows three.

## What it makes

Builds go to `native/` beside the config. The folder ignores itself in git.

| target | made by | what you get | signed with |
|---|---|---|---|
| `ios` | the Swift shell | `native/ios-simulator/<Name>.app` | ad hoc, for the Simulator |
| `ios --device` | the Swift shell | `native/ios/<Name>.app` | the identity and profile you name, or nothing |
| `mac`, `mac-direct` | the Swift shell | `native/mac/<Name>.app` | ad hoc: it runs on this Mac |
| `android` | the Kotlin shell | `native/android/<Name>-<version>.apk` and `.aab` | the keystore you name, or the debug key |
| `web` | nothing | the web build is the app | |
| `windows`, `linux`, `steam` | not yet | `build` skips them and says so | |

`native/work/` holds intermediates: the staged web build, the icons, compiler caches. It
can be deleted at any time. The package's own folder is only read, so it builds the same
from `node_modules`.

A build compiles the shell for the app. From an empty `native/`, on the Mac this was
measured on and with the compilers already warm: iOS 6 s, the Mac app 7 s, Android 5 s. The
first build after a restart is slower (the Mac app took 15 s, all three targets 25 s).

## The config decides what the app may do

The command writes the config into the app as a file the page cannot change, and builds
the app with only what the config declares:

- **Apple:** a usage string for each declared device permission and no others (location's
  "while using" reason, and the `location` background mode only for `background: true`),
  the iCloud entitlement only with `cloudSaves`, the Files-app keys only with `files` (on
  the Mac, the reason macOS shows when the app first opens its folder in Documents), and
  only with `settings` a Settings bundle (the app's page in the iOS Settings app, made from
  the settings schema, one specifier per setting) and the Mac's Settings… menu item.
- **Android:** Play's review library only with `reviews`; a manifest permission only for a declared capability (an app that declares
  none has no permissions at all, not even the internet, which comes with `network`;
  location's fine permission only when it is not `approximate`, and its foreground service
  only for `background: true`), backup only with `cloudSaves`, and the gear on App info
  ("Additional settings in the app", an activity alias for `APPLICATION_PREFERENCES`) only
  with `settings`.
- **Every shell:** the bridge refuses the methods of an undeclared capability, and the
  page can load data only from the app itself and the addresses `network` lists.

`bun run examples` checks this from both sides for each example: inside a debug shell the
example's page asks for what it did not declare and must be refused, and the release build
is read back (its id, name, version, permissions, background modes, orientation and launch
screen). `bun run examples trail ios` runs one example on one target.

## How it looks before the page

`orientation` (`'portrait'`, `'landscape'` or `'any'`) becomes the Info.plist's supported
orientations for iPhone and iPad, and Android's `screenOrientation` (`userPortrait`,
`userLandscape`: they keep to the player's rotation lock). iPadOS letterboxes an app that
will not turn; Android 16 ignores the setting on screens 600 dp and wider for apps that are
not games.

`launch` is drawn twice, so it looks like one screen (CAPABILITIES.md, decision 13): as the
system's launch screen (an iOS storyboard compiled with `ibtool`; Android's theme, which
from Android 12 can show only the colour), and as the shell's own view over the web view
until the page calls `native.ready()`, or three seconds after it loaded. The image, SVG or
PNG, is drawn at each size the platforms need by `bin/raster.swift` (AppKit), so building
an app with a launch image needs a Mac, as the iOS and Mac builds do anyway.

## Signing

The command uses only what it is told, and never looks in a keychain.

- **iOS device:** `--device --identity "<name>" --profile <file> --team <id>` (or
  `APPLE_SIGNING_IDENTITY`, `APPLE_PROVISIONING_PROFILE`, `APPLE_TEAM_ID`). Without all
  three the app is built unsigned and will not install. The app's `application-identifier`
  takes its prefix from the profile, which is not always the team's id: Xcode's team wildcard
  profile (`iOS Team Provisioning Profile: *`, which covers every registered device and any
  bundle id) can carry an older one. With two certificates of the same name in the keychain,
  give the identity as its SHA-1 (`security find-identity -v -p codesigning`). Install with
  `xcrun devicectl device install app --device <id> <Name>.app`; `run` opens the Simulator
  only.
- **Android:** `--keystore <file> --key-alias <alias>`, with the passwords in
  `TOSIJS_KEYSTORE_PASSWORD` and `TOSIJS_KEY_PASSWORD`. Without them the release build is
  signed with the debug key: it installs on a test device, and a store refuses it.
- **macOS:** ad hoc. Developer ID signing and notarisation are done for the hello app by
  `bun run release` in this repo, with the entitlements written beside the `.app`
  (`<Name>.entitlements`: a hardened runtime needs one for each declared device
  permission). A signing step in this command is still to come.

## Not built yet

- A Mac App Store build (sandboxed, with a category): the `mac` target makes the same app
  as `mac-direct` today. PLAN.md, spike 4.
- Windows, Linux and Steam through this command. Linux builds only for the hello app
  (`bun run linux deb`).
- An iOS archive for App Store Connect, and a universal (Intel and Apple silicon) Mac app.
- `--debug` for anything but this package's own checks, and a way to load the page from a
  dev server while it is being written.
- A launch screen for dark mode, and an app `category` (Android 16 honours a game's
  orientation on tablets by it; the Mac App Store needs one).
