Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

React Native module for CodePush

License

NotificationsYou must be signed in to change notification settings

microsoft/react-native-code-push

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

appcenterbanner

Sign up With App Center to use CodePush

React Native Module for CodePush

Warning

React Native CodePush won't support new Architecture. In order to use this plugin on React Native versions starting from 0.76 you will need toopt out from new architecture.

Note: This README is only relevant to the latest version of our plugin. If you are using an older version, please switch to the relevant tag onour GitHub repo to view the docs for that particular version.

Switching tags

This plugin provides client-side integration for theCodePush service, allowing you to easily add a dynamic update experience to your React Native app(s).

How does it work?

A React Native app is composed of JavaScript files and any accompanyingimages, which are bundled together by themetro bundler and distributed as part of a platform-specific binary (i.e. an.ipa or.apk file). Once the app is released, updating either the JavaScript code (e.g. making bug fixes, adding new features) or image assets, requires you to recompile and redistribute the entire binary, which of course, includes any review time associated with the store(s) you are publishing to.

The CodePush plugin helps get product improvements in front of your end users instantly, by keeping your JavaScript and images synchronized with updates you release to the CodePush server. This way, your app gets the benefits of an offline mobile experience, as well as the "web-like" agility of side-loading updates as soon as they are available. It's a win-win!

In order to ensure that your end users always have a functioning version of your app, the CodePush plugin maintains a copy of the previous update, so that in the event that you accidentally push an update which includes a crash, it can automatically roll back. This way, you can rest assured that your newfound release agility won't result in users becoming blocked before you have a chance toroll back on the server. It's a win-win-win!

Note: Any product changes which touch native code (e.g. modifying yourAppDelegate.m/MainActivity.java file, adding a new plugin) cannot be distributed via CodePush, and therefore, must be updated via the appropriate store(s).

Supported React Native platforms

  • iOS (7+)
  • Android (4.1+) on TLS 1.2 compatible devices
  • Windows (UWP)

We try our best to maintain backwards compatibility of our plugin with previous versions of React Native, but due to the nature of the platform, and the existence of breaking changes between releases, it is possible that you need to use a specific version of the CodePush plugin in order to support the exact version of React Native you are using. The following table outlines which CodePush plugin versions officially support the respective React Native versions:

React Native version(s)Supporting CodePush version(s)
<0.14Unsupported
v0.14v1.3(introduced Android support)
v0.15-v0.18v1.4-v1.6(introduced iOS asset support)
v0.19-v0.28v1.7-v1.17(introduced Android asset support)
v0.29-v0.30v1.13-v1.17(RN refactored native hosting code)
v0.31-v0.33v1.14.6-v1.17(RN refactored native hosting code)
v0.34-v0.35v1.15-v1.17(RN refactored native hosting code)
v0.36-v0.39v1.16-v1.17(RN refactored resume handler)
v0.40-v0.42v1.17(RN refactored iOS header files)
v0.43-v0.44v2.0+(RN refactored uimanager dependencies)
v0.45v3.0+(RN refactored instance manager code)
v0.46v4.0+(RN refactored js bundle loader code)
v0.46-v0.53v5.1+(RN removed unused registration of JS modules)
v0.54-v0.55v5.3+(Android Gradle Plugin 3.x integration)
v0.56-v0.58v5.4+(RN upgraded versions for Android tools)
v0.59v5.6+(RN refactored js bundle loader code)
v0.60-v0.61v6.0+(RN migrated to Autolinking)
v0.62-v0.64v6.2+(RN removed LiveReload)
v0.65-v0.70v7.0+(RN updated iPhone-target-version)
v0.71v8.0+(RN moved to react-native-gradle-plugin)

NOTE:react-native-code-push versions lower thanv5.7.0 will stop working in the near future. You can find more information in ourdocumentation.

We work hard to respond to new RN releases, but they do occasionally break us. We will update this chart with each RN release, so that users can check to see what our "official" support is.

Supported Components

When using the React Native assets system (i.e. using therequire("./foo.png") syntax), the following list represents the set of core components (and props) that support having their referenced images and videos updated via CodePush:

ComponentProp(s)
Imagesource
MapView.Marker
(Requiresreact-native-maps>=O.3.2)
image
ProgressViewIOSprogressImage,trackImage
TabBarIOS.Itemicon,selectedIcon
ToolbarAndroid
(React Native 0.21.0+)
actions[].icon,logo,overflowIcon
Videosource

The following list represents the set of components (and props) that don't currently support their assets being updated via CodePush, due to their dependency on static images and videos (i.e. using the{ uri: "foo" } syntax):

ComponentProp(s)
SliderIOSmaximumTrackImage,minimumTrackImage,thumbImage,trackImage
Videosource

As new core components are released, which support referencing assets, we'll update this list to ensure users know what exactly they can expect to update using CodePush.

Note: CodePush only works with Video components when usingrequire in the source prop. For example:

<Videosource={require("./foo.mp4")}/>

Getting Started

Once you've followed the general-purpose"getting started" instructions for setting up your CodePush account, you can start CodePush-ifying your React Native app by running the following command from within your app's root directory:

npm install --save react-native-code-push

As with all other React Native plugins, the integration experience is different for iOS and Android, so perform the following setup steps depending on which platform(s) you are targeting. Note, if you are targeting both platforms it is recommended to create separate CodePush applications for each platform.

If you want to see how other projects have integrated with CodePush, you can check out the excellentexample apps provided by the community. Additionally, if you'd like to quickly familiarize yourself with CodePush + React Native, you can check out the awesome getting started videos produced byBilal Budhani and/orDeepak Sisodiya.

NOTE: This guide assumes you have used thereact-native init command to initialize your React Native project. As of March 2017, the commandcreate-react-native-app can also be used to initialize a React Native project. If using this command, please runnpm run eject in your project's home directory to get a project very similar to whatreact-native init would have created.

Then continue with installing the native module

Plugin Usage

With the CodePush plugin downloaded and linked, and your app asking CodePush where to get the right JS bundle from, the only thing left is to add the necessary code to your app to control the following policies:

  1. When (and how often) to check for an update? (for example app start, in response to clicking a button in a settings page, periodically at some fixed interval)

  2. When an update is available, how to present it to the end user?

The simplest way to do this is to "CodePush-ify" your app's root component. To do so, you can choose one of the following two options:

  • Option 1: Wrap your root component with thecodePush higher-order component:

    • For class component

      importcodePushfrom"react-native-code-push";classMyAppextendsComponent{}MyApp=codePush(MyApp);
    • For functional component

      importcodePushfrom"react-native-code-push";letMyApp:()=>React$Node=()=>{}MyApp=codePush(MyApp);
  • Option 2: Use theES7 decorator syntax:

    NOTE: Decorators are not yet supported in Babel 6.x pending proposal update. You may need to enable it by installing and usingbabel-preset-react-native-stage-0.

    • For class component

      importcodePushfrom"react-native-code-push";@codePushclassMyAppextendsComponent{}
    • For functional component

      importcodePushfrom"react-native-code-push";constMyApp:()=>React$Node=()=>{}exportdefaultcodePush(MyApp);

By default, CodePush will check for updates on every app start. If an update is available, it will be silently downloaded, and installed the next time the app is restarted (either explicitly by the end user or by the OS), which ensures the least invasive experience for your end users. If an available update is mandatory, then it will be installed immediately, ensuring that the end user gets it as soon as possible.

If you would like your app to discover updates more quickly, you can also choose to sync up with the CodePush server every time the app resumes from the background.

  • For class component

    letcodePushOptions={checkFrequency:codePush.CheckFrequency.ON_APP_RESUME};classMyAppextendsComponent{}MyApp=codePush(codePushOptions)(MyApp);
  • For functional component

    letcodePushOptions={checkFrequency:codePush.CheckFrequency.ON_APP_RESUME};letMyApp:()=>React$Node=()=>{}MyApp=codePush(codePushOptions)(MyApp);

Alternatively, if you want fine-grained control over when the check happens (like a button press or timer interval), you can callCodePush.sync() at any time with your desiredSyncOptions, and optionally turn off CodePush's automatic checking by specifying a manualcheckFrequency:

letcodePushOptions={checkFrequency:codePush.CheckFrequency.MANUAL};classMyAppextendsComponent{onButtonPress(){codePush.sync({updateDialog:true,installMode:codePush.InstallMode.IMMEDIATE});}render(){return(<View><TouchableOpacityonPress={this.onButtonPress}><Text>Check for updates</Text></TouchableOpacity></View>)}}MyApp=codePush(codePushOptions)(MyApp);

If you would like to display an update confirmation dialog (an "active install"), configure when an available update is installed (like force an immediate restart) or customize the update experience in any other way, refer to thecodePush() API reference for information on how to tweak this default behavior.

NOTE: If you are usingRedux andRedux Saga, you can alternatively use thereact-native-code-push-saga module, which allows you to customize whensync is called in a perhaps simpler/more idiomatic way.

Store Guideline Compliance

Android Google Play and iOS App Store have corresponding guidelines that have rules you should be aware of before integrating the CodePush solution within your application.

Google play

Third paragraph ofDevice and Network Abuse topic describe that updating source code by any method other than Google Play's update mechanism is restricted. But this restriction does not apply to updating javascript bundles.

This restriction does not apply to code that runs in a virtual machine and has limited access to Android APIs (such as JavaScript in a webview or browser).

That fully allow CodePush as it updates just JS bundles and can't update native code part.

App Store

Paragraph3.3.2, since back in 2015'sApple Developer Program License Agreement fully allowed performing over-the-air updates of JavaScript and assets - and in its latest version (20170605)downloadable here this ruling is even broader:

Interpreted code may be downloaded to an Application but only so long as such code: (a) does not change the primary purpose of the Application by providing features or functionality that are inconsistent with the intended and advertised purpose of the Application as submitted to the App Store, (b) does not create a store or storefront for other code or applications, and (c) does not bypass signing, sandbox, or other security features of the OS.

CodePush allows you to follow these rules in full compliance so long as the update you push does not significantly deviate your product from its original App Store approved intent.

To further remain in compliance with Apple's guidelines we suggest that App Store-distributed apps don't enable theupdateDialog option when callingsync, since in theApp Store Review Guidelines it is written that:

Apps must not force users to rate the app, review the app, download other apps, or other similar actions in order to access functionality, content, or use of the app.

This is not necessarily the case forupdateDialog, since it won't force the user to download the new version, but at least you should be aware of that ruling if you decide to show it.

Releasing Updates

Once your app is configured and distributed to your users, and you have made some JS or asset changes, it's time to release them. The recommended way to release them is using therelease-react command in the App Center CLI, which will bundle your JavaScript files, asset files, and release the update to the CodePush server.

NOTE: Before you can start releasing updates, please log into App Center by running theappcenter login command.

In its most basic form, this command only requires one parameter: your owner name + "/" + app name.

appcenter codepush release-react -a<ownerName>/<appName>appcenter codepush release-react -a<ownerName>/MyApp-iOSappcenter codepush release-react -a<ownerName>/MyApp-Android

Therelease-react command enables such a simple workflow because it provides many sensible defaults (like generating a release bundle, assuming your app's entry file on iOS is eitherindex.ios.js orindex.js). However, all of these defaults can be customized to allow incremental flexibility as necessary, which makes it a good fit for most scenarios.

# Release a mandatory update with a changelogappcenter codepush release-react -a<ownerName>/MyApp-iOS  -m --description"Modified the header color"# Release an update for an app that uses a non-standard entry file name, and also capture# the sourcemap file generated by react-native bundleappcenter codepush release-react -a<ownerName>/MyApp-iOS --entry-file MyApp.js --sourcemap-output ../maps/MyApp.map# Release a dev Android build to just 1/4 of your end usersappcenter codepush release-react -a<ownerName>/MyApp-Android  --rollout 25 --developmenttrue# Release an update that targets users running any 1.1.* binary, as opposed to# limiting the update to exact version name in the build.gradle fileappcenter codepush release-react -a<ownerName>/MyApp-Android  --target-binary-version"~1.1.0"

The CodePush client supports differential updates, so even though you are releasing your JS bundle and assets on every update, your end users will only actually download the files they need. The service handles this automatically so that you can focus on creating awesome apps and we can worry about optimizing end user downloads.

For more details about how therelease-react command works, as well as the various parameters it exposes, refer to theCLI docs. Additionally, if you would prefer to handle running thereact-native bundle command yourself, and therefore, want an even more flexible solution thanrelease-react, refer to therelease command for more details.

If you run into any issues, or have any questions/comments/feedback, you can ping us within the#code-push channel on Reactiflux,e-mail us and/or check out thetroubleshooting details below.

NOTE: CodePush updates should be tested in modes other than Debug mode. In Debug mode, React Native app always downloads JS bundle generated by packager, so JS bundle downloaded by CodePush does not apply.

Multi-Deployment Testing

In ourgetting started docs, we illustrated how to configure the CodePush plugin using a specific deployment key. However, in order to effectively test your releases, it is critical that you leverage theStaging andProduction deployments that are auto-generated when you first created your CodePush app (or any custom deployments you may have created). This way, you never release an update to your end users that you haven't been able to validate yourself.

NOTE: Our client-side rollback feature can help unblock users after installing a release that resulted in a crash, and server-side rollbacks (i.e.appcenter codepush rollback) allow you to prevent additional users from installing a bad release once it's been identified. However, it's obviously better if you can prevent an erroneous update from being broadly released in the first place.

Taking advantage of theStaging andProduction deployments allows you to achieve a workflow like the following (feel free to customize!):

  1. Release a CodePush update to yourStaging deployment using theappcenter codepush release-react command (orappcenter codepush release if you need more control)

  2. Run your staging/beta build of your app, sync the update from the server, and verify it works as expected

  3. Promote the tested release fromStaging toProduction using theappcenter codepush promote command

  4. Run your production/release build of your app, sync the update from the server and verify it works as expected

NOTE: If you want to take a more cautious approach, you can even choose to perform a "staged rollout" as part of #3, which allows you to mitigate additional potential risk with the update (like did your testing in #2 touch all possible devices/conditions?) by only making the production update available to a percentage of your users (for exampleappcenter codepush promote -a <ownerName>/<appName> -s Staging -d Production -r 20). Then, after waiting for a reasonable amount of time to see if any crash reports or customer feedback comes in, you can expand it to your entire audience by runningappcenter codepush patch -a <ownerName>/<appName> Production -r 100.

You'll notice that the above steps refer to a "staging build" and "production build" of your app. If your build process already generates distinct binaries per "environment", then you don't need to read any further, since swapping out CodePush deployment keys is just like handling environment-specific config for any other service your app uses (like Facebook). However, if you're looking for examples (including demo projects) on how to setup your build process to accommodate this, then refer to the following sections, depending on the platform(s) your app is targeting:

Dynamic Deployment Assignment

The above section illustrated how you can leverage multiple CodePush deployments in order to effectively test your updates before broadly releasing them to your end users. However, since that workflow statically embeds the deployment assignment into the actual binary, a staging or production build will only ever sync updates from that deployment. In many cases, this is sufficient, since you only want your team, customers, stakeholders, etc. to sync with your pre-production releases, and therefore, only they need a build that knows how to sync with staging. However, if you want to be able to perform A/B tests, or provide early access of your app to certain users, it can prove very useful to be able to dynamically place specific users (or audiences) into specific deployments at runtime.

In order to achieve this kind of workflow, all you need to do is specify the deployment key you want the current user to syncronize with when calling thecodePush method. When specified, this key will override the "default" one that was provided in your app'sInfo.plist (iOS) orMainActivity.java (Android) files. This allows you to produce a build for staging or production, that is also capable of being dynamically "redirected" as needed.

// Imagine that "userProfile" is a prop that this component received// which includes the deployment key that the current user should use.codePush.sync({deploymentKey:userProfile.CODEPUSH_KEY});

With that change in place, now it's just a matter of choosing how your app determines the right deployment key for the current user. In practice, there are typically two solutions for this:

  1. Expose a user-visible mechanism for changing deployments at any time. For example, your settings page could have a toggle for enabling "beta" access. This model works well if you're not concerned with the privacy of your pre-production updates, and you have power users that may want to opt-in to earlier (and potentially buggy) updates at their own will (kind of like Chrome channels). However, this solution puts the decision in the hands of your users, which doesn't help you perform A/B tests transparently.

  2. Annotate the server-side profile of your users with an additional piece of metadata that indicates the deployment they should sync with. By default, your app could just use the binary-embedded key, but after a user has authenticated, your server can choose to "redirect" them to a different deployment, which allows you to incrementally place certain users or groups in different deployments as needed. You could even choose to store the server-response in local storage so that it becomes the new default. How you store the key alongside your user's profiles is entirely up to your authentication solution (for example Auth0, Firebase, custom DB + REST API), but is generally pretty trivial to do.

NOTE: If needed, you could also implement a hybrid solution that allowed your end-users to toggle between different deployments, while also allowing your server to override that decision. This way, you have a hierarchy of "deployment resolution" that ensures your app has the ability to update itself out-of-the-box, your end users can feel rewarded by getting early access to bits, but you also have the ability to run A/B tests on your users as needed.

Since we recommend using theStaging deployment for pre-release testing of your updates (as explained in the previous section), it doesn't neccessarily make sense to use it for performing A/B tests on your users, as opposed to allowing early-access (as explained in option #1 above). Therefore, we recommend making full use of custom app deployments, so that you can segment your users however makes sense for your needs. For example, you could create long-term or even one-off deployments, release a variant of your app to it, and then place certain users into it in order to see how they engage.

// #1) Create your new deployment to hold releases of a specific app variantappcentercodepushdeploymentadd-a<ownerName>/<appName>test-variant-one// #2) Target any new releases at that custom deploymentappcentercodepushrelease-react-a<ownerName>/<appName>-dtest-variant-one

NOTE: The total user count that is reported in your deployment's "Install Metrics" will take into account users that have "switched" from one deployment to another. For example, if yourProduction deployment currently reports having 1 total user, but you dynamically switch that user toStaging, then theProduction deployment would report 0 total users, whileStaging would report 1 (the user that just switched). This behavior allows you to accurately track your release adoption, even in the event of using a runtime-based deployment redirection solution.


API Reference

Example Apps / Starters

The React Native community has graciously created some awesome open source apps that can serve as examples for developers that are getting started. The following is a list of OSS React Native apps that are also using CodePush, and can therefore be used to see how others are using the service:

Additionally, if you're looking to get started with React Native + CodePush, and are looking for an awesome starter kit, you should check out the following:

Note: If you've developed a React Native app using CodePush, that is also open-source, please let us know. We would love to add it to this list!

Debugging / Troubleshooting

Thesync method includes a lot of diagnostic logging out-of-the-box, so if you're encountering an issue when using it, the best thing to try first is examining the output logs of your app. This will tell you whether the app is configured correctly (like can the plugin find your deployment key?), if the app is able to reach the server, if an available update is being discovered, if the update is being successfully downloaded/installed, etc. We want to continue improving the logging to be as intuitive/comprehensive as possible, so pleaselet us know if you find it to be confusing or missing anything.

The simplest way to view these logs is to add the flag--debug for each command. This will output a log stream that is filtered to just CodePush messages. This makes it easy to identify issues, without needing to use a platform-specific tool, or wade through a potentially high volume of logs.

screen shot 2016-06-21 at 10 15 42 am

Additionally, you can also use any of the platform-specific tools to view the CodePush logs, if you are more comfortable with them. Simple start up the Chrome DevTools Console, the Xcode Console (iOS), theOS X Console (iOS) and/or ADB logcat (Android), and look for messages which are prefixed with[CodePush].

Note that by default, React Native logs are disabled on iOS in release builds, so if you want to view them in a release build, you need to make the following changes to yourAppDelegate.m file:

  1. Add an#import <React/RCTLog.h> statement. For RN < v0.40 use:#import "RCTLog.h"

  2. Add the following statement to the top of yourapplication:didFinishLaunchingWithOptions method:

    RCTSetLogThreshold(RCTLogLevelInfo);

Now you'll be able to see CodePush logs in either debug or release mode, on both iOS or Android. If examining the logs don't provide an indication of the issue, please refer to the following common issues for additional resolution ideas:

Issue / SymptomPossible Solution
Compilation ErrorDouble-check that your version of React Native iscompatible with the CodePush version you are using.
Network timeout / hang when callingsync orcheckForUpdate in the iOS SimulatorTry resetting the simulator by selecting theSimulator -> Reset Content and Settings.. menu item, and then re-running your app.
Server responds with a404 when callingsync orcheckForUpdateDouble-check that the deployment key you added to yourInfo.plist (iOS),build.gradle (Android) or that you're passing tosync/checkForUpdate, is in fact correct. You can runappcenter codepush deployment list <ownerName>/<appName> --displayKeys to view the correct keys for your app deployments.
Update not being discoveredDouble-check that the version of your running app (like1.0.0) matches the version you specified when releasing the update to CodePush. Additionally, make sure that you are releasing to the same deployment that your app is configured to sync with.
Update not being displayed after restartIf you're not callingsync on app start (like withincomponentDidMount of your root component), then you need to explicitly callnotifyApplicationReady on app start, otherwise, the plugin will think your update failed and roll it back.
I've released an update for iOS but my Android app also shows an update and it breaks itBe sure you have different deployment keys for each platform in order to receive updates correctly
I've released new update but changes are not reflectedBe sure that you are running app in modes other than Debug. In Debug mode, React Native app always downloads JS bundle generated by packager, so JS bundle downloaded by CodePush does not apply.
No JS bundle is being found when running your app against the iOS simulatorBy default, React Native doesn't generate your JS bundle when running against the simulator. Therefore, if you're using[CodePush bundleURL], and targetting the iOS simulator, you may be getting anil result. This issue will be fixed in RN 0.22.0, but only for release builds. You can unblock this scenario right now by makingthis change locally.

Continuous Integration / Delivery

In addition to being able to use the CodePush CLI to "manually" release updates, we believe that it's important to create a repeatable and sustainable solution for contiously delivering updates to your app. That way, it's simple enough for you and/or your team to create and maintain the rhythm of performing agile deployments. In order to assist with setting up a CodePush-based CD pipeline, refer to the following integrations with various CI servers:

Additionally, if you'd like more details of what a complete mobile CI/CD workflow can look like, which includes CodePush, check out thisexcellent article by theZeeMee engineering team.

TypeScript Consumption

This module ships its*.d.ts file as part of its NPM package, which allows you to simplyimport it, and receive intellisense in supporting editors (like Visual Studio Code), as well as compile-time type checking if you're using TypeScript. For the most part, this behavior should just work out of the box, however, if you've specifiedes6 as the value for either thetarget ormodulecompiler option in yourtsconfig.json file, then just make sure that you also set themoduleResolution option tonode. This ensures that the TypeScript compiler will look within thenode_modules for the type definitions of imported modules. Otherwise, you'll get an error like the following when trying to import thereact-native-code-push module:error TS2307: Cannot find module 'react-native-code-push'.


This project has adopted theMicrosoft Open Source Code of Conduct. For more information see theCode of Conduct FAQ or contactopencode@microsoft.com with any additional questions or comments.


[8]ページ先頭

©2009-2025 Movatter.jp