Skip to content

Commit 394bbb8

Browse files
committed
fix: Ensure compatibility with new React Native version
1 parent 1c5ef8d commit 394bbb8

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

android/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import com.android.Version
22
import in.juspay.payments.core.ClientConfig
3+
import groovy.json.JsonSlurper
34

45
buildscript {
56
repositories {
@@ -14,6 +15,14 @@ buildscript {
1415
}
1516
}
1617

18+
def getRNVersion() {
19+
def file = new File(rootDir, "../package.json")
20+
def json = new JsonSlurper().parse(file)
21+
return json.dependencies["react-native"] ?: json.devDependencies["react-native"]
22+
}
23+
24+
def rnVersion = getRNVersion()
25+
1726
def isNewArchitectureEnabled() {
1827
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
1928
}
@@ -49,6 +58,7 @@ android {
4958
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
5059
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
5160
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
61+
buildConfigField "String", "REACT_NATIVE_VERSION", "\"${rnVersion}\""
5262
consumerProguardFiles "consumer-rules.pro"
5363
}
5464
buildTypes {

android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import androidx.fragment.app.FragmentActivity;
2323

2424
import com.facebook.react.ReactApplication;
25+
import com.facebook.react.ReactHost;
2526
import com.facebook.react.ReactInstanceManager;
2627
import com.facebook.react.ReactRootView;
2728
import com.facebook.react.bridge.ActivityEventListener;
@@ -89,6 +90,8 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A
8990

9091
private boolean wasProcessWithActivity = false;
9192

93+
private boolean useNewApprochForMerchantView = false;
94+
9295
private Set<String> registeredComponents = new HashSet<>();
9396

9497
@NonNull
@@ -97,6 +100,7 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A
97100
HyperSdkReactModule(ReactApplicationContext reactContext) {
98101
super(reactContext);
99102
this.context = reactContext;
103+
useNewApprochForMerchantView = setUseNewApprochForMerchantView();
100104
reactContext.addActivityEventListener(this);
101105
}
102106

@@ -318,20 +322,47 @@ public View createReactSurfaceView(String viewName) {
318322
}
319323

320324
private View createMerchantView(String viewName) {
321-
if (newArchEnabled) {
322-
return createReactSurfaceView(viewName);
323-
} else {
325+
326+
if (!useNewApprochForMerchantView) {
327+
if (newArchEnabled) {
328+
return createReactSurfaceView(viewName);
329+
}
324330
ReactRootView reactRootView = new ReactRootView(activity);
325331
reactRootView.startReactApplication(reactInstanceManager, viewName);
326332
return reactRootView;
327333
}
334+
try {
335+
ReactHost reactHost = ((ReactApplication) activity.getApplication()).getReactHost();
336+
if (reactHost != null) {
337+
Object surface = reactHost.createSurface(
338+
activity,
339+
viewName,
340+
null
341+
);
342+
surface.getClass().getMethod("start").invoke(surface);
343+
344+
return (View) surface.getClass().getMethod("getView").invoke(surface);
345+
}
346+
return null;
347+
} catch (Exception e) {
348+
SdkTracker.trackAndLogBootException(
349+
NAME,
350+
LogConstants.CATEGORY_LIFECYCLE,
351+
LogConstants.SUBCATEGORY_HYPER_SDK,
352+
LogConstants.SDK_TRACKER_LABEL,
353+
"Exception in createMerchantView",
354+
e
355+
);
356+
return null;
357+
}
328358
}
329359

360+
330361
@Nullable
331362
@Override
332363
public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewType) {
333364
Activity activity = (Activity) getCurrentActivity();
334-
if (reactInstanceManager == null || activity == null) {
365+
if (!useNewApprochForMerchantView && reactInstanceManager == null || activity == null) {
335366
return super.getMerchantView(viewGroup, merchantViewType);
336367
} else {
337368
View merchantView = null;
@@ -370,6 +401,17 @@ public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewTy
370401
}
371402
}
372403

404+
private Boolean setUseNewApprochForMerchantView() {
405+
String version = BuildConfig.REACT_NATIVE_VERSION;
406+
String[] parts = version.split("\\.");
407+
if (parts.length > 1) {
408+
int major = Integer.parseInt(parts[0]);
409+
int minor = Integer.parseInt(parts[1]);
410+
return major > 0 || (major == 0 && minor >= 82);
411+
}
412+
return false;
413+
}
414+
373415
private void createHyperService(@Nullable String tenantId, @Nullable String clientId) {
374416
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
375417
if (activity == null) {
@@ -384,7 +426,12 @@ private void createHyperService(@Nullable String tenantId, @Nullable String clie
384426
Application app = activity.getApplication();
385427
if (app instanceof ReactApplication) {
386428
this.app = ((ReactApplication) app);
387-
reactInstanceManager = ((ReactApplication) app).getReactNativeHost().getReactInstanceManager();
429+
if (!useNewApprochForMerchantView) {
430+
reactInstanceManager = ((ReactApplication) app).getReactNativeHost().getReactInstanceManager();
431+
}
432+
else {
433+
reactInstanceManager = null;
434+
}
388435
}
389436
if (tenantId != null && clientId != null) {
390437
hyperServices = new HyperServices(activity, tenantId, clientId);

0 commit comments

Comments
 (0)