Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 39132b0

Browse files
committed
Merge pull request #257 from Microsoft/simplify-android-setup
Simplify android setup
2 parents 587f9b2 + 511dbe9 commit 39132b0

File tree

4 files changed

+62
-81
lines changed

4 files changed

+62
-81
lines changed

Examples/CodePushDemoApp/android/app/src/main/java/com/microsoft/codepushdemoapp/MainActivity.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.microsoft.codepushdemoapp;
22

3-
import android.os.Bundle;
4-
53
import com.facebook.react.ReactActivity;
64
import com.facebook.react.ReactPackage;
75
import com.facebook.react.shell.MainReactPackage;
@@ -12,17 +10,9 @@
1210

1311
public class MainActivity extends ReactActivity {
1412

15-
private CodePush codePush;
16-
17-
@Override
18-
protected void onCreate(Bundle savedInstanceState) {
19-
codePush = new CodePush("deployment-key-here", this, BuildConfig.DEBUG);
20-
super.onCreate(savedInstanceState);
21-
}
22-
2313
@Override
2414
protected String getJSBundleFile() {
25-
return this.codePush.getBundleUrl("index.android.bundle");
15+
return CodePush.getBundleUrl("index.android.bundle");
2616
}
2717

2818
/**
@@ -51,7 +41,7 @@ protected boolean getUseDeveloperSupport() {
5141
protected List<ReactPackage> getPackages() {
5242
return Arrays.<ReactPackage>asList(
5343
new MainReactPackage(),
54-
this.codePush.getReactPackage()
44+
new CodePush("deployment-key-here", this, BuildConfig.DEBUG)
5545
);
5646
}
5747
}

README.md

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ After installing the plugin and syncing your Android Studio project with Gradle,
269269
}
270270
```
271271

272-
2. Ensure that the `android.defaultConfig.versionName` property in your `android/app/build.gradle` file is set to a semver compliant value. Note that if the value provided is missing a patch version, the CodePush server will assume it is `0`, i.e. `1.0` will be treated as `1.0.0`.
272+
2. Ensure that the `android.defaultConfig.versionName` property in your `android/app/build.gradle` file is set to a semver-compliant value. Note that if the value provided is missing a patch version, the CodePush server will assume it is `0`, i.e. `1.0` will be treated as `1.0.0`.
273273

274274
```gradle
275275
android {
@@ -297,12 +297,7 @@ After installing the plugin and syncing your Android Studio project with Gradle,
297297
import com.microsoft.codepush.react.CodePush;
298298
299299
public class MainActivity extends ReactActivity {
300-
// 2. Define a private field to hold the CodePush runtime instance
301-
private CodePush _codePush;
302-
303-
...
304-
305-
// 3. Override the getJSBundleFile method in order to let
300+
// 2. Override the getJSBundleFile method in order to let
306301
// the CodePush runtime determine where to get the JS
307302
// bundle location from on each app start
308303
@Override
@@ -312,41 +307,20 @@ After installing the plugin and syncing your Android Studio project with Gradle,
312307
313308
@Override
314309
protected List<ReactPackage> getPackages() {
315-
// 4. Instantiate an instance of the CodePush runtime, using the right deployment key. If you don't
316-
// already have it, you can run "code-push deployment ls <appName> -k" to retrieve your key.
317-
this._codePush = new CodePush("0dsIDongIcoH0mqAmoR0CYb5FhBZNy1w4Bf-l", this, BuildConfig.DEBUG);
318-
319-
// 5. Add the CodePush package to the list of existing packages
320-
return Arrays.<ReactPackage>asList(
321-
new MainReactPackage(), this._codePush.getReactPackage());
322-
}
323-
324-
...
325-
}
326-
```
327-
328-
2. If you used RNPM to install/link the CodePush plugin, there are two additional changes you'll need to make due to the fact that RNPM makes some assumptions about 3rd party modules that we don't currently support. If you're not using RNPM then simply skip to step #3:
329-
330-
```java
331-
...
332-
// 1. Remove the following import statement
333-
import com.microsoft.codepush.react.CodePushReactPackage;
334-
...
335-
public class MainActivity extends ReactActivity {
336-
...
337-
@Override
338-
protected List<ReactPackage> getPackages() {
310+
// 3. Instantiate an instance of the CodePush runtime and add it to the list of
311+
// existing packages, specifying the right deployment key. If you don't already
312+
// have it, you can run "code-push deployment ls <appName> -k" to retrieve your key.
339313
return Arrays.<ReactPackage>asList(
340-
...
341-
new CodePushReactPackage() // 2. Remove this line
342-
...
314+
new MainReactPackage(),
315+
new CodePush("deployment-key-here", this, BuildConfig.DEBUG)
343316
);
344317
}
318+
345319
...
346320
}
347321
```
348322

349-
3. Ensure that the `android.defaultConfig.versionName` property in your `android/app/build.gradle` file is set to a semver compliant value. Note that if the value provided is missing a patch version, the CodePush server will assume it is `0`, i.e. `1.0` will be treated as `1.0.0`.
323+
2. Ensure that the `android.defaultConfig.versionName` property in your `android/app/build.gradle` file is set to a semver-compliant value. Note that if the value provided is missing a patch version, the CodePush server will assume it is `0`, i.e. `1.0` will be treated as `1.0.0`.
350324

351325
```gradle
352326
android {

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

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import java.util.zip.ZipEntry;
4040
import java.util.zip.ZipFile;
4141

42-
public class CodePush {
42+
public class CodePush implements ReactPackage {
4343
private static boolean needToReportRollback = false;
4444
private static boolean isRunningBinaryVersion = false;
4545
private static boolean testConfigurationFlag = false;
@@ -48,8 +48,8 @@ public class CodePush {
4848

4949
private String assetsBundleFileName;
5050

51-
private final String ASSETS_BUNDLE_PREFIX = "assets://";
52-
private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime";
51+
private static final String ASSETS_BUNDLE_PREFIX = "assets://";
52+
private static final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime";
5353
private final String CODE_PUSH_PREFERENCES = "CodePush";
5454
private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress";
5555
private final String FAILED_UPDATES_KEY = "CODE_PUSH_FAILED_UPDATES";
@@ -65,7 +65,6 @@ public class CodePush {
6565
// Helper classes.
6666
private CodePushNativeModule codePushNativeModule;
6767
private CodePushPackage codePushPackage;
68-
private CodePushReactPackage codePushReactPackage;
6968
private CodePushTelemetryManager codePushTelemetryManager;
7069

7170
// Config properties.
@@ -78,6 +77,8 @@ public class CodePush {
7877
private Context applicationContext;
7978
private final boolean isDebugMode;
8079

80+
private static CodePush currentInstance;
81+
8182
public CodePush(String deploymentKey, Activity mainActivity) {
8283
this(deploymentKey, mainActivity, false);
8384
}
@@ -100,6 +101,11 @@ public CodePush(String deploymentKey, Activity mainActivity, boolean isDebugMode
100101
}
101102

102103
initializeUpdateAfterRestart();
104+
if (currentInstance != null) {
105+
CodePushUtils.log("More than one CodePush instance has been initialized. Please use the instance method codePush.getBundleUrlInternal() to get the correct bundleURL for a particular instance.");
106+
}
107+
108+
currentInstance = this;
103109
}
104110

105111
private void clearReactDevBundleCache() {
@@ -129,10 +135,18 @@ private long getBinaryResourcesModifiedTime() {
129135
}
130136
}
131137

132-
public String getBundleUrl(String assetsBundleFileName) {
138+
public static String getBundleUrl(String assetsBundleFileName) {
139+
if (currentInstance == null) {
140+
throw new CodePushNotInitializedException("A CodePush instance has not been created yet. Have you added it to your app's list of ReactPackages?");
141+
}
142+
143+
return currentInstance.getBundleUrlInternal(assetsBundleFileName);
144+
}
145+
146+
public String getBundleUrlInternal(String assetsBundleFileName) {
133147
this.assetsBundleFileName = assetsBundleFileName;
134148
String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName;
135-
long binaryResourcesModifiedTime = getBinaryResourcesModifiedTime();
149+
long binaryResourcesModifiedTime = this.getBinaryResourcesModifiedTime();
136150

137151
try {
138152
String packageFilePath = codePushPackage.getCurrentPackageBundlePath(this.assetsBundleFileName);
@@ -143,7 +157,7 @@ public String getBundleUrl(String assetsBundleFileName) {
143157
return binaryJsBundleUrl;
144158
}
145159

146-
ReadableMap packageMetadata = codePushPackage.getCurrentPackage();
160+
ReadableMap packageMetadata = this.codePushPackage.getCurrentPackage();
147161
Long binaryModifiedDateDuringPackageInstall = null;
148162
String binaryModifiedDateDuringPackageInstallString = CodePushUtils.tryGetString(packageMetadata, BINARY_MODIFIED_TIME_KEY);
149163
if (binaryModifiedDateDuringPackageInstallString != null) {
@@ -159,7 +173,7 @@ public String getBundleUrl(String assetsBundleFileName) {
159173
return packageFilePath;
160174
} else {
161175
// The binary version is newer.
162-
didUpdate = false;
176+
this.didUpdate = false;
163177
if (!this.isDebugMode || !this.appVersion.equals(packageAppVersion)) {
164178
this.clearUpdates();
165179
}
@@ -206,13 +220,6 @@ private JSONObject getPendingUpdate() {
206220
return null;
207221
}
208222
}
209-
210-
public ReactPackage getReactPackage() {
211-
if (codePushReactPackage == null) {
212-
codePushReactPackage = new CodePushReactPackage();
213-
}
214-
return codePushReactPackage;
215-
}
216223

217224
private void initializeUpdateAfterRestart() {
218225
JSONObject pendingUpdate = getPendingUpdate();
@@ -349,6 +356,7 @@ private void loadBundle() {
349356
Intent intent = mainActivity.getIntent();
350357
mainActivity.finish();
351358
mainActivity.startActivity(intent);
359+
currentInstance = null;
352360
}
353361

354362
@ReactMethod
@@ -439,7 +447,6 @@ protected Void doInBackground(Void... params) {
439447

440448
@ReactMethod
441449
public void getNewStatusReport(final Promise promise) {
442-
443450
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
444451
@Override
445452
protected Void doInBackground(Void... params) {
@@ -605,27 +612,25 @@ public String getName() {
605612
}
606613
}
607614

608-
private class CodePushReactPackage implements ReactPackage {
609-
@Override
610-
public List<NativeModule> createNativeModules(ReactApplicationContext reactApplicationContext) {
611-
List<NativeModule> nativeModules = new ArrayList<>();
612-
CodePush.this.codePushNativeModule = new CodePushNativeModule(reactApplicationContext);
613-
CodePushDialog dialogModule = new CodePushDialog(reactApplicationContext, mainActivity);
615+
@Override
616+
public List<NativeModule> createNativeModules(ReactApplicationContext reactApplicationContext) {
617+
List<NativeModule> nativeModules = new ArrayList<>();
618+
this.codePushNativeModule = new CodePushNativeModule(reactApplicationContext);
619+
CodePushDialog dialogModule = new CodePushDialog(reactApplicationContext, mainActivity);
614620

615-
nativeModules.add(CodePush.this.codePushNativeModule);
616-
nativeModules.add(dialogModule);
621+
nativeModules.add(this.codePushNativeModule);
622+
nativeModules.add(dialogModule);
617623

618-
return nativeModules;
619-
}
624+
return nativeModules;
625+
}
620626

621-
@Override
622-
public List<Class<? extends JavaScriptModule>> createJSModules() {
623-
return new ArrayList<>();
624-
}
627+
@Override
628+
public List<Class<? extends JavaScriptModule>> createJSModules() {
629+
return new ArrayList<>();
630+
}
625631

626-
@Override
627-
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
628-
return new ArrayList<>();
629-
}
632+
@Override
633+
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
634+
return new ArrayList<>();
630635
}
631636
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.microsoft.codepush.react;
2+
3+
public final class CodePushNotInitializedException extends RuntimeException {
4+
5+
public CodePushNotInitializedException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
public CodePushNotInitializedException(String message) {
10+
super(message);
11+
}
12+
}

0 commit comments

Comments
 (0)