# Configuring your mobile app for CodePush

Configure the CodePush SDK in your React Native app to receive over-the-air updates. Both bare React Native and Expo projects are supported: see [Configuring CodePush for bare React Native projects](#configuring-codepush-for-bare-react-native-projects) and [Configuring CodePush for Expo apps](#configuring-codepush-for-expo-apps).

The CodePush SDK [is open-source](https://github.com/bitrise-io/react-native-code-push) and [feedback is welcome](https://github.com/bitrise-io/react-native-code-push/issues/new).

## Configuring CodePush for Expo apps

:::note[Continuous Native Generation]

This guide is for projects which adopted Expo's [Continuous Native Generation](https://docs.expo.dev/workflow/continuous-native-generation/) feature.

If you manually manage the native iOS and Android projects within your codebase (no Expo Prebuild), follow our [bare React Native setup](#configuring-codepush-for-bare-react-native-projects) instructions instead.

:::

1. Make sure your app's bundle ID and package name match the ones used while [creating the apps](/release-management/getting-started-with-release-management/adding-a-new-app-to-release-management) on Bitrise. In `app.json`, these are the `ios.bundleIdentifier` and `android.package` fields.
1. Install the CodePush app SDK:

   ```bash
   npm install @bitrise/code-push-sdk
   ```
1. Add the Expo CodePush plugin to `app.json`:

     ```json
     {
        "plugins": [
           // ...existing plugins
           [
              "@bitrise/code-push-sdk/expo",
              {
                 "ios": {
                    "CodePushDeploymentKey": "...",
                    "CodePushServerURL": "https://<workspace-slug>.codepush.bitrise.io"
                 },
                 "android": {
                    "CodePushDeploymentKey": "...",
                    "CodePushServerURL": "https://<workspace-slug>.codepush.bitrise.io"
                 }
              }
           ]
        ]
     }
     ```

   **Deployment key**: Copy the value from the Bitrise CodePush deployments page. You get the deployment key when creating the [CodePush deployment on Bitrise](/release-management/codepush/creating-a-codepush-deployment).

   **Workspace slug**: The server URL requires your Bitrise workspace slug: [Identifying Workspaces and apps with their slugs](/bitrise-ci/api/identifying-workspaces-and-apps-with-their-slugs).

   :::note[Deployment keys are not secrets]

   These end up as plain string values in final app builds. They are not secrets, but they are unique to your workspace and your CodePush deployment setup.

   :::

1. Verify configuration by running `npx expo prebuild`. You can manually verify the following generated files:
   - `ios/<projectname>/Info.plist`: contains `CodePushDeploymentKey` and `CodePushServerURL` entries.
   - `ios/<projectname>/AppDelegate.swift`: contains the `CodePush` import and `ReactNativeDelegate.bundleURL()` method override.
   - `android/app/src/main/res/values/strings.xml`: contains `CodePushDeploymentKey` and `CodePushServerURL` entries.
   - `android/app/src/main/java/.../MainApplication.kt`: `jsBundleFilePath` set to `CodePush.getJSBundleFile()`.
1. Initialize the update check in your app's root component: [Customizing the update lifecycle](#customizing-the-update-lifecycle).

## Configuring CodePush for bare React Native projects

These instructions cover React Native's New Architecture. On older React Native versions, follow the SDK's [iOS setup](https://github.com/bitrise-io/react-native-code-push/blob/master/docs/setup-ios.md) and [Android setup](https://github.com/bitrise-io/react-native-code-push/blob/master/docs/setup-android.md) guides instead — then come back to this page for the deployment key and server URL values.

1. Add the CodePush SDK to the project:

   ```bash
   npm install @bitrise/code-push-sdk
   ```
1. Set up the CodePush iOS SDK in your project: [iOS setup](#ios-setup).
1. Set up the CodePush Android SDK in your project: [Android setup](#android-setup).
1. Initialize the update check in your app's root component: [Customizing the update lifecycle](#customizing-the-update-lifecycle).

### iOS setup

1. Run `bundle exec pod install` from the `ios` folder to pick up the previously added CodePush SDK dependency.
1. Update `ios/<projectname>/SceneDelegate.swift`:

   1. Add an import statement for CodePush headers:

      ```swift
      import CodePush
      ```
   1. In `class SceneDelegate`, find the line that returns the bundle URL for release builds and replace it with a call to the CodePush SDK:

      ```diff
        override func bundleURL() -> URL? {
        #if DEBUG
          RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
        #else
      -   Bundle.main.url(forResource: "main", withExtension: "jsbundle")
      +   CodePush.bundleURL()
        #endif
        }
      ```

   Use CodePush to resolve the JS bundle location only in release builds. The `DEBUG` pre-processor macro switches between the Metro packager in debug builds and CodePush in release builds, so React Native DevTools and Fast Refresh keep working while you debug.

   :::note[Apps that still use AppDelegate]

   Apps created with React Native 0.88 or later initialize React Native in `SceneDelegate.swift`, because the iOS 27 SDK requires the UIScene lifecycle. If your app still initializes React Native in `AppDelegate.swift` (created with 0.87 or earlier and not yet moved to the UIScene lifecycle), the `bundleURL()` method is in `class ReactNativeDelegate` in `ios/<projectname>/AppDelegate.swift`. Make the same two changes in that file.

   :::

1. Update `ios/<projectname>/Info.plist` with the CodePush deployment key and the CodePush server URL to let the CodePush runtime know which deployment it should query for updates against.

   ```xml
   <key>CodePushDeploymentKey</key>
   <string></string>
   <key>CodePushServerURL</key>
   <string>https://<workspace-slug>.codepush.bitrise.io</string>
   ```

   **Deployment key**: Copy the value from the Bitrise CodePush deployments page. You get the deployment key when creating the [CodePush deployment on Bitrise](/release-management/codepush/creating-a-codepush-deployment).

   **Workspace slug**: The server URL requires your Bitrise workspace slug: [Identifying Workspaces and apps with their slugs](/bitrise-ci/api/identifying-workspaces-and-apps-with-their-slugs).

   :::note[Deployment keys are not secrets]

   These end up as plain string values in final app builds. They are not secrets, but they are unique to your workspace and your CodePush deployment setup.

   :::

1. Optional: If your iOS minimum deployment target is lower than 15.5, you need to bump it to at least `15.5`. In `ios/Podfile`, look for the line `platform :ios, min_ios_version_supported`. Change it to `platform :ios, '15.5'`.

### Android setup

1. Edit the app module's Gradle build script at `android/app/build.gradle`. Add this at the end of the file as an additional build task definition:

   ```groovy
   ...
   apply from: "../../node_modules/@bitrise/code-push-sdk/android/codepush.gradle"
   ...
   ```
1. Update your Application class at `android/app/src/main/java/…/MainApplication.kt` to hook into the CodePush runtime.

   ```diff
   + import com.microsoft.codepush.react.CodePush

     class MainApplication : Application(), ReactApplication {

        override val reactHost: ReactHost by lazy {
           getDefaultReactHost(
                 context = applicationContext,
                 packageList = PackageList(this).packages.apply {
                    // Packages that cannot be autolinked yet can be added manually here, for example:
                    // add(MyReactNativePackage())
                 },
   +             // Set jsBundleFilePath to CodePush so CodePush resolves the JS bundle path
   +             // at startup (OTA update if available, fallback to bundled JS otherwise).
   +             jsBundleFilePath = CodePush.getJSBundleFile(),
           )
        }
     }
   ```
1. In `android/app/src/main/res/values/strings.xml`, add the CodePush deployment key and the CodePush server URL to let the CodePush runtime know which deployment it should query for updates against.

   ```xml
   <string name="CodePushDeploymentKey" translatable="false">...</string>
   <string name="CodePushServerUrl" translatable="false">https://<workspace-slug>.codepush.bitrise.io</string>
   ```

   **Deployment key**: Copy the value from the Bitrise CodePush deployments page. You get the deployment key when creating the [CodePush deployment on Bitrise](/release-management/codepush/creating-a-codepush-deployment).

   **Workspace slug**: The server URL requires your Bitrise workspace slug: [Identifying Workspaces and apps with their slugs](/bitrise-ci/api/identifying-workspaces-and-apps-with-their-slugs).

   :::note[Deployment keys are not secrets]

   These end up as plain string values in final app builds. They are not secrets, but they are unique to your workspace and your CodePush deployment setup.

   :::

## Customizing the update lifecycle

After you complete the configuration steps, CodePush hooks into the app's lifecycle and can load an updated bundle instead of the one packaged into the app.

Customize when and how updates are installed by calling `codePush.sync()` from your app's root component. The SDK provides several options to customize the update experience.

:::note[API reference]

The [CodePush JS API reference](https://github.com/bitrise-io/react-native-code-push/blob/master/docs/api-js.md) lists all options and parameters.

:::

You can set this up in several ways.

### Silent sync on app start

The simplest, default behavior. Your app automatically downloads available updates and applies them the next time it restarts. This way, the entire update experience is silent to the end user, since they don't see any update dialog.

```javascript
import { useEffect } from "react";
import codePush from "@bitrise/code-push-sdk";

function App() {
    useEffect(() => {
        // Fully silent update which keeps the app in
        // sync with the server, without ever
        // interrupting the end user
        codePush.sync();
    }, []);

    return <YourAppContent />;
}
```

### Silent sync every time the app resumes

Same as the previous, except the app checks for updates, or applies an update if one exists every time the app returns to the foreground.

```javascript
import { useEffect } from "react";
import { AppState } from "react-native";
import codePush from "@bitrise/code-push-sdk";

function App() {
    useEffect(() => {
        const syncOptions = {
           installMode: codePush.InstallMode.ON_NEXT_RESUME,
        };
        codePush.sync(syncOptions);

        const subscription = AppState.addEventListener("change", (newState) => {
            if (newState === "active") {
                codePush.sync(syncOptions);
            }
        });

        return () => subscription.remove();
    }, []);

    return <YourAppContent />;
}
```

### Interactive

When an update is available, prompt the end user for permission before downloading it, and then immediately apply the update. If an update sets the `mandatory` flag, the end user is still notified about the update, but they don't have the choice to ignore it.

```javascript
import { useEffect } from "react";
import codePush from "@bitrise/code-push-sdk";

function App() {
    useEffect(() => {
        // Active update, which lets the end user know
        // about each update, and displays it to them
        // immediately after downloading it
        codePush.sync({
           updateDialog: true,
           installMode: codePush.InstallMode.IMMEDIATE
        });
    }, []);

    return <YourAppContent />;
}
```

### Log/display progress

Pass the `syncStatusChangedCallback` and/or `downloadProgressCallback` arguments to `sync` to log the different stages of the process, or even display a progress bar to the user.

```javascript
import { useEffect, useState } from "react";
import codePush from "@bitrise/code-push-sdk";

function App() {
    const [status, setStatus] = useState(null);

    useEffect(() => {
        codePush.sync(
            {},
            (syncStatus) => setStatus(syncStatus),
            ({ receivedBytes, totalBytes }) => {
                console.log(`${receivedBytes} of ${totalBytes} received.`);
            }
        );
    }, []);

    return <YourAppContent status={status} />;
}
```
