# Getting Started

This guide takes you from zero to a running `zero-native` app on Android and iOS.

> **Pre-1.0:** zero-native is at `0.1.x`. Core screens, navigation, OTA (bsdiff patches included), `zn dev`, UI-thread gestures and motion, `<Canvas>`, `WebSocket`, and most plugins work on both Android and iOS; [Platform Differences & Parity](https://zero-native.github.io/zero-native/guides/platform-differences/) lists what still differs.

---

## 1. Prerequisites

| Tool | Required for | Notes |
|---|---|---|
| **[Bun](https://bun.sh) 1.3+** | Everyone | Runs the compiler, CLI (`zn`), tests, and update server |
| **Android Studio** (JDK 21 + Android SDK) | Android | A phone with USB debugging, or an emulator (minSdk 24) |
| **macOS + Xcode 26** | iOS | Targets iOS 17+ |

---

## 2. Create your app

```bash
bunx @zeronative/cli new my-app
cd my-app
bun install
```

The app is built on one zero-native version, written in three places:

- `package.json`: `@zeronative/core`, `@zeronative/ui` and `@zeronative/cli` (the `zn` command) from npm;
- `ios/<AppClass>.xcodeproj`: the Swift package `https://github.com/zero-native/zero-native.git` at that exact version;
- `android/app/build.gradle.kts`: `io.github.zero-native:runtime` from Maven Central.

Keep them all at the same exact version (no `^`): `zn build` stops with a message when one differs.

The Android runtime and plugins come from Maven Central with a debug and a release build: your app's debug build
gets the debug one (where `zn dev` runs) and its release build the release one. If you add a build type of your own
(a `staging`, say), tell Gradle which one it takes in `app/build.gradle.kts`, or it finds no match:

```kotlin
buildTypes {
    create("staging") {
        matchingFallbacks += listOf("debug") // or "release"
    }
}
```

### Project structure

```
my-app/
├── app/
│   ├── src/
│   │   ├── main.ts          # Entry point: installRuntime() + startApp(routes)
│   │   ├── routes.ts        # Typed screen registry ("/" is the start screen)
│   │   ├── HomeScreen.tsx   # Example screen
│   │   └── AboutScreen.tsx  # Second screen (with deepLink: true)
│   └── assets/              # App images & fonts (create as needed)
├── android/                 # Android host (the runtime from Maven Central)
├── ios/                     # iOS host: an Xcode project on zero-native's Swift package
├── tools/ios.ts             # Helper to build & launch on the iOS Simulator
└── package.json
```

---

## 3. Run it

#### Android

Connect an Android phone (USB debugging on) or start an Android emulator, then run:

```bash
bun run android
```

This compiles your TSX into `main.zn.ui` + `main.hbc`, copies the bundle into `android/app/src/main/assets/`, and installs the debug APK. You can also open `android/` directly in Android Studio after running `bun run app:js`.

> **Release builds for performance testing:** Always measure cold start and scrolling on a release build (`./gradlew :app:installRelease`), never debug.

#### iOS

On macOS, boot a simulator and run:

```bash
open -a Simulator
bun run ios
```

This builds the JS, builds the app, installs it on the booted simulator and starts it with its console attached. The first build takes about a minute: Xcode downloads zero-native's Swift package from GitHub, and Hermes from zero-native's GitHub Release.

Useful flags for `bun run ios`:
- `--link my-app://app/about?from=link` — cold-start directly into a deep link
- `--device "iPhone 16"` — choose a simulator (booted first when it isn't running)
- `--build-only` — compile without launching

You can also open `ios/<AppClass>.xcodeproj` in Xcode after `bun run app:js:ios` and press Run; that is also how you run it on a physical iPhone (pick your team under Signing).

### Live reload (`zn dev`)

`bun run dev` builds the JS on every save and sends it to the app's debug build, without a native rebuild. The iOS simulator finds it by itself; a real iPhone on the same Wi-Fi uses `bun run dev --lan` (about 550 ms on an iPhone 11). *(On an app made by `zn new`, `zn dev` is checked on the iOS simulator; not on Android yet.)*

---

## 4. Your first screen

Here's what a real `zero-native` screen looks like:

```tsx
import { signal, computed, nav } from "@zeronative/core"
import { Column, Text, Pressable, StyleSheet } from "@zeronative/ui"

export function HomeScreen() {
  const count = signal(0)
  const label = computed(() =>
    count() === 0 ? "Tap to start" : `Tapped ${count()} times`
  )

  return (
    <Column style={s.page}>
      <Text style={s.title}>{label()}</Text>
      <Pressable style={s.button} onPress={() => count.set(count() + 1)}>
        <Text style={s.buttonText}>Increment</Text>
      </Pressable>
      <Pressable onPress={() => nav.push("about", { from: "home" })}>
        <Text style={s.link}>Go to About →</Text>
      </Pressable>
    </Column>
  )
}

const s = StyleSheet.create({
  page: { flex: 1, justifyContent: "center", alignItems: "center", gap: 16, padding: 24 },
  title: { fontSize: 22, fontWeight: 700, color: "#111827" },
  button: { backgroundColor: "#111827", paddingHorizontal: 20, paddingVertical: 12, borderRadius: 10 },
  buttonText: { color: "#FFFFFF", fontWeight: 600 },
  link: { color: "#2563EB", fontSize: 15 },
})
```

Notice there's no `useState`, no dependency arrays, and no re-rendering. `HomeScreen` runs **once**; when `count.set(...)` is called, only the `<Text>` slot updates on the native UI thread.

---

## 5. CLI cheat sheet (`zn`)

| Command | What it does |
|---|---|
| `zn new <dir> [--name N] [--id ID] [--ios cmake\|packages] [--from npm\|checkout]` | Scaffold a new Android + iOS app (on this checkout, or on the published packages) |
| `zn dev [appDir]` | Watch & live-reload JS/templates to Android debug builds |
| `zn build <appDir> --assets <dir> --name <app>` | Compile TSX + bytecode into a bundle |
| `zn release` / `zn publish` / `zn rollout` | Release & manage OTA updates via `zn-updates` |
| `zn store-build --os android\|ios --channel production --version-code <n> --version-name <x.y.z>` | Build the JS for a Play / App Store build, before the AAB / IPA ([Releasing your app](https://github.com/zero-native/zero-native/blob/main/docs/guides/releasing-your-app.md)) |
| `zn server [--demo]` | Run a local `zn-updates` server |
| `zn where` | Print the `zero-native` checkout path |
| `zn <command> --help` | Every flag of one command (also `zn help <command>`) |

---

## 6. Building on a zero-native checkout

To work on zero-native itself, or to try an app against unreleased changes, make the app from a clone. You also need
CMake (`brew install cmake`) for the CMake iOS host.

```bash
git clone https://github.com/zero-native/zero-native.git
cd zero-native
bun install
bun run sample:js   # optional: build the sample app
bun run test        # optional: run the test suite
```

From inside the `zero-native` folder, scaffold a new app outside the repo (`--name` must be lowercase letters, digits, or hyphens, starting with a letter):

```bash
bun run tools/zn.ts new ../my-app --name my-app --id com.example.myapp
cd ../my-app
bun install
```

*(Tip: you can run `bun link && bun link zero-native -g` once in the `zero-native` root so `zn` is available globally from any folder.)*

The app references your local `zero-native` checkout by relative path in `package.json`, `android/gradle.properties`, `ios/CMakeLists.txt`, and `tools/ios.ts`; update those four if you move either folder. A checkout of a release tag can also make an app on the published packages with `zn new --from npm` (on `main` the version is `0.0.0`, which it refuses).

### Running a checkout app on a physical iPhone
First download release Hermes once inside `zero-native`:

```bash
bun run tools/fetch-hermes-apple.ts release
```

Then from **your `zn new` app folder** (replace `<AppClass>` with the Swift target name in `ios/CMakeLists.txt`, e.g., `MyApp`):

```bash
bun run app:js:ios
cmake -G Xcode -S ios -B ios/build/device \
  -DCMAKE_SYSTEM_NAME=iOS \
  -DCMAKE_OSX_SYSROOT=iphoneos \
  -DAPP_TEAM=<YOUR_APPLE_TEAM_ID> \
  -DZN_HERMES_FLAVOR=release

xcodebuild -project ios/build/device/<AppClass>.xcodeproj \
  -target <AppClass> -sdk iphoneos -configuration Release \
  -allowProvisioningUpdates build

xcrun devicectl device install app --device <DEVICE_ID> \
  ios/build/device/Release-iphoneos/<AppClass>.app
```

*(For building the `zero-native` sample app itself on a physical iPhone, pass `-DZN_TEAM=<TEAM_ID>` and `-DZN_HERMES_DIR=$PWD/ios/third_party/hermes/release/destroot` against `ios/CMakeLists.txt`.)*

---

## Next steps

- Read **[Core Concepts](https://zero-native.github.io/zero-native/get-started/core-concepts/)** to master signals, JSX rules, and the gotchas that trip up React developers.
- Check **[Styling & Layout](https://zero-native.github.io/zero-native/guides/styling-and-layout/)** and **[Components](https://zero-native.github.io/zero-native/guides/components/)** to build real screens.
- Coming from another framework? Jump to **[Coming from React Native](https://zero-native.github.io/zero-native/coming-from/react-native/)** or **[Coming from Flutter](https://zero-native.github.io/zero-native/coming-from/flutter/)**.
