Skip to content

Commit 07e270d

Browse files
author
Richard Hua
authored
Merge pull request #447 from Microsoft/override-app-version
Allow an app's binary version to be manually specified
2 parents f058495 + 23e7b18 commit 07e270d

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,8 @@ Because of this behavior, you can safely deploy updates to both the app store(s)
11051105
11061106
- __(NSURL \*)bundleURLForResource:(NSString \*)resourceName withExtension:(NSString \*)resourceExtension__: Equivalent to the `bundleURLForResource:` method, but also allows customizing the extension used by the JS bundle that is looked for within the app binary. This is useful if you aren't naming this file `*.jsbundle` (which is the default convention).
11071107
1108+
- __(void)overrideAppVersion:(NSString \*)appVersionOverride__ - Sets the version of the application's binary interface, which would otherwise default to the App Store version specified as the `CFBundleShortVersionString` in the `Info.plist`. This should be called a single time, before the bundle URL is loaded.
1109+
11081110
- __(void)setDeploymentKey:(NSString \*)deploymentKey__ - Sets the deployment key that the app should use when querying for updates. This is a dynamic alternative to setting the deployment key in your `Info.plist` and/or specifying a deployment key in JS when calling `checkForUpdate` or `sync`.
11091111
11101112
### Java API Reference (Android)
@@ -1131,6 +1133,8 @@ Constructs the CodePush client runtime and represents the `ReactPackage` instanc
11311133
11321134
- __getBundleUrl(String bundleName)__ - Returns the path to the most recent version of your app's JS bundle file, using the specified resource name (e.g. `index.android.bundle`). This method has the same resolution behavior as the Objective-C equivalent described above.
11331135
1136+
- __overrideAppVersion(String appVersionOverride)__ - Sets the version of the application's binary interface, which would otherwise default to the Play Store version specified as the `versionName` in the `build.gradle`. This should be called a single time, before the CodePush instance is constructed.
1137+
11341138
## Example Apps / Starters
11351139
11361140
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:

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class CodePush implements ReactPackage {
2929
private static boolean sIsRunningBinaryVersion = false;
3030
private static boolean sNeedToReportRollback = false;
3131
private static boolean sTestConfigurationFlag = false;
32+
private static String sAppVersion = null;
3233

3334
private boolean mDidUpdate = false;
3435

@@ -40,7 +41,6 @@ public class CodePush implements ReactPackage {
4041
private SettingsManager mSettingsManager;
4142

4243
// Config properties.
43-
private String mAppVersion;
4444
private String mDeploymentKey;
4545
private String mServerUrl = "https://codepush.azurewebsites.net/";
4646

@@ -63,11 +63,13 @@ public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
6363
mIsDebugMode = isDebugMode;
6464
mSettingsManager = new SettingsManager(mContext);
6565

66-
try {
67-
PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
68-
mAppVersion = pInfo.versionName;
69-
} catch (PackageManager.NameNotFoundException e) {
70-
throw new CodePushUnknownException("Unable to get package info for " + mContext.getPackageName(), e);
66+
if (sAppVersion == null) {
67+
try {
68+
PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
69+
sAppVersion = pInfo.versionName;
70+
} catch (PackageManager.NameNotFoundException e) {
71+
throw new CodePushUnknownException("Unable to get package info for " + mContext.getPackageName(), e);
72+
}
7173
}
7274

7375
mCurrentInstance = this;
@@ -96,7 +98,7 @@ public boolean didUpdate() {
9698
}
9799

98100
public String getAppVersion() {
99-
return mAppVersion;
101+
return sAppVersion;
100102
}
101103

102104
public String getAssetsBundleFileName() {
@@ -177,14 +179,14 @@ public String getJSBundleFileInternal(String assetsBundleFileName) {
177179
String packageAppVersion = CodePushUtils.tryGetString(packageMetadata, "appVersion");
178180
if (binaryModifiedDateDuringPackageInstall != null &&
179181
binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime &&
180-
(isUsingTestConfiguration() || this.mAppVersion.equals(packageAppVersion))) {
182+
(isUsingTestConfiguration() || sAppVersion.equals(packageAppVersion))) {
181183
CodePushUtils.logBundleUrl(packageFilePath);
182184
sIsRunningBinaryVersion = false;
183185
return packageFilePath;
184186
} else {
185187
// The binary version is newer.
186188
this.mDidUpdate = false;
187-
if (!this.mIsDebugMode || !this.mAppVersion.equals(packageAppVersion)) {
189+
if (!this.mIsDebugMode || !sAppVersion.equals(packageAppVersion)) {
188190
this.clearUpdates();
189191
}
190192

@@ -249,6 +251,10 @@ boolean needToReportRollback() {
249251
return sNeedToReportRollback;
250252
}
251253

254+
public static void overrideAppVersion(String appVersionOverride) {
255+
sAppVersion = appVersionOverride;
256+
}
257+
252258
private void rollbackPackage() {
253259
WritableMap failedPackage = mUpdateManager.getCurrentPackage();
254260
mSettingsManager.saveFailedUpdate(failedPackage);
@@ -280,7 +286,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactAppli
280286
CodePushNativeModule codePushModule = new CodePushNativeModule(reactApplicationContext, this, mUpdateManager, mTelemetryManager, mSettingsManager);
281287
CodePushDialog dialogModule = new CodePushDialog(reactApplicationContext);
282288

283-
List<NativeModule> nativeModules = new ArrayList<>();
289+
List<NativeModule> nativeModules = new ArrayList<>();
284290
nativeModules.add(codePushModule);
285291
nativeModules.add(dialogModule);
286292
return nativeModules;

ios/CodePush/CodePush.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
+ (NSString *)bundleAssetsPath;
3131

3232
/*
33-
* This methods allows dynamically setting the app's
33+
* This method allows the version of the app's binary interface
34+
* to be specified, which would otherwise default to the
35+
* App Store version of the app.
36+
*/
37+
+ (void)overrideAppVersion:(NSString *)deploymentKey;
38+
39+
/*
40+
* This method allows dynamically setting the app's
3441
* deployment key, in addition to setting it via
3542
* the Info.plist file's CodePushDeploymentKey setting.
3643
*/
@@ -45,7 +52,7 @@
4552

4653
@interface CodePushConfig : NSObject
4754

48-
@property (readonly) NSString *appVersion;
55+
@property (copy) NSString *appVersion;
4956
@property (readonly) NSString *buildVersion;
5057
@property (readonly) NSDictionary *configuration;
5158
@property (copy) NSString *deploymentKey;

ios/CodePush/CodePush.m

+6-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ + (NSString *)bundleAssetsPath
7373
if (bundleResourceSubdirectory) {
7474
resourcePath = [resourcePath stringByAppendingPathComponent:bundleResourceSubdirectory];
7575
}
76-
76+
7777
return [resourcePath stringByAppendingPathComponent:[CodePushUpdateUtils assetsFolderName]];
7878
}
7979

@@ -159,6 +159,11 @@ + (NSString *)getApplicationSupportDirectory
159159
return applicationSupportDirectory;
160160
}
161161

162+
+ (void)overrideAppVersion:(NSString *)appVersion
163+
{
164+
[CodePushConfig current].appVersion = appVersion;
165+
}
166+
162167
+ (void)setDeploymentKey:(NSString *)deploymentKey
163168
{
164169
[CodePushConfig current].deploymentKey = deploymentKey;

ios/CodePush/CodePushConfig.m

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ - (NSString *)clientUniqueId
8686
return [_configDictionary objectForKey:ClientUniqueIDConfigKey];
8787
}
8888

89+
- (void)setAppVersion:(NSString *)appVersion
90+
{
91+
[_configDictionary setValue:appVersion forKey:AppVersionConfigKey];
92+
}
93+
8994
- (void)setDeploymentKey:(NSString *)deploymentKey
9095
{
9196
[_configDictionary setValue:deploymentKey forKey:DeploymentKeyConfigKey];

0 commit comments

Comments
 (0)