Skip to content

Commit f29c96f

Browse files
Move PublicKey and ServerUrl properties to string resources (#1740)
1 parent aae3de3 commit f29c96f

File tree

13 files changed

+69
-28
lines changed

13 files changed

+69
-28
lines changed

Examples/CodePushDemoApp-pre0.49/android/app/src/main/java/com/codepushdemoapp/MainApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public boolean getUseDeveloperSupport() {
3030
protected List<ReactPackage> getPackages() {
3131
return Arrays.<ReactPackage>asList(
3232
new MainReactPackage(),
33-
new CodePush(getResources().getString(R.string.reactNativeCodePush_androidDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)
33+
new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)
3434
);
3535
}
3636
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<resources>
2-
<string moduleConfig="true" name="reactNativeCodePush_androidDeploymentKey">deployment-key-here</string>
2+
<string moduleConfig="true" name="CodePushDeploymentKey">deployment-key-here</string>
33
<string name="app_name">CodePushDemoApp</string>
44
</resources>

Examples/CodePushDemoApp/android/app/src/main/java/com/codepushdemoapp/MainApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public boolean getUseDeveloperSupport() {
3030
protected List<ReactPackage> getPackages() {
3131
return Arrays.<ReactPackage>asList(
3232
new MainReactPackage(),
33-
new CodePush(getResources().getString(R.string.reactNativeCodePush_androidDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)
33+
new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)
3434
);
3535
}
3636

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<resources>
2-
<string moduleConfig="true" name="reactNativeCodePush_androidDeploymentKey">deployment-key-here</string>
2+
<string moduleConfig="true" name="CodePushDeploymentKey">deployment-key-here</string>
33
<string name="app_name">CodePushDemoApp</string>
44
</resources>

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
7777

7878
mCurrentInstance = this;
7979

80+
String publicKeyFromStrings = getCustomPropertyFromStringsIfExist("PublicKey");
81+
if (publicKeyFromStrings != null) mPublicKey = publicKeyFromStrings;
82+
83+
String serverUrlFromStrings = getCustomPropertyFromStringsIfExist("ServerUrl");
84+
if (serverUrlFromStrings != null) mServerUrl = serverUrlFromStrings;
85+
8086
clearDebugCacheIfNeeded(null);
8187
initializeUpdateAfterRestart();
8288
}
@@ -120,10 +126,29 @@ private String getPublicKeyByResourceDescriptor(int publicKeyResourceDescriptor)
120126
return publicKey;
121127
}
122128

129+
private String getCustomPropertyFromStringsIfExist(String propertyName) {
130+
String property;
131+
132+
String packageName = mContext.getPackageName();
133+
int resId = mContext.getResources().getIdentifier("CodePush" + propertyName, "string", packageName);
134+
135+
if (resId != 0) {
136+
property = mContext.getString(resId);
137+
138+
if (!property.isEmpty()) {
139+
return property;
140+
} else {
141+
CodePushUtils.log("Specified " + propertyName + " is empty");
142+
}
143+
}
144+
145+
return null;
146+
}
147+
123148
public void clearDebugCacheIfNeeded(ReactInstanceManager instanceManager) {
124149
boolean isLiveReloadEnabled = false;
125150

126-
// Use instanceManager for checking if we use LiveRelaod mode. In this case we should not remove ReactNativeDevBundle.js file
151+
// Use instanceManager for checking if we use LiveReload mode. In this case we should not remove ReactNativeDevBundle.js file
127152
// because we get error with trying to get this after reloading. Issue: https://github.com/Microsoft/react-native-code-push/issues/1272
128153
if (instanceManager != null) {
129154
DevSupportManager devSupportManager = instanceManager.getDevSupportManager();

docs/api-android.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
### Java API Reference (Android)
22

3+
### API for React Native 0.60 version and above
4+
5+
Since `autolinking` uses `react-native.config.js` to link plugins, constructors are specified in that file. But you can override custom variables to manage the CodePush plugin by placing these values in string resources.
6+
7+
* __Public Key__ - used for bundle verification in the Code Signing Feature. Please refer to [Code Signing](setup-android.md#code-signing-setup) section for more details about the Code Signing Feature.
8+
To set the public key, you should add the content of the public key to `strings.xml` with name `CodePushPublicKey`. CodePush automatically gets this property and enables the Code Signing feature. For example:
9+
```xml
10+
<string moduleConfig="true" name="CodePushPublicKey">your-public-key</string>
11+
```
12+
13+
* __Server Url__ - used for specifying CodePush Server Url.
14+
The Default value: "https://codepush.appcenter.ms/" is overridden by adding your path to `strings.xml` with name `CodePushServerUrl`. CodePush automatically gets this property and will use this path to send requests. For example:
15+
```xml
16+
<string moduleConfig="true" name="CodePushServerUrl">https://yourcodepush.server.com</string>
17+
```
18+
19+
### API for React Native lower than 0.60
20+
321
The Java API is made available by importing the `com.microsoft.codepush.react.CodePush` class into your `MainActivity.java` file, and consists of a single public class named `CodePush`.
422

523
#### CodePush
@@ -16,9 +34,9 @@ Constructs the CodePush client runtime and represents the `ReactPackage` instanc
1634

1735
2. The local cache that the React Native runtime maintains in debug mode is deleted whenever a CodePush update is installed. This ensures that when the app is restarted after an update is applied, you will see the expected changes. As soon as [this PR](https://github.com/facebook/react-native/pull/4738) is merged, we won't need to do this anymore.
1836

19-
- __CodePush(String deploymentKey, Context context, boolean isDebugMode, Integer publicKeyResourceDescriptor)__ - Equivalent to the previous constructor, but allows you to specify the public key resource descriptor needed to read public key content. Please refer to [Code Signing](setup-android.md#code-signing) section for more details about Code Signing Feature.
37+
- __CodePush(String deploymentKey, Context context, boolean isDebugMode, Integer publicKeyResourceDescriptor)__ - Equivalent to the previous constructor, but allows you to specify the public key resource descriptor needed to read public key content. Please refer to [Code Signing](setup-android.md#code-signing-setup) section for more details about the Code Signing Feature.
2038

21-
- __CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl)__ Constructor allows you to specify CodePush Server Url. The Default value: `"https://codepush.appcenter.ms/"` is overridden by value specfied in `serverUrl`.
39+
- __CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl)__ Constructor allows you to specify CodePush Server Url. The Default value: `"https://codepush.appcenter.ms/"` is overridden by value specified in `serverUrl`.
2240

2341
##### Builder
2442

@@ -46,7 +64,7 @@ As an alternative to constructors *you can also use `CodePushBuilder`* to setup
4664

4765
* __public CodePushBuilder setServerUrl(String serverUrl)__ - allows you to specify CodePush Server Url. Default value: `"https://codepush.appcenter.ms/"`.
4866

49-
* __public CodePushBuilder setPublicKeyResourceDescriptor(int publicKeyResourceDescriptor)__ - allows you to specify Public Key resource descriptor which will be used for reading Public Key content for `strings.xml` file. Please refer to [Code Signing](#code-signing) section for more detailed information about purpose of this parameter.
67+
* __public CodePushBuilder setPublicKeyResourceDescriptor(int publicKeyResourceDescriptor)__ - allows you to specify Public Key resource descriptor which will be used for reading Public Key content for `strings.xml` file. Please refer to [Code Signing](setup-android.md#code-signing-setup) section for more detailed information about purpose of this parameter.
5068

5169
* __public CodePush build()__ - return configured `CodePush` instance.
5270

docs/multi-deployment-testing-android.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ To set this up, perform the following steps:
2323
debug {
2424
...
2525
// Note: CodePush updates should not be tested in Debug mode as they are overriden by the RN packager. However, because CodePush checks for updates in all modes, we must supply a key.
26-
resValue "string", "reactNativeCodePush_androidDeploymentKey", '""'
26+
resValue "string", "CodePushDeploymentKey", '""'
2727
...
2828
}
2929
3030
releaseStaging {
3131
...
32-
resValue "string", "reactNativeCodePush_androidDeploymentKey", '"<INSERT_STAGING_KEY>"'
32+
resValue "string", "CodePushDeploymentKey", '"<INSERT_STAGING_KEY>"'
3333
3434
// Note: It is a good idea to provide matchingFallbacks for the new buildType you create to prevent build issues
3535
// Add the following line if not already there
@@ -39,7 +39,7 @@ To set this up, perform the following steps:
3939
4040
release {
4141
...
42-
resValue "string", "reactNativeCodePush_androidDeploymentKey", '"<INSERT_PRODUCTION_KEY>"'
42+
resValue "string", "CodePushDeploymentKey", '"<INSERT_PRODUCTION_KEY>"'
4343
...
4444
}
4545
}

docs/setup-android.md

+9-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* [For React Native >= v0.29 (Background React Instances)](#for-react-native--v029-background-react-instances)
1414
* [For React Native v0.19 - v0.28 (Background React Instances)](#for-react-native-v019---v028-background-react-instances)
1515
* [WIX React Native Navigation applications (ver 1.x)](#wix-react-native-navigation-applications)
16-
* [Code Signing setup](#code-signing-setup)
16+
* [Code Signing setup](#code-signing-setup)
1717

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

@@ -52,7 +52,7 @@ In order to integrate CodePush into your Android project, please perform the fol
5252
5353
3. Add the Deployment key to `strings.xml`:
5454
55-
To let the CodePush runtime know which deployment it should query for updates against, open your app's `strings.xml` file and add a new string named `reactNativeCodePush_androidDeploymentKey`, 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 `code-push deployment ls <appName> -k` in the CodePush CLI (the `-k` flag is necessary since keys aren't displayed by default) and copying the value of the `Deployment 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. That "friendly name" is intended only for authenticated management usage from the CLI, and not for public consumption within your app.
55+
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 `code-push deployment ls <appName> -k` in the CodePush CLI (the `-k` flag is necessary since keys aren't displayed by default) and copying the value of the `Deployment 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.
5656
5757
![Deployment list](https://cloud.githubusercontent.com/assets/116461/11601733/13011d5e-9a8a-11e5-9ce2-b100498ffb34.png)
5858
@@ -63,11 +63,11 @@ In order to integrate CodePush into your Android project, please perform the fol
6363
```xml
6464
<resources>
6565
<string name="app_name">AppName</string>
66-
<string moduleConfig="true" name="reactNativeCodePush_androidDeploymentKey">DeploymentKey</string>
66+
<string moduleConfig="true" name="CodePushDeploymentKey">DeploymentKey</string>
6767
</resources>
6868
```
6969
70-
*Note: You can also set your deployment key in JS code using [Code-Push options](./api-js.md#CodePushOptions)*
70+
*Note: If you need to dynamically use a different deployment, you can also override your deployment key in JS code using [Code-Push options](./api-js.md#CodePushOptions)*
7171
7272
### Plugin Installation for React Native lower than 0.60 (Android)
7373
@@ -414,11 +414,11 @@ public class MainApplication extends NavigationApplication {
414414
}
415415
```
416416

417-
#### Code Signing setup
417+
### Code Signing setup
418418

419419
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](https://github.com/Microsoft/code-push/tree/master/cli#code-signing). In order to use Public Key for Code Signing you need to do following steps:
420420

421-
1. Add `CodePushPublicKey` string item to `/path_to_your_app/android/app/src/main/res/values/strings.xml`. It may looks like this:
421+
Add `CodePushPublicKey` string item to `/path_to_your_app/android/app/src/main/res/values/strings.xml`. It may looks like this:
422422

423423
```xml
424424
<resources>
@@ -435,9 +435,9 @@ zwIDAQAB
435435
</resources>
436436
```
437437

438-
2. Configure `CodePush` instance to use this parameter
438+
#### For React Native <= v0.60 you should configure the `CodePush` instance to use this parameter using one of the following approaches
439439

440-
* using constructor
440+
##### Using constructor
441441

442442
```java
443443
new CodePush(
@@ -447,9 +447,7 @@ new CodePush(
447447
R.string.CodePushPublicKey)
448448
```
449449

450-
or
451-
452-
* using builder
450+
##### Using builder
453451

454452
```java
455453
new CodePushBuilder("deployment-key-here",getApplicationContext())

docs/setup-ios.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ Once you've acquired the CodePush plugin, you need to integrate it into the Xcod
5050
5151
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](../README.md#multi-deployment-testing) docs below before actually moving your app's usage of CodePush into production.
5252
53-
*Note: You can also set your deployment key in JS code using [Code-Push options](./api-js.md#CodePushOptions)*
54-
53+
*Note: If you need to dynamically use a different deployment, you can also override your deployment key in JS code using [Code-Push options](./api-js.md#CodePushOptions)*
54+
5555
### Plugin Installation for React Native lower than 0.60 (iOS)
5656
5757
In order to accommodate as many developer preferences as possible, the CodePush plugin supports iOS installation via three mechanisms:

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
},
4343
"rnpm": {
4444
"android": {
45-
"packageInstance": "new CodePush(getResources().getString(R.string.reactNativeCodePush_androidDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
45+
"packageInstance": "new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
4646
},
4747
"ios": {
4848
"sharedLibraries": [

react-native.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
platforms: {
44
android: {
55
packageInstance:
6-
"new CodePush(getResources().getString(R.string.reactNativeCodePush_androidDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
6+
"new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
77
}
88
}
99
}

scripts/postunlink/android/postunlink.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports = () => {
6363
if (!~stringsResourcesContent.indexOf(deploymentKeyName)) {
6464
console.log(`${deploymentKeyName} already removed from the strings.xml`);
6565
} else {
66-
var AndroidDeploymentKey = stringsResourcesContent.match(/(<string moduleConfig="true" name="reactNativeCodePush_androidDeploymentKey">.*<\/string>)/);
66+
var AndroidDeploymentKey = stringsResourcesContent.match(/(<string moduleConfig="true" name="CodePushDeploymentKey">.*<\/string>)/);
6767
if (AndroidDeploymentKey) {
6868
stringsResourcesContent = stringsResourcesContent.replace(`\n\t${AndroidDeploymentKey[0]}`,"");
6969
fs.writeFileSync(stringsResourcesPath, stringsResourcesContent);

scripts/tools/linkToolsAndroid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports.getJSBundleFileOverride = `
1414
exports.reactNativeHostInstantiation = "new ReactNativeHost(this) {";
1515
exports.mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {";
1616
exports.codePushGradleLink = `\napply from: "../../node_modules/react-native-code-push/android/codepush.gradle"`;
17-
exports.deploymentKeyName = "reactNativeCodePush_androidDeploymentKey";
17+
exports.deploymentKeyName = "CodePushDeploymentKey";
1818

1919
exports.getMainApplicationLocation = function () {
2020
return findMainApplication() || glob.sync("**/MainApplication.java", ignoreFolders)[0];

0 commit comments

Comments
 (0)