Learn how Expo CLI generates native code of your project before compiling it.


Before a native app can be compiled, the native source code needs to exist. Expo CLI provides a unique and powerful system called prebuild, which generates the native code for your project based on four factors:

  1. The app config file (app.json, app.config.js).
  2. Arguments passed to the npx expo prebuild command.
  3. Version of expo that's installed in the project and its corresponding prebuild template.
  4. Autolinking, for linking native modules found in package.json.

Usage

Prebuild can be used by running:

Terminal
npx expo prebuild

This creates the android and ios directories for running your React code. If you modify the generated directories manually then you risk losing your changes the next time you run npx expo prebuild --clean. Instead, you should use config plugins — functions that perform modifications on native projects during prebuild.

We highly recommend using prebuild for the reasons listed in the pitch section, but the system is fully optional, and you can stop using it at any time.

Usage with EAS Build

If your project does not contain android and ios directories, EAS Build will run Prebuild to generate these native directories before compilation. This is the default behavior for any project created using npx create-expo-app.

For a project that has android and ios directories, EAS Build will not run Prebuild by default to avoid overwriting any changes you've made to the native directories.

If you troubleshoot your app by compiling it locally (running npx expo prebuild, or npx expo run:android or npx expo run:ios), you can still use Prebuild with EAS Build to generate fresh native directories during the build process. In this scenario, add the android and ios directories to .gitignore or .easignore files:

.gitignore
+ /android
+ /ios

Usage with Expo CLI run commands

You can perform a native build locally by running:

Terminal
# Build your native Android project
npx expo run:android

# Build your native iOS project
npx expo run:ios

If native build directories are not present npx expo prebuild will be run one time for the specific platform you wish to run. On subsequent uses of the run commands, you will need to manually run npx expo prebuild to ensure the native code is freshly synchronized with your local configuration.

Platform support

Prebuild currently supports Android and iOS. Web support is not required because there is no native project to generate for the web — the app it runs in is always your web browser. You can build for individual platforms by using the --platform flag:

Terminal
npx expo prebuild --platform ios

Dependencies

The first step in prebuild is to initialize new native projects from a template. There is a template for each Expo SDK version, and each Expo SDK version corresponds to a specific version of React and React Native. The React and React Native versions in your project will be updated to the versions used in the dependencies field in the template package.json, only, if they are not already the same.

You can skip changing npm package versions with the --skip-dependency-update flag:

Terminal
npx expo prebuild --skip-dependency-update react-native,react

Package managers

When the dependencies are changed, prebuild will reinstall packages using the package manager that is currently used in the project (this is inferred from the lockfile). You can force a specific package manager by providing one of: --npm, --yarn, --pnpm.

All installations can be skipped by passing the --no-install command, which is useful for testing generation quickly.

Clean

The --clean flag will delete any existing native directories before generating.

When you re-run npx expo prebuild without the --clean flag, it layers the changes on top of your existing files. This is faster than re-generating from scratch, but it may not produce the same results in some cases. For example, not all config plugins are idempotent — when your project utilizes a lot of "dangerous modifiers" for performing changes like adding regex changes to application code, this can sometimes lead to unexpected behavior. This is why using the --clean flag is the safest way to use the prebuild command and it is generally recommended in most cases.

Due to the destructive nature of cleaning, you'll be warned to have a clean git status when the --clean flag is used. This prompt is optional and will be skipped when encountered in CI.

If you'd like, you can disable the check by enabling the environment variable EXPO_NO_GIT_STATUS=1.

The purpose of the prompt is to encourage users to add the android and ios directories to the project's .gitignore. This ensures that these directories are always generated by prebuild. However, this can make custom config plugins harder to develop so we haven't introduced any mechanism to enforce this behavior.

There are cases where developers may want to swap between workflows often. For example, you may want to build custom functionality natively in Xcode and Android Studio, and then move that functionality into local config plugins.

Templates

You can customize how the native folders are generated by config plugins. Many config plugins already exist for lots of modifications, and community libraries often ship their own as well. You can see a list of some popular plugins for more information.

Prebuild starts from template files which are then modified with config plugins. The template files are based on the Expo SDK version and come from the npm package expo-template-bare-minimum. You can change the template used by passing --template /path/to/template.tgz to the npx expo prebuild command. This is not generally recommended because the base modifiers in @expo/prebuild-config make some undocumented assumptions about the template files, so it may be tricky to maintain your custom template.

Note: In network environments where all packages are downloaded from a private registry and npm public registry access is blocked, a locally-available template must be passed to the prebuild command. Learn more about using a local version of the default template.

Side effects

npx expo prebuild performs several side effects outside of generating the android and ios directories. Work is in progress to eliminate these side effects — ideally, running npx expo prebuild would generate the Android and iOS projects and leave the rest of the project untouched.

In addition to generating the native folders, prebuild also makes the following modifications:

  • Modifies the scripts field in the package.json.
  • Modifies the dependencies field in the package.json.

The convenience change to the scripts field is the only side effect that alters how a developer works on their app before/after prebuild. All other changes can be left in place and committed to git to minimize the diff when running prebuild.

Optionality

Prebuilding is completely optional and it works great with all tools and services offered by Expo. There are existing React Native projects that do not use npx expo prebuild — where developers make (direct changes to their native projects), rather than continuously generating them on demand as with prebuild.

Everything offered by Expo including EAS, Expo CLI, and the libraries in the Expo SDK are built to fully support bare React Native projects as this is a minimum requirement for supporting projects using npx expo prebuild. The only exception is the Expo Go app, which can load arbitrary React Native projects, but only if they are structured to provide JavaScript fallbacks for native code that does not exist in the Expo Go runtime.

Pitch

A single native project on its own is complicated to maintain, scale, and grow. In a cross-platform app, you have multiple native projects that you must maintain and keep up to date with the latest operating system releases to avoid falling too far behind in any third-party dependencies. We created the optional Expo Prebuild system to streamline this process. Below are a few issues we've identified with native development in the context of React Native and some corresponding reasons we believe Expo Prebuild solves these issues.

Prebuild can be used in any React Native project. For more information, see Adopt prebuild.

Sensible upgrades

Building native code requires a basic familiarity with that native platform's default tooling leading to a fairly difficult learning curve. In cross-platform, this curve is multiplied by the number of platforms you wish to develop for. Cross-platform tooling doesn't solve this issue if you need to drop down and implement many features individually in platform-specific native code.

When you bootstrap a native app, there is a bunch of code and configuration that you don't need to understand to get started. But you now own it. Eventually, you will want to upgrade your native application and now you'll need to be familiar with how all of the initial code works to safely upgrade it. This is extremely challenging and users often either upgrade their app incorrectly, missing crucial changes, or they'll bootstrap a new app and copy all of their source code into the new application.

With Prebuild upgrading is much closer to upgrading a pure JavaScript application. Bump the versions in your package.json and regenerate the native project.

Cross-platform configuration

Cross-platform configurations such as the app icon, name, splash screen, and so on must be implemented manually in native code. These are often implemented very differently across platforms.

With Prebuild cross-platform configurations are handled at the config plugin level, and the developer only needs to set a single value like "icon": "./icon.png" to have all icon generation taken care of.

Dependency side effects

Many complex native packages require additional setup beyond installing and autolinking. For example, a camera library requires permission settings to be added to AndroidManifest.xml for Android and Info.plist for iOS. This additional setup can be considered a configuration side effect of a package. Pasting the required side effect code into your project's native files can lead to difficult native compilation errors, and it's also code that you now own and maintain.

With Prebuild library authors, who know how to configure their library better than anyone, can create a testable and versioned script called a config plugin, to automate adding the required configuration side effects for their library. This means library side effects can be more expressive, powerful, and stable. For native code side effects, we also provide AppDelegate Subscribers and Android Lifecycle Listeners which come standard in the default prebuild template.

Orphaned code

When you uninstall a package you have to be certain you removed all of the side effects required to make that package work. If you miss anything, it leads to orphaned code that you cannot trace back to any particular package, this code builds up and makes your project harder to understand and maintain.

With Prebuild the only side effect is the config plugin in a project's Expo config (app.json), which will throw an error when the corresponding node module has been uninstalled, meaning a lot less orphaned configuration.

Anti-pitch

Here are some reasons Expo Prebuilding might not be the right fit for a particular project.

React Native versioning

npx expo prebuild generates native code based on the version of expo a project has installed, so a project with SDK 50 (expo@50.0.0) would generate a react-native@0.73 app.

Expo releases a new version approximately every quarter, and react-native does not follow a calendar-based release schedule. This means there are times when you cannot use npx expo prebuild with the latest release of React Native. This could potentially be circumvented by using a custom prebuild template if you are willing to experiment. You can also mitigate this by cherry-picking any changes you need from the latest version of React Native into a fork and using that in your project.

Platform compatibility

Prebuild can only be used for native platforms that are supported by the Expo SDK. This means Android and iOS for the time being. Except for web, which doesn't require npx expo prebuild since it uses the browser instead of a custom native runtime.

In the future, we would like to support additional platforms, such as react-native-macos and react-native-windows, but this is not currently a priority.

If you are targeting additional platforms, you can still use prebuild for your android, and ios projects — any extra platforms will have to be configured manually in the meantime.

Making changes directly is quicker than modularizing and automating

All native changes must be added with native modules (using React Native's built-in Native Module APIs or the Expo Modules API) and config plugins. This means if you want to quickly add a native file to your project to experiment, then you may be better off running prebuild and adding the file manually, then working your way back into the system with a monorepo. We plan to speed this process up by adding functionality to Expo Autolinking that finds project native files outside of the native folders and links them before building.

If you want to modify the configuration, such as the gradle.properties file, you'll have to write a plugin (example). This can be easily automated with helper plugin libraries, however, it is a bit slower if you need to do it often.

Config plugin support in the community

Not all packages support Expo Prebuilding yet. If you find a library that requires extra setup after installation and doesn't yet have a config plugin, we recommend opening a pull request or an issue so that the maintainer is aware of the feature request.

Many packages, such as react-native-blurhash, don't require any additional native configuration beyond what is handled by autolinking and so no config plugin is required.

Other packages, such as react-native-ble-plx, do require additional setup and therefore require a config plugin to be used with npx expo prebuild (in this case there's an external plugin called @config-plugins/react-native-ble-plx).

Alternatively, we also have a repo for out-of-tree config plugins which provides plugins for popular packages that haven't adopted the system yet. Think of this like DefinitelyTyped for TypeScript. We prefer packages ship their own config plugin, but if they haven't adopted the system yet, the community can use the packages listed in the repo.