Once you've acquired the CodePush plugin, you need to integrate it into the Visual Studio project of your React Native app and configure it correctly. To do this, take the following steps:
Once the plugin has been downloaded, run npx react-native autolink-windows in your application's root directory to automatically add the CodePush c++ project to your application's windows solution file.
-
Replace the following files located at
windows/<app name>with those in the CodePushDemoAppCpp example app in this repo found atExamples/CodePushDemoAppCpp/windows/CodePushDemoAppCpp:- app.h
- app.cpp
- app.xaml
-
In the above files, replace any occurance of
CodePushDemoAppCppwith the name of your application -
Enter your application's app version and deployment key to the
configMapobject at the top of your app'sOnLaunchedmethod inApp.cpp:
...
void App::OnLaunched(activation::LaunchActivatedEventArgs const& e)
{
winrt::Microsoft::CodePush::ReactNative::CodePushConfig::SetHost(Host());
auto configMap{ winrt::single_threaded_map<hstring, hstring>() };
configMap.Insert(L"appVersion", L"1.0.0");
configMap.Insert(L"deploymentKey", L"<app deployment key>");
winrt::Microsoft::CodePush::ReactNative::CodePushConfig::Init(configMap);
...
}
...-
add name space
Microsoft.CodePushtoApp.xaml.cs -
add app version and deployment key to
configMapat the start of your app'sOnLaunchedmethod inApp.xaml.cs.
using Microsoft.CodePush;
...
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Microsoft.CodePush.ReactNative.CodePushConfig.SetHost(Host);
IDictionary<string, string> configMap = new Dictionary<string, string>();
configMap.Add("appVersion", "1.0.0");
configMap.Add("deploymentKey", "deployment key");
Microsoft.CodePush.ReactNative.CodePushConfig.Init(configMap);
...
}
...-
Open the Visual Studio solution located at
windows-legacy\<AppName>\<AppName>.slnwithin your app -
Right-click the solution node in the
Solution Explorerwindow and select theAdd -> Existing Project...menu item -
Browse to the
node_modules\react-native-code-push\windowsdirectory, select theCodePush.csprojfile and clickOK -
Back in the
Solution Explorer, right-click the project node that is named after your app, and select theAdd -> Reference...menu item -
Select the
Projectstab on the left hand side, check theCodePushitem and then clickOK
After installing the plugin, 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, update the MainReactNativeHost.cs file to use CodePush via the following changes:
...
// 1. Import the CodePush namespace
using CodePush.ReactNative;
...
class MainReactNativeHost : ReactNativeHost
{
// 2. Declare a private instance variable for the CodePushModule instance.
private CodePushReactPackage codePushReactPackage;
// 3. Update the JavaScriptBundleFile property to initalize the CodePush runtime,
// specifying the right deployment key, then use it to return the bundle URL from
// CodePush instead of statically from the binary. If you don't already have your
// deployment key, you can run "appcenter codepush deployment list -a <ownerName>/<appName> -k" to retrieve it.
protected override string JavaScriptBundleFile
{
get
{
codePushReactPackage = new CodePushReactPackage("deployment-key-here", this);
return codePushReactPackage.GetJavaScriptBundleFile();
}
}
// 4. Add the codePushReactPackage instance to the list of existing packages.
protected override List<IReactPackage> Packages
{
get
{
return new List<IReactPackage>
{
new MainReactPackage(),
...
codePushReactPackage
};
}
}
...
}