Skip to content

Commit e1b756b

Browse files
committed
Remove multiple android constructors
1 parent a64dbd0 commit e1b756b

5 files changed

Lines changed: 50 additions & 122 deletions

File tree

README.md

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,7 @@ You may also use the versions that we published based on the versions v5-v9 with
9696

9797
If you are less in a hurry, you can migrate to `@appzung/react-native-code-push` v10+ where we will add new features in the future and actively support the module.
9898

99-
#### Switch to @appzung/react-native-code-push npm package
100-
101-
1. Replace `react-native-code-push` in your package.json with `@appzung/react-native-code-push`: `@appzung/react-native-code-push: "^10.0.0"`
102-
2. Run `npm install` (or `yarn` depending on your project)
103-
104-
#### Change your JS code
105-
106-
1. Replace every `react-native-code-push` imports with `@appzung/react-native-code-push` imports
107-
2. (optional) As the package is now compatible with ESM, if you call CodePush functions like `CodePush.sync()` or import types/interfaces, you may have to replace your imports `import CodePush from` to selective imports `import withCodePush, { sync, DownloadProgress } from` (or `import * as CodePush from`)
108-
3. (optional) If you use a jest global mock, move the mock from `__mocks__/react-native-code-push.ts` to `__mocks__/@appzung/react-native-code-push.ts`
109-
4. (optional) If you use dynamic deployment assignation, rename `deploymentKey` option to `releaseChannelPublicId` (TypeScript should catch that)
110-
111-
#### Change your iOS setup
112-
113-
1. Run `bundle exec pod install`
114-
2. Rename `CodePushDeploymentKey` to `CodePushReleaseChannelPublicId` in your `Info.plist`
115-
3. (optional) If you already use code signing, rename `CodePushPublicKey` to `CodePushSigningPublicKey` in your `Info.plist`
116-
117-
#### Change your Android setup
118-
119-
1. In `android/settings.gradle` remove the lines about CodePush : `include ':react-native-code-push'` and `project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')`
120-
2. In `android/app/build.gradle` change the line about CodePush: `apply from: "../../node_modules/@appzung/react-native-code-push/android/codepush.gradle"`
121-
3. In your Android files (eg. `MainApplication.kt`), rename every `com.microsoft.codepush` prefix imports with `com.appzung.codepush`
122-
4. Rename `CodePushDeploymentKey` to `CodePushReleaseChannelPublicId` in your strings resources (located either at strings.xml or app/build.gradle).
123-
5. (optional) If you already use code signing, rename `CodePushPublicKey` to `CodePushSigningPublicKey` in your strings resources
99+
See the [corresponding steps](./docs/migrating-to-v10.md).
124100

125101
## Compatibility table
126102

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

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import android.content.Context;
44
import android.content.pm.PackageInfo;
55
import android.content.pm.PackageManager;
6-
import android.content.res.Resources;
76

87
import com.facebook.react.ReactInstanceManager;
98
import com.facebook.react.ReactPackage;
10-
import com.facebook.react.bridge.JavaScriptModule;
119
import com.facebook.react.bridge.NativeModule;
1210
import com.facebook.react.bridge.ReactApplicationContext;
1311
import com.facebook.react.devsupport.interfaces.DevSupportManager;
@@ -43,30 +41,35 @@ public class CodePush implements ReactPackage {
4341
private static String mServerUrl = "https://codepush.appzung.com/";
4442

4543
private Context mContext;
46-
private final boolean mIsDebugMode;
44+
private final boolean mIsDebugMode = BuildConfig.DEBUG;
4745

4846
private static String mPublicKey;
4947

5048
private static ReactInstanceHolder mReactInstanceHolder;
5149
private static CodePush mCurrentInstance;
5250

53-
public CodePush(String releaseChannelPublicId, Context context) {
54-
this(releaseChannelPublicId, context, false);
55-
}
56-
5751
public static String getServiceUrl() {
5852
return mServerUrl;
5953
}
6054

61-
public CodePush(String releaseChannelPublicId, Context context, boolean isDebugMode) {
55+
public CodePush(Context context) {
6256
mContext = context.getApplicationContext();
6357

58+
String releaseChannelPublicIdFromStrings = getCustomPropertyFromStringsIfExist("ReleaseChannelPublicId");
59+
if (releaseChannelPublicIdFromStrings == null) {
60+
throw new CodePushUnknownException("Missing release channel public ID in strings resource");
61+
}
62+
63+
mReleaseChannelPublicId = releaseChannelPublicIdFromStrings;
64+
6465
mUpdateManager = new CodePushUpdateManager(context.getFilesDir().getAbsolutePath());
6566
mTelemetryManager = new CodePushTelemetryManager(mContext);
66-
mReleaseChannelPublicId = releaseChannelPublicId;
67-
mIsDebugMode = isDebugMode;
6867
mSettingsManager = new SettingsManager(mContext);
6968

69+
if (mIsDebugMode) {
70+
CodePushUtils.log("Running in DEBUG mode");
71+
}
72+
7073
if (sAppVersion == null) {
7174
try {
7275
PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
@@ -85,54 +88,15 @@ public CodePush(String releaseChannelPublicId, Context context, boolean isDebugM
8588
}
8689

8790
String serverUrlFromStrings = getCustomPropertyFromStringsIfExist("ServerUrl");
88-
if (serverUrlFromStrings != null) mServerUrl = serverUrlFromStrings;
91+
if (serverUrlFromStrings != null) {
92+
CodePushUtils.log("Executing CodePush with a custom server URL.");
93+
mServerUrl = serverUrlFromStrings;
94+
}
8995

9096
clearDebugCacheIfNeeded(null);
9197
initializeUpdateAfterRestart();
9298
}
9399

94-
public CodePush(String releaseChannelPublicId, Context context, boolean isDebugMode, String serverUrl) {
95-
this(releaseChannelPublicId, context, isDebugMode);
96-
mServerUrl = serverUrl;
97-
}
98-
99-
public CodePush(String releaseChannelPublicId, Context context, boolean isDebugMode, int publicKeyResourceDescriptor) {
100-
this(releaseChannelPublicId, context, isDebugMode);
101-
102-
mPublicKey = getPublicKeyByResourceDescriptor(publicKeyResourceDescriptor);
103-
}
104-
105-
public CodePush(String releaseChannelPublicId, Context context, boolean isDebugMode, String serverUrl, Integer publicKeyResourceDescriptor) {
106-
this(releaseChannelPublicId, context, isDebugMode);
107-
108-
if (publicKeyResourceDescriptor != null) {
109-
mPublicKey = getPublicKeyByResourceDescriptor(publicKeyResourceDescriptor);
110-
}
111-
112-
mServerUrl = serverUrl;
113-
}
114-
115-
private String getPublicKeyByResourceDescriptor(int publicKeyResourceDescriptor){
116-
String publicKey;
117-
try {
118-
publicKey = mContext.getString(publicKeyResourceDescriptor);
119-
} catch (Resources.NotFoundException e) {
120-
throw new CodePushInvalidPublicKeyException(
121-
"Unable to get public key, related resource descriptor " +
122-
publicKeyResourceDescriptor +
123-
" can not be found", e
124-
);
125-
}
126-
127-
if (publicKey.isEmpty()) {
128-
throw new CodePushInvalidPublicKeyException("Specified public key is empty");
129-
}
130-
131-
CodePushUtils.log("Executing CodePush with a signing public key.");
132-
133-
return publicKey;
134-
}
135-
136100
private String getCustomPropertyFromStringsIfExist(String propertyName) {
137101
String property;
138102

@@ -433,11 +397,6 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactAppli
433397
return nativeModules;
434398
}
435399

436-
// Deprecated in RN v0.47.
437-
public List<Class<? extends JavaScriptModule>> createJSModules() {
438-
return new ArrayList<>();
439-
}
440-
441400
@Override
442401
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
443402
return new ArrayList<>();

android/app/src/main/java/com/appzung/codepush/react/CodePushBuilder.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

docs/migrating-to-v10.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Migration steps to @appzung/react-native-code-push v10+
2+
3+
If you are less in a hurry, you can migrate to `@appzung/react-native-code-push` v10+ where we will add new features in the future and actively support the module.
4+
5+
### Switch to @appzung/react-native-code-push npm package
6+
7+
1. Replace `react-native-code-push` in your package.json with `@appzung/react-native-code-push`: `@appzung/react-native-code-push: "^10.0.0"`
8+
2. Run `npm install` (or `yarn` depending on your project)
9+
10+
### Change your JS code
11+
12+
1. Replace every `react-native-code-push` imports with `@appzung/react-native-code-push` imports
13+
2. (optional) As the package is now compatible with ESM, if you call CodePush functions like `CodePush.sync()` or import types/interfaces, you may have to replace your imports `import CodePush from` to selective imports `import withCodePush, { sync, DownloadProgress } from` (or `import * as CodePush from`)
14+
3. (optional) If you use a jest global mock, move the mock from `__mocks__/react-native-code-push.ts` to `__mocks__/@appzung/react-native-code-push.ts`
15+
4. (optional) If you use dynamic deployment assignation, rename `deploymentKey` option to `releaseChannelPublicId` (TypeScript should catch that)
16+
17+
### Change your iOS setup
18+
19+
1. Run `bundle exec pod install`
20+
2. Rename `CodePushDeploymentKey` to `CodePushReleaseChannelPublicId` in your `Info.plist`
21+
3. (optional) If you already use code signing, rename `CodePushPublicKey` to `CodePushSigningPublicKey` in your `Info.plist`
22+
23+
### Change your Android setup
24+
25+
1. In `android/settings.gradle` remove the lines about CodePush : `include ':react-native-code-push'` and `project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')`
26+
2. In `android/app/build.gradle` change the line about CodePush: `apply from: "../../node_modules/@appzung/react-native-code-push/android/codepush.gradle"`
27+
3. In your Android files (eg. `MainApplication.kt`), rename every `com.microsoft.codepush` prefix imports with `com.appzung.codepush`
28+
4. Rename `CodePushDeploymentKey` to `CodePushReleaseChannelPublicId` in your strings resources (located either at strings.xml or app/build.gradle).
29+
5. (optional) If you already use code signing, rename `CodePushPublicKey` to `CodePushSigningPublicKey` in your strings resources
30+
6. (optional) If you used `CodePushBuilder` to instantiate CodePush manually, it has been removed, consider using the standard way or contact us if you really need this
31+
7. (optional) If you used a custom CodePush `packageInstance` in your `react-native.config.js` file, it is no longer possible, consider using the standard way or contact us if you really need this

react-native.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ module.exports = {
22
dependency: {
33
platforms: {
44
android: {
5-
packageInstance:
6-
'new CodePush(getResources().getString(R.string.CodePushReleaseChannelPublicId), getApplicationContext(), BuildConfig.DEBUG)',
5+
packageInstance: 'new CodePush(getApplicationContext())',
76
sourceDir: './android/app',
87
},
98
},

0 commit comments

Comments
 (0)