How To Use React Native Over The Air (OTA) Updates
Tags: reactnative, mobile
Published: 2021-07-21 15:17:34

<!-- wp:heading -->
<h2>What are Over The Air (OTA) updates? Why might I need them?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Over The Air (OTA) updates are one of the advantages of <a href="https://pagepro.co/react-native-development.html" target="_blank" rel="noreferrer noopener">React Native</a> apps over native ones. In React Native apps, code is split into two parts:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>Native binaries</strong> – the part with native code that is compiled</li><li><strong>Javascript bundle</strong> – the bundle with Javascript code and assets that is not compiled</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Normally when you want to update your app, you need first to build it and then send it to App Store or Google Play. A major drawback of this solution is the time needed for these stores to approve your update. Imagine a situation where an app with a critical error was published to users by mistake. In these cases, every second count. And this is where Over The Air (OTA) may help us.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In short, OTA allows us to send updated Javascript bundles directly to users without the need of accepting them by Apple or Google. We can fix errors or push small updates (more on those later) almost immediately. This gives you more confidence about the stability of the app – there is always something that we can do instead of just waiting for an update to be accepted while users can't use the app</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>This sounds great! Are there any limitations?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As good as it may sound, there are few things that we need to keep in mind.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The first thing is that OTA updates only work with Javascript bundles. You may think that this isn't really a big limitation because, in React Native, we write almost all code in JS. While this may be true, there still are modules that we install to our app. If a module changes your android or ios directories, you can't use OTA to update your app. That's because these directories' contents are used to compile Native binaries, which OTA can't update.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The second thing is what we can do with OTA. Technically we can change everything in our app, but App Store and Google Play limit us. Google Play is less restrictive – they say that apps installed from Google Play shouldn't update in other ways than by Google services, <a href="https://support.google.com/googleplay/android-developer/answer/9888379?hl=en" target="_blank" rel="noreferrer noopener nofollow">but Javascript bundles aren't restricted to do so</a>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Apple is more restrictive in these manners. <a href="https://developer.apple.com/programs/information/Apple_Developer_Program_Information_8_12_15.pdf" target="_blank" rel="noreferrer noopener nofollow">Take a look at paragraph 3.3.2</a>, which says that OTA updates can't make significant changes in the app. To keep it simple, we should use such updates only to fix bugs or make performance improvements without making significant changes to the app's UI or usability.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>How to start using OTA?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We need a server that will receive our updates and send them to users of our app to use OTA. While we can make our own server, we can also use dedicated solutions. In this article, we will use <a href="https://github.com/microsoft/react-native-code-push" rel="noreferrer noopener nofollow" target="_blank">CodePush </a>from Microsoft App Center.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Setting up CodePush on App Center</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>First, we have to create an app on the App Center. We choose Android as an OS (if you are also making an app for iOS, you need to create a second app) and React Native as a platform.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5610,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-10.png" alt="" class="wp-image-5610"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After creating our app, we go to Distribute -&gt; CodePush in the left menu. There we can create a new deployment for our app.<br></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":5612,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-12.png" alt="" class="wp-image-5612"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After this, we should have our deployment visible in CodePush. The last step is to go to settings (the wrench icon in the upper right corner) and check our deployment keys. We should have two because CodePush makes two branches by default. We will need these codes later when we are sending our updates.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5614,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-14.png" alt="" class="wp-image-5614"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Installing CodePush plugin</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's assume you already have React Native app. To add the CodePush plugin first, we have to install it using <code>yarn</code> or <code>npm</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"#using yarn\nyarn add react-native-code-push\n\n#using npm\nnpm install react-native-code-push\n\n#only for ios app\ncd ios \u0026\u0026 pod install","lang":"Bash"} -->
<pre class="wp-block-code-mind-code c-code"><code class="bash">#using yarn
yarn add react-native-code-push

#using npm
npm install react-native-code-push

#only for ios app
cd ios &amp;&amp; pod install</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>After installing the plugin, we need to set it up. Here we will only go with the Android setup. You can find the iOS setup <a href="https://github.com/microsoft/react-native-code-push/blob/master/docs/setup-ios.md" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First we need to add these lines at the end of  <code>android/settings.gradle</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"include ':app', ':react-native-code-push'\nproject(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')"} -->
<pre class="wp-block-code-mind-code c-code"><code class="javascript">include ':app', ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>Next we add <code>codepush.gradle</code> in <code>android/app/build.gradle</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"apply from: \u0022../../node_modules/react-native/react.gradle\u0022\n// add this line\napply from: \u0022../../node_modules/react-native-code-push/android/codepush.gradle\u0022"} -->
<pre class="wp-block-code-mind-code c-code"><code class="javascript">apply from: "../../node_modules/react-native/react.gradle"
// add this line
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>Now we update <code>MainApplication.java</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"...\n// 1. Import the plugin class.\nimport com.microsoft.codepush.react.CodePush;\n\npublic class MainApplication extends Application implements ReactApplication {\n\n    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {\n        ...\n\n        // 2. Override the getJSBundleFile method in order to let\n        // the CodePush runtime determine where to get the JS\n        // bundle location from on each app start\n        @Override\n        protected String getJSBundleFile() {\n            return CodePush.getJSBundleFile();\n        }\n    };\n}","lang":"Java"} -->
<pre class="wp-block-code-mind-code c-code"><code class="java">...
// 1. Import the plugin class.
import com.microsoft.codepush.react.CodePush;

public class MainApplication extends Application implements ReactApplication {

    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        ...

        // 2. Override the getJSBundleFile method in order to let
        // the CodePush runtime determine where to get the JS
        // bundle location from on each app start
        @Override
        protected String getJSBundleFile() {
            return CodePush.getJSBundleFile();
        }
    };
}</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>Next, we need to set is the deployment key. We have to tell the app which branch on App Center it should check for updates. To do so, we have to update <code>android\app\src\main\res\values\strings.xml</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"\u003cresources\u003e\n    \u003cstring name=\u0022app_name\u0022\u003eOtaExample\u003c/string\u003e\n    \u003cstring moduleConfig=\u0022true\u0022 name=\u0022CodePushDeploymentKey\u0022\u003eYOUR_KEY\u003c/string\u003e\n\u003c/resources\u003e\n","lang":"HTML"} -->
<pre class="wp-block-code-mind-code c-code"><code class="html">&lt;resources>
    &lt;string name="app_name">OtaExample&lt;/string>
    &lt;string moduleConfig="true" name="CodePushDeploymentKey">YOUR_KEY&lt;/string>
&lt;/resources>
</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>This is the key we obtain from CodePush on App Center. To update our app later, we have to push updates to the same branch that we connect here with the deployment key. You can find more info about using multiple deployments <a href="https://github.com/microsoft/react-native-code-push/blob/master/README.md#multi-deployment-testing" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Finally, we wrap our app root component with CodePush HOC.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"import codePush from 'react-native-code-push';\n\nconst App = () =\u003e {\n  return (\n    \u003c\u003e\n      \u003cStatusBar barStyle=\u0022dark-content\u0022 /\u003e\n      \u003cSafeAreaView\u003e\n        \u003cScrollView\n          contentInsetAdjustmentBehavior=\u0022automatic\u0022\n          style={styles.scrollView}\u003e\n          \u003cView style={styles.body}\u003e\n            \u003cText\u003eTest\u003c/Text\u003e\n          \u003c/View\u003e\n        \u003c/ScrollView\u003e\n      \u003c/SafeAreaView\u003e\n    \u003c/\u003e\n  );\n};\n\nexport default codePush(App);","lang":"JSX"} -->
<pre class="wp-block-code-mind-code c-code"><code class="jsx">import codePush from 'react-native-code-push';

const App = () => {
  return (
    &lt;>
      &lt;StatusBar barStyle="dark-content" />
      &lt;SafeAreaView>
        &lt;ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          &lt;View style={styles.body}>
            &lt;Text>Test&lt;/Text>
          &lt;/View>
        &lt;/ScrollView>
      &lt;/SafeAreaView>
    &lt;/>
  );
};

export default codePush(App);</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>By default, CodePush will check for updates on every app launch and install them in the background, which means that the user will get the changes on the next launch after the update. This behaviour focuses on the best TTI (time to interactive), so the user doesn't have to wait for the update to download and install before using the app. How to install updates, and how often they can be changed – more about you can find <a href="https://github.com/microsoft/react-native-code-push#plugin-usage" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Lets send our first update OTA!</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>After all these configurations, we are finally able to send OTA updates. To do so, we first have to install and login into CodePush globally. We need to log in only once.</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"#using yarn\nyarn global add appcenter-cli\n\n#using npm\nnpm install -g appcenter-cli\n\n#will open a dialog to login into appcenter\nappcenter login\n\n#to send update OTA\nappcenter codepush release-cordova -a \u003cownerName\u003e/\u003cappName\u003e","lang":"Bash"} -->
<pre class="wp-block-code-mind-code c-code"><code class="bash">#using yarn
yarn global add appcenter-cli

#using npm
npm install -g appcenter-cli

#will open a dialog to login into appcenter
appcenter login

#to send update OTA
appcenter codepush release-cordova -a &lt;ownerName>/&lt;appName></code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>So let's do it! First, let's make a release build of our react native app and install it on the device. More about building release APK for Android you can find <a href="https://reactnative.dev/docs/signed-apk-android#generating-the-release-apk" target="_blank" rel="noreferrer noopener nofollow">here</a>.<br></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":5616,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-15.png" alt="" class="wp-image-5616"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Above you can see our app launched on an emulator. Let's make some changes and send them Over The Air!</p>
<!-- /wp:paragraph -->

<!-- wp:code-mind/code {"code":"const App = () =\u003e {\n  return (\n    \u003c\u003e\n      \u003cStatusBar barStyle=\u0022dark-content\u0022 /\u003e\n      \u003cSafeAreaView\u003e\n        \u003cScrollView\n          contentInsetAdjustmentBehavior=\u0022automatic\u0022\n          style={styles.scrollView}\u003e\n          \u003cView style={styles.body}\u003e\n            \u003cText\u003eTest\u003c/Text\u003e\n            \u003cText\u003eTesting OTA updates!\u003c/Text\u003e\n          \u003c/View\u003e\n        \u003c/ScrollView\u003e\n      \u003c/SafeAreaView\u003e\n    \u003c/\u003e\n  );\n};","lang":"JSX"} -->
<pre class="wp-block-code-mind-code c-code"><code class="jsx">const App = () => {
  return (
    &lt;>
      &lt;StatusBar barStyle="dark-content" />
      &lt;SafeAreaView>
        &lt;ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          &lt;View style={styles.body}>
            &lt;Text>Test&lt;/Text>
            &lt;Text>Testing OTA updates!&lt;/Text>
          &lt;/View>
        &lt;/ScrollView>
      &lt;/SafeAreaView>
    &lt;/>
  );
};</code></pre>
<!-- /wp:code-mind/code -->

<!-- wp:paragraph -->
<p>We added <code>&lt;Text&gt;Testing OTA updates!&lt;/Text&gt;</code> to our app. Now let's push this change using the command above! Then we can launch our app (we won't see the changes first) and wait 10-15 seconds. After relaunching our app, we should see this:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":5617,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://pagepro.co/blog/wp-content/uploads/2021/02/image-16.png" alt="" class="wp-image-5617"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Conclusions</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>OTA updates are a powerful tool that you can add with ease to React Native apps. They give us another way to update which is almost immediate and invisible for users. Thanks to this, we can always fix our app even on production without waiting for App Store or Google Play to accept our update. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We have to keep in mind that we cannot update native libraries, which will be required in some situations and not making updates that change the UI or functionalities of our app because it's not allowed by Apple and Google policies.</p>
<!-- /wp:paragraph -->