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

Commit 68fe5e0

Browse files
committed
simply android setup
1 parent e717eb0 commit 68fe5e0

File tree

4 files changed

+58
-81
lines changed

4 files changed

+58
-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: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
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.
320313
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() {
339-
return Arrays.<ReactPackage>asList(
340-
...
341-
new CodePushReactPackage() // 2. Remove this line
342-
...
314+
new MainReactPackage(),
315+
new CodePush("0dsIDongIcoH0mqAmoR0CYb5FhBZNy1w4Bf-l", 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: 36 additions & 35 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,7 @@ public CodePush(String deploymentKey, Activity mainActivity, boolean isDebugMode
100101
}
101102

102103
initializeUpdateAfterRestart();
104+
currentInstance = this;
103105
}
104106

105107
private void clearReactDevBundleCache() {
@@ -129,21 +131,29 @@ private long getBinaryResourcesModifiedTime() {
129131
}
130132
}
131133

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

137147
try {
138-
String packageFilePath = codePushPackage.getCurrentPackageBundlePath();
148+
String packageFilePath = this.codePushPackage.getCurrentPackageBundlePath();
139149
if (packageFilePath == null) {
140150
// There has not been any downloaded updates.
141151
CodePushUtils.logBundleUrl(binaryJsBundleUrl);
142152
isRunningBinaryVersion = true;
143153
return binaryJsBundleUrl;
144154
}
145155

146-
ReadableMap packageMetadata = codePushPackage.getCurrentPackage();
156+
ReadableMap packageMetadata = this.codePushPackage.getCurrentPackage();
147157
Long binaryModifiedDateDuringPackageInstall = null;
148158
String binaryModifiedDateDuringPackageInstallString = CodePushUtils.tryGetString(packageMetadata, BINARY_MODIFIED_TIME_KEY);
149159
if (binaryModifiedDateDuringPackageInstallString != null) {
@@ -159,7 +169,7 @@ public String getBundleUrl(String assetsBundleFileName) {
159169
return packageFilePath;
160170
} else {
161171
// The binary version is newer.
162-
didUpdate = false;
172+
this.didUpdate = false;
163173
if (!this.isDebugMode || !this.appVersion.equals(packageAppVersion)) {
164174
this.clearUpdates();
165175
}
@@ -206,13 +216,6 @@ private JSONObject getPendingUpdate() {
206216
return null;
207217
}
208218
}
209-
210-
public ReactPackage getReactPackage() {
211-
if (codePushReactPackage == null) {
212-
codePushReactPackage = new CodePushReactPackage();
213-
}
214-
return codePushReactPackage;
215-
}
216219

217220
private void initializeUpdateAfterRestart() {
218221
JSONObject pendingUpdate = getPendingUpdate();
@@ -349,6 +352,7 @@ private void loadBundle() {
349352
Intent intent = mainActivity.getIntent();
350353
mainActivity.finish();
351354
mainActivity.startActivity(intent);
355+
currentInstance = null;
352356
}
353357

354358
@ReactMethod
@@ -439,7 +443,6 @@ protected Void doInBackground(Void... params) {
439443

440444
@ReactMethod
441445
public void getNewStatusReport(final Promise promise) {
442-
443446
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
444447
@Override
445448
protected Void doInBackground(Void... params) {
@@ -605,27 +608,25 @@ public String getName() {
605608
}
606609
}
607610

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);
611+
@Override
612+
public List<NativeModule> createNativeModules(ReactApplicationContext reactApplicationContext) {
613+
List<NativeModule> nativeModules = new ArrayList<>();
614+
this.codePushNativeModule = new CodePushNativeModule(reactApplicationContext);
615+
CodePushDialog dialogModule = new CodePushDialog(reactApplicationContext, mainActivity);
614616

615-
nativeModules.add(CodePush.this.codePushNativeModule);
616-
nativeModules.add(dialogModule);
617+
nativeModules.add(this.codePushNativeModule);
618+
nativeModules.add(dialogModule);
617619

618-
return nativeModules;
619-
}
620+
return nativeModules;
621+
}
620622

621-
@Override
622-
public List<Class<? extends JavaScriptModule>> createJSModules() {
623-
return new ArrayList<>();
624-
}
623+
@Override
624+
public List<Class<? extends JavaScriptModule>> createJSModules() {
625+
return new ArrayList<>();
626+
}
625627

626-
@Override
627-
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
628-
return new ArrayList<>();
629-
}
628+
@Override
629+
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
630+
return new ArrayList<>();
630631
}
631632
}
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 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)