Skip to content

Latest commit

History

History
466 lines (348 loc) · 19 KB

File metadata and controls

466 lines (348 loc) · 19 KB

Android Setup

In order to integrate CodePush into your Android project, please perform the following steps:

Plugin Installation and Configuration for React Native 0.60 version and above (Android)

  1. In your android/settings.gradle file, make the following additions at the end of the file:

    ...
    include ':app', ':react-native-code-push'
    project(':react-native-code-push').projectDir =newFile(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
  2. In your android/app/build.gradle file, add the codepush.gradle file as an additional build task definition underneath react.gradle:

    ...
    apply from: "../../node_modules/react-native/react.gradle"
    apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"...
  3. Update the MainApplication.java file to use CodePush via the following changes:

    ...
    // 1. Import the plugin class.importcom.microsoft.codepush.react.CodePush;
    publicclassMainApplicationextendsApplicationimplementsReactApplication {
    privatefinalReactNativeHostmReactNativeHost = newReactNativeHost(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@OverrideprotectedStringgetJSBundleFile() {
    returnCodePush.getJSBundleFile();
    }
    };
    }
  4. Add the Deployment key to strings.xml:

    To let the CodePush runtime know which deployment it should query for updates, open your app's strings.xml file and add a new string named CodePushDeploymentKey, whose value is the key of the deployment you want to configure this app against (like the key for the Staging deployment for the FooBar app). You can retrieve this value by running appcenter codepush deployment list -a <ownerName>/<appName> -k in the CodePush CLI (the -k flag is necessary since keys aren't displayed by default) and copying the value of the Key column which corresponds to the deployment you want to use (see below). Note that using the deployment's name (like Staging) will not work. The "friendly name" is intended only for authenticated management usage from the CLI, and not for public consumption within your app.

    Deployment list

    In order to effectively make use of the Staging and Production deployments that were created along with your CodePush app, refer to the multi-deployment testing docs below before actually moving your app's usage of CodePush into production.

    Your strings.xml should looks like this:

     <resources>
    <stringname="app_name">AppName</string>
    <stringmoduleConfig="true"name="CodePushDeploymentKey">DeploymentKey</string>
    </resources>

    Note: If you need to dynamically use a different deployment, you can also override your deployment key in JS code using Code-Push options

Plugin Installation for React Native lower than 0.60 (Android)

In order to accommodate as many developer preferences as possible, the CodePush plugin supports Android installation via two mechanisms:

  1. RNPM - React Native Package Manager (RNPM) is an awesome tool that provides the simplest installation experience possible for React Native plugins. If you're already using it, or you want to use it, then we recommend this approach.

  2. "Manual" - If you don't want to depend on any additional tools or are fine with a few extra installation steps (it's a one-time thing), then go with this approach.

*Note: Due to a code change from the React Native repository, if your installed React Native version ranges from 0.29 to 0.32, we recommend following the manual steps to set up correctly. *

Plugin Installation (Android - RNPM)

  1. As of v0.27 of React Native, rnpm link has already been merged into the React Native CLI. Simply run:

    react-native link react-native-code-push
    

    If your app uses a version of React Native that is lower than v0.27, run the following:

    rnpm link react-native-code-push
    

    Note: If you don't already have RNPM installed, you can do so by simply running npm i -g rnpm and then executing the above command.

  2. If you're using RNPM >=1.6.0, you will be prompted for the deployment key you'd like to use. If you don't already have it, you can retrieve this value by running appcenter codepush deployment list -a <ownerName>/<appName> -k, or you can choose to ignore it (by simply hitting <ENTER>) and add it in later. To get started, we would recommend just using your Staging deployment key, so that you can test out the CodePush end-to-end.

And that's it for installation using RNPM! Continue below to the Plugin Configuration section to complete the setup.

Plugin Installation (Android - Manual)

  1. In your android/settings.gradle file, make the following additions:

    include ':app', ':react-native-code-push'
    project(':react-native-code-push').projectDir =newFile(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
  2. In your android/app/build.gradle file, add the :react-native-code-push project as a compile-time dependency:

    ...
    dependencies {
    ...
    compile project(':react-native-code-push')
    }
  3. In your android/app/build.gradle file, add the codepush.gradle file as an additional build task definition underneath react.gradle:

    ...
    apply from: "../../node_modules/react-native/react.gradle"
    apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"...

Plugin Configuration for React Native lower than 0.60 (Android)

NOTE: If you used RNPM or react-native link to automatically link the plugin, these steps have already been done for you so you may skip this section.

After installing the plugin and syncing your Android Studio project with Gradle, you need to configure your app to consult CodePush for the location of your JS bundle, since it will "take control" of managing the current and all future versions. To do this:

For React Native v0.29 - v0.59

For newly created React Native application

If you are integrating Code Push into React Native application please do the following steps:

Update the MainApplication.java file to use CodePush via the following changes:

...
// 1. Import the plugin class.importcom.microsoft.codepush.react.CodePush;
publicclassMainApplicationextendsApplicationimplementsReactApplication {
privatefinalReactNativeHostmReactNativeHost = newReactNativeHost(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@OverrideprotectedStringgetJSBundleFile() {
returnCodePush.getJSBundleFile();
}
@OverrideprotectedList<ReactPackage> getPackages() {
// 3. Instantiate an instance of the CodePush runtime and add it to the list of// existing packages, specifying the right deployment key. If you don't already// have it, you can run "appcenter codepush deployment list -a <ownerName>/<appName> -k" to retrieve your key.returnArrays.<ReactPackage>asList(
newMainReactPackage(),
newCodePush("deployment-key-here", MainApplication.this, BuildConfig.DEBUG)
);
}
};
}

NOTE: For React Native v0.49+ please be sure that getJSMainModuleName function in the MainApplication.java file determines correct URL to fetch JS bundle (used when dev support is enabled, see this for more details) e.g.

@Override
protected String getJSMainModuleName() {
return "index";
}
For existing native application

If you are integrating React Native into existing native application please do the following steps:

Update MyReactActivity.java (it could be named differently in your app) file to use CodePush via the following changes:

...
// 1. Import the plugin class.importcom.microsoft.codepush.react.CodePush;
publicclassMyReactActivityextendsActivity {
privateReactRootViewmReactRootView;
privateReactInstanceManagermReactInstanceManager;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
...
mReactInstanceManager = ReactInstanceManager.builder()
// ...// Add CodePush package
.addPackage(newCodePush("deployment-key-here", getApplicationContext(), BuildConfig.DEBUG))
// Get the JS Bundle File via Code Push
.setJSBundleFile(CodePush.getJSBundleFile())
// ...
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null);
setContentView(mReactRootView);
}
...
}

For React Native v0.19 - v0.28

Update the MainActivity.java file to use CodePush via the following changes:

...
// 1. Import the plugin class (if you used RNPM to install the plugin, this// should already be done for you automatically so you can skip this step).importcom.microsoft.codepush.react.CodePush;
publicclassMainActivityextendsReactActivity {
// 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@OverrideprotectedStringgetJSBundleFile() {
returnCodePush.getJSBundleFile();
}
@OverrideprotectedList<ReactPackage> getPackages() {
// 3. Instantiate an instance of the CodePush runtime and add it to the list of// existing packages, specifying the right deployment key. If you don't already// have it, you can run "appcenter codepush deployment list -a <ownerName>/<appName> -k" to retrieve your key.returnArrays.<ReactPackage>asList(
newMainReactPackage(),
newCodePush("deployment-key-here", this, BuildConfig.DEBUG)
);
}
...
}

Background React Instances

This section is only necessary if you're explicitly launching a React Native instance without an Activity (for example, from within a native push notification receiver). For these situations, CodePush must be told how to find your React Native instance.

In order to update/restart your React Native instance, CodePush must be configured with a ReactInstanceHolder before attempting to restart an instance in the background. This is done in your Application implementation.

For React Native >= v0.29 (Background React Instances)

Update the MainApplication.java file to use CodePush via the following changes:

...
// 1. Declare your ReactNativeHost to extend ReactInstanceHolder. ReactInstanceHolder is a subset of ReactNativeHost, so no additional implementation is needed.importcom.microsoft.codepush.react.ReactInstanceHolder;
publicclassMyReactNativeHostextendsReactNativeHostimplementsReactInstanceHolder {
// ... usual overrides
}
// 2. Provide your ReactNativeHost to CodePush.publicclassMainApplicationextendsApplicationimplementsReactApplication {
privatefinalMyReactNativeHostmReactNativeHost = newMyReactNativeHost(this);
@OverridepublicvoidonCreate() {
CodePush.setReactInstanceHolder(mReactNativeHost);
super.onCreate();
}
}
For React Native v0.19 - v0.28 (Background React Instances)

Before v0.29, React Native did not provide a ReactNativeHost abstraction. If you're launching a background instance, you'll likely have built your own, which should now implement ReactInstanceHolder. Once that's done:

// 1. Provide your ReactInstanceHolder to CodePush.publicclassMainApplicationextendsApplication {
@OverridepublicvoidonCreate() {
// ... initialize your instance holderCodePush.setReactInstanceHolder(myInstanceHolder);
super.onCreate();
}
}

In order to effectively make use of the Staging and Production deployments that were created along with your CodePush app, refer to the multi-deployment testing docs below before actually moving your app's usage of CodePush into production.

WIX React Native Navigation applications

If you are using WIX React Native Navigation version 1.x based application, please do the following steps to integrate CodePush:

  1. No need to change MainActivity.java file, so if you are integrating CodePush to newly created RNN application it might be looking like this:
importcom.facebook.react.ReactActivity;
importcom.reactnativenavigation.controllers.SplashActivity;
publicclassMainActivityextendsSplashActivity {
}
  1. Update the MainApplication.java file to use CodePush via the following changes:
// ...importcom.facebook.react.ReactInstanceManager;
// Add CodePush importsimportcom.microsoft.codepush.react.ReactInstanceHolder;
importcom.microsoft.codepush.react.CodePush;
publicclassMainApplicationextendsNavigationApplicationimplementsReactInstanceHolder {
@OverridepublicbooleanisDebug() {
// Make sure you are using BuildConfig from your own applicationreturnBuildConfig.DEBUG;
}
protectedList<ReactPackage> getPackages() {
// Add additional packages you require herereturnArrays.<ReactPackage>asList(
newCodePush("deployment-key-here", getApplicationContext(), BuildConfig.DEBUG)
);
}
@OverridepublicList<ReactPackage> createAdditionalReactPackages() {
returngetPackages();
}
@OverridepublicStringgetJSBundleFile() {
// Override default getJSBundleFile method with the one CodePush is providingreturnCodePush.getJSBundleFile();
}
@OverridepublicStringgetJSMainModuleName() {
return"index";
}
@OverridepublicReactInstanceManagergetReactInstanceManager() {
// CodePush must be told how to find React Native instancereturngetReactNativeHost().getReactInstanceManager();
}
}

If you are using WIX React Native Navigation version 2.x based application, please do the following steps to integrate CodePush:

  1. As per React Native Navigation's documentation, MainActivity.java should extend NavigationActivity, no changes required to incorporate CodePush:
importcom.reactnativenavigation.NavigationActivity;
publicclassMainActivityextendsNavigationActivity {
}
  1. Update the MainApplication.java file to use CodePush via the following changes:
// ...importcom.facebook.react.ReactInstanceManager;
// Add CodePush importsimportcom.microsoft.codepush.react.CodePush;
publicclassMainApplicationextendsNavigationApplication {
@OverridepublicbooleanisDebug() {
returnBuildConfig.DEBUG;
}
@OverrideprotectedReactGatewaycreateReactGateway() {
ReactNativeHosthost = newNavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
@javax.annotation.Nullable@OverrideprotectedStringgetJSBundleFile() {
returnCodePush.getJSBundleFile();
}
};
returnnewReactGateway(this, isDebug(), host);
}
@OverridepublicList<ReactPackage> createAdditionalReactPackages() {
returnArrays.<ReactPackage>asList(
newCodePush("deployment-key-here", getApplicationContext(), isDebug())
//,MainReactPackage , etc...
}
}

Code Signing setup

Starting with CLI version 2.1.0 you can self sign bundles during release and verify its signature before installation of update. For more info about Code Signing please refer to relevant code-push documentation section. In order to use Public Key for Code Signing you need to do following steps:

Add CodePushPublicKey string item to /path_to_your_app/android/app/src/main/res/values/strings.xml. It may looks like this:

<resources>
<stringname="app_name">my_app</string>
<stringname="CodePushPublicKey">-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtPSR9lkGzZ4FR0lxF+ZA
P6jJ8+Xi5L601BPN4QESoRVSrJM08roOCVrs4qoYqYJy3Of2cQWvNBEh8ti3FhHu
tiuLFpNdfzM4DjAw0Ti5hOTfTixqVBXTJPYpSjDh7K6tUvp9MV0l5q/Ps3se1vud
M1/X6g54lIX/QoEXTdMgR+SKXvlUIC13T7GkDHT6Z4RlwxkWkOmf2tGguRcEBL6j
ww7w/3g0kWILz7nNPtXyDhIB9WLH7MKSJWdVCZm+cAqabUfpCFo7sHiyHLnUxcVY
OTw3sz9ceaci7z2r8SZdsfjyjiDJrq69eWtvKVUpredy9HtyALtNuLjDITahdh8A
zwIDAQAB
-----END PUBLIC KEY-----</string>
</resources>

For React Native <= v0.60 you should configure the CodePush instance to use this parameter using one of the following approaches

Using constructor
newCodePush(
"deployment-key",
getApplicationContext(),
BuildConfig.DEBUG,
R.string.CodePushPublicKey)
Using builder
newCodePushBuilder("deployment-key-here",getApplicationContext())
.setIsDebugMode(BuildConfig.DEBUG)
.setPublicKeyResourceDescriptor(R.string.CodePushPublicKey)
.build()