Skip to content

Commit 628b09b

Browse files
committed
feat: refactor CodePush module for new architecture support and update Gradle configurations
1 parent 7dfe1e5 commit 628b09b

File tree

15 files changed

+439
-303
lines changed

15 files changed

+439
-303
lines changed

CodePush.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Pod::Spec.new do |s|
1010
s.license = package['license']
1111
s.homepage = package['homepage']
1212
s.source = { :git => 'https://github.com/srcpush/react-native-code-push.git', :tag => "v#{s.version}"}
13-
s.ios.deployment_target = '15.5'
14-
s.tvos.deployment_target = '15.5'
13+
s.ios.deployment_target = '11.0'
14+
s.tvos.deployment_target = '11.0'
1515
s.preserve_paths = '*.js'
1616
s.library = 'z'
1717
s.source_files = 'ios/CodePush/*.{h,m}'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
connection.project.dir=..
2+
eclipse.preferences.version=1

android/app/build.gradle

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,99 @@
1+
import groovy.json.JsonSlurper
2+
13
apply plugin: "com.android.library"
24

35
def safeExtGet(prop, fallback) {
46
return rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
57
}
68

7-
def isNewArchitectureEnabled() {
8-
// To opt-in for the New Architecture, you can either:
9-
// - Set `newArchEnabled` to true inside the `gradle.properties` file
10-
// - Invoke gradle with `-newArchEnabled=true`
11-
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
12-
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true" || System.env.ORG_GRADLE_PROJECT_newArchEnabled == "true"
9+
def DEFAULT_COMPILE_SDK_VERSION = 26
10+
def DEFAULT_BUILD_TOOLS_VERSION = "26.0.3"
11+
def DEFAULT_TARGET_SDK_VERSION = 26
12+
def DEFAULT_MIN_SDK_VERSION = 16
13+
14+
def findReactNativePackageJson() {
15+
File currentDir = projectDir
16+
17+
while (currentDir != null) {
18+
File packageJson = new File(currentDir, "node_modules/react-native/package.json")
19+
if (packageJson.exists()) {
20+
return packageJson
21+
}
22+
23+
currentDir = currentDir.parentFile
24+
}
25+
26+
return null
27+
}
28+
29+
def getReactNativeMinorVersion() {
30+
File packageJson = findReactNativePackageJson()
31+
if (packageJson == null) {
32+
return null
33+
}
34+
35+
def version = new JsonSlurper().parseText(packageJson.text).version
36+
def stableVersion = version.tokenize("-")[0]
37+
def versionParts = stableVersion.tokenize(".")
38+
if (versionParts.size() < 2) {
39+
return null
40+
}
41+
42+
return versionParts[1].toInteger()
1343
}
1444

15-
def IS_NEW_ARCHITECTURE_ENABLED = isNewArchitectureEnabled()
45+
def getReactNativeVersion() {
46+
File packageJson = findReactNativePackageJson()
47+
if (packageJson == null) {
48+
return null
49+
}
1650

17-
if (IS_NEW_ARCHITECTURE_ENABLED) {
18-
apply plugin: "com.facebook.react"
51+
def version = new JsonSlurper().parseText(packageJson.text).version
52+
return version.tokenize("-")[0]
1953
}
2054

21-
def DEFAULT_COMPILE_SDK_VERSION = 26
22-
def DEFAULT_BUILD_TOOLS_VERSION = "26.0.3"
23-
def DEFAULT_TARGET_SDK_VERSION = 26
24-
def DEFAULT_MIN_SDK_VERSION = 16
55+
def reactNativeMinorVersion = getReactNativeMinorVersion()
56+
def reactNativeVersion = getReactNativeVersion()
57+
def reactNativeDependency = reactNativeMinorVersion != null && reactNativeMinorVersion < 71
58+
? "com.facebook.react:react-native:${reactNativeVersion ?: '+'}"
59+
: "com.facebook.react:react-android:${reactNativeVersion ?: '+'}"
2560

2661
android {
27-
namespace "com.microsoft.codepush.react"
62+
if (project.android.hasProperty("namespace")) {
63+
namespace "com.microsoft.codepush.react"
64+
}
2865

2966
compileSdkVersion safeExtGet("compileSdkVersion", DEFAULT_COMPILE_SDK_VERSION)
30-
buildToolsVersion safeExtGet("buildToolsVersion", DEFAULT_BUILD_TOOLS_VERSION)
3167

32-
buildFeatures {
33-
buildConfig true
68+
if (project.android.hasProperty("buildToolsVersion")) {
69+
buildToolsVersion safeExtGet("buildToolsVersion", DEFAULT_BUILD_TOOLS_VERSION)
3470
}
3571

3672
defaultConfig {
3773
minSdkVersion safeExtGet("minSdkVersion", DEFAULT_MIN_SDK_VERSION)
3874
targetSdkVersion safeExtGet("targetSdkVersion", DEFAULT_TARGET_SDK_VERSION)
3975
versionCode 1
4076
versionName "1.0"
41-
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", IS_NEW_ARCHITECTURE_ENABLED.toString()
42-
consumerProguardFiles 'proguard-rules.pro'
77+
consumerProguardFiles "proguard-rules.pro"
4378
}
4479

45-
sourceSets {
46-
main {
47-
if (IS_NEW_ARCHITECTURE_ENABLED) {
48-
java.srcDirs += ["src/newarch/java"]
49-
java.srcDirs += ["$buildDir/generated/source/codegen/java", "$buildDir/generated/source/codegen/android/app/build/generated/source/codegen/java"]
50-
} else {
51-
java.srcDirs += ["src/oldarch/java"]
52-
}
53-
}
80+
compileOptions {
81+
sourceCompatibility JavaVersion.VERSION_1_8
82+
targetCompatibility JavaVersion.VERSION_1_8
5483
}
5584

5685
lintOptions {
5786
abortOnError false
5887
}
5988
}
6089

90+
repositories {
91+
google()
92+
mavenCentral()
93+
mavenLocal()
94+
}
95+
6196
dependencies {
62-
implementation("com.facebook.react:react-android")
63-
implementation 'com.nimbusds:nimbus-jose-jwt:9.37.3'
97+
implementation(reactNativeDependency)
98+
implementation "com.nimbusds:nimbus-jose-jwt:9.37.3"
6499
}

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

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@
55
import android.content.pm.PackageManager;
66
import android.content.res.Resources;
77

8-
import com.facebook.react.ReactHost;
98
import com.facebook.react.ReactInstanceManager;
10-
import com.facebook.react.BaseReactPackage;
9+
import com.facebook.react.ReactPackage;
1110
import com.facebook.react.bridge.JavaScriptModule;
1211
import com.facebook.react.bridge.NativeModule;
1312
import com.facebook.react.bridge.ReactApplicationContext;
14-
import com.facebook.react.module.model.ReactModuleInfo;
15-
import com.facebook.react.module.model.ReactModuleInfoProvider;
1613
import com.facebook.react.uimanager.ViewManager;
1714

1815
import org.json.JSONException;
1916
import org.json.JSONObject;
2017

2118
import java.io.File;
2219
import java.util.ArrayList;
23-
import java.util.HashMap;
2420
import java.util.List;
25-
import java.util.Map;
2621

27-
public class CodePush extends BaseReactPackage {
22+
public class CodePush implements ReactPackage {
2823
private static final Object LOCK = new Object();
2924
private static volatile CodePush mCurrentInstance;
3025
public static CodePush getInstance(String deploymentKey, Context context, boolean isDebugMode) {
@@ -64,6 +59,7 @@ public static CodePush getInstance(String deploymentKey, Context context, boolea
6459
private static ReactInstanceHolder mReactInstanceHolder;
6560

6661
private static ReactHostHolder mReactHostHolder;
62+
private static Object mReactHost;
6763

6864
public CodePush(String deploymentKey, Context context) {
6965
this(deploymentKey, context, false);
@@ -410,14 +406,22 @@ public static void setReactHost(ReactHostHolder reactHostHolder) {
410406
mReactHostHolder = reactHostHolder;
411407
}
412408

409+
public static void setReactHost(Object reactHost) {
410+
mReactHost = reactHost;
411+
}
412+
413413
static ReactInstanceManager getReactInstanceManager() {
414414
if (mReactInstanceHolder == null) {
415415
return null;
416416
}
417417
return mReactInstanceHolder.getReactInstanceManager();
418418
}
419419

420-
static ReactHost getReactHost() {
420+
static Object getReactHost() {
421+
if (mReactHost != null) {
422+
return mReactHost;
423+
}
424+
421425
if (mReactHostHolder == null) {
422426
return null;
423427
}
@@ -426,14 +430,11 @@ static ReactHost getReactHost() {
426430
}
427431

428432
@Override
429-
public NativeModule getModule(String name, ReactApplicationContext reactApplicationContext) {
430-
if (CodePushNativeModule.NAME.equals(name)) {
431-
return new CodePushNativeModule(reactApplicationContext, this, mUpdateManager, mTelemetryManager, mSettingsManager);
432-
}
433-
if (CodePushDialog.NAME.equals(name)) {
434-
return new CodePushDialog(reactApplicationContext);
435-
}
436-
return null;
433+
public List<NativeModule> createNativeModules(ReactApplicationContext reactApplicationContext) {
434+
List<NativeModule> modules = new ArrayList<>();
435+
modules.add(new CodePushNativeModule(reactApplicationContext, this, mUpdateManager, mTelemetryManager, mSettingsManager));
436+
modules.add(new CodePushDialog(reactApplicationContext));
437+
return modules;
437438
}
438439

439440
// Deprecated in RN v0.47.
@@ -445,42 +446,4 @@ public List<Class<? extends JavaScriptModule>> createJSModules() {
445446
public List<ViewManager> createViewManagers(ReactApplicationContext reactApplicationContext) {
446447
return new ArrayList<>();
447448
}
448-
449-
@Override
450-
public ReactModuleInfoProvider getReactModuleInfoProvider() {
451-
return new ReactModuleInfoProvider() {
452-
@Override
453-
public Map<String, ReactModuleInfo> getReactModuleInfos() {
454-
boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
455-
456-
Map<String, ReactModuleInfo> map = new HashMap<>();
457-
map.put(
458-
CodePushNativeModule.NAME,
459-
new ReactModuleInfo(
460-
CodePushNativeModule.NAME,
461-
CodePushNativeModule.NAME,
462-
false,
463-
false,
464-
true,
465-
false,
466-
isTurboModule
467-
)
468-
);
469-
map.put(
470-
CodePushDialog.NAME,
471-
new ReactModuleInfo(
472-
CodePushDialog.NAME,
473-
CodePushDialog.NAME,
474-
false,
475-
false,
476-
false,
477-
false,
478-
isTurboModule
479-
)
480-
);
481-
482-
return map;
483-
}
484-
};
485-
}
486449
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.microsoft.codepush.react;
2+
3+
import com.facebook.react.bridge.BaseJavaModule;
4+
import com.facebook.react.bridge.Callback;
5+
import com.facebook.react.bridge.ReactApplicationContext;
6+
import com.facebook.react.bridge.ReactMethod;
7+
8+
public class CodePushDialog extends BaseJavaModule {
9+
public static final String NAME = "CodePushDialog";
10+
11+
private final CodePushDialogImpl impl;
12+
13+
public CodePushDialog(ReactApplicationContext reactContext) {
14+
super(reactContext);
15+
impl = new CodePushDialogImpl(reactContext);
16+
}
17+
18+
@ReactMethod
19+
public void showDialog(
20+
final String title,
21+
final String message,
22+
final String button1Text,
23+
final String button2Text,
24+
final Callback successCallback,
25+
Callback errorCallback
26+
) {
27+
try {
28+
impl.showDialog(title, message, button1Text, button2Text, successCallback, errorCallback);
29+
} catch (Throwable e) {
30+
if (errorCallback != null) {
31+
errorCallback.invoke(e.getMessage());
32+
}
33+
}
34+
}
35+
36+
@ReactMethod
37+
public void addListener(String eventName) {
38+
// no-op
39+
}
40+
41+
@ReactMethod
42+
public void removeListeners(double count) {
43+
// no-op
44+
}
45+
46+
@Override
47+
public String getName() {
48+
return NAME;
49+
}
50+
}

0 commit comments

Comments
 (0)