Skip to content

Commit bdeae70

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

File tree

3 files changed

+116
-9
lines changed

3 files changed

+116
-9
lines changed

android/build.gradle

Lines changed: 49 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,53 @@ buildscript {
1415
}
1516
}
1617

18+
def getRNVersion() {
19+
def rootPackageFile = new File(rootDir, "../package.json")
20+
if (rootPackageFile.exists()) {
21+
try {
22+
def json = new JsonSlurper().parse(rootPackageFile)
23+
def version = json.dependencies["react-native"] ?: json.devDependencies["react-native"]
24+
if (version) {
25+
return version
26+
}
27+
} catch (Exception e) {
28+
// Ignore parsing errors and continue
29+
}
30+
}
31+
32+
def searchPaths = [
33+
'../react-native/package.json',
34+
'../../react-native/package.json',
35+
'../../../react-native/package.json',
36+
'node_modules/react-native/package.json',
37+
'../node_modules/react-native/package.json',
38+
'../../node_modules/react-native/package.json'
39+
]
40+
41+
for (path in searchPaths) {
42+
def packageFile = new File(project.projectDir, path).getCanonicalFile()
43+
if (packageFile.exists()) {
44+
try {
45+
def json = new JsonSlurper().parse(packageFile)
46+
def version = json.version
47+
if (version) {
48+
if (version == '*' || version.contains('*')) {
49+
return "0.80.0" // Default for wildcard versions
50+
}
51+
return version
52+
}
53+
} catch (Exception e) {
54+
// Ignore parsing errors and continue
55+
}
56+
}
57+
}
58+
return "0.77.0"
59+
}
60+
61+
62+
def rnVersion = getRNVersion()
63+
println "Found react native version as ${rnVersion}"
64+
1765
def isNewArchitectureEnabled() {
1866
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
1967
}
@@ -49,6 +97,7 @@ android {
4997
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
5098
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
5199
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
100+
buildConfigField "String", "REACT_NATIVE_VERSION", "\"${rnVersion}\""
52101
consumerProguardFiles "consumer-rules.pro"
53102
}
54103
buildTypes {

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

Lines changed: 55 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,56 @@ 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 (reactInstanceManager == null) {
328+
Application app = activity.getApplication();
329+
if (app instanceof ReactApplication) {
330+
reactInstanceManager = ((ReactApplication) app).getReactNativeHost().getReactInstanceManager();
331+
}
332+
}
333+
if (newArchEnabled) {
334+
return createReactSurfaceView(viewName);
335+
}
336+
if (reactInstanceManager == null) {
337+
return null;
338+
}
324339
ReactRootView reactRootView = new ReactRootView(activity);
325340
reactRootView.startReactApplication(reactInstanceManager, viewName);
326341
return reactRootView;
327342
}
343+
try {
344+
ReactHost reactHost = ((ReactApplication) activity.getApplication()).getReactHost();
345+
if (reactHost != null) {
346+
Object surface = reactHost.createSurface(
347+
activity,
348+
viewName,
349+
null
350+
);
351+
surface.getClass().getMethod("start").invoke(surface);
352+
353+
return (View) surface.getClass().getMethod("getView").invoke(surface);
354+
}
355+
return null;
356+
} catch (Exception e) {
357+
SdkTracker.trackAndLogBootException(
358+
NAME,
359+
LogConstants.CATEGORY_LIFECYCLE,
360+
LogConstants.SUBCATEGORY_HYPER_SDK,
361+
LogConstants.SDK_TRACKER_LABEL,
362+
"Exception in createMerchantView",
363+
e
364+
);
365+
return null;
366+
}
328367
}
329368

369+
330370
@Nullable
331371
@Override
332372
public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewType) {
333373
Activity activity = (Activity) getCurrentActivity();
334-
if (reactInstanceManager == null || activity == null) {
374+
if (activity == null) {
335375
return super.getMerchantView(viewGroup, merchantViewType);
336376
} else {
337377
View merchantView = null;
@@ -370,6 +410,17 @@ public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewTy
370410
}
371411
}
372412

413+
private Boolean setUseNewApprochForMerchantView() {
414+
String version = BuildConfig.REACT_NATIVE_VERSION;
415+
String[] parts = version.split("\\.");
416+
if (parts.length > 1) {
417+
int major = Integer.parseInt(parts[0]);
418+
int minor = Integer.parseInt(parts[1]);
419+
return major > 0 || (major == 0 && minor >= 82);
420+
}
421+
return false;
422+
}
423+
373424
private void createHyperService(@Nullable String tenantId, @Nullable String clientId) {
374425
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
375426
if (activity == null) {
@@ -384,7 +435,6 @@ private void createHyperService(@Nullable String tenantId, @Nullable String clie
384435
Application app = activity.getApplication();
385436
if (app instanceof ReactApplication) {
386437
this.app = ((ReactApplication) app);
387-
reactInstanceManager = ((ReactApplication) app).getReactNativeHost().getReactInstanceManager();
388438
}
389439
if (tenantId != null && clientId != null) {
390440
hyperServices = new HyperServices(activity, tenantId, clientId);

hyper-sdk-react.podspec

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ require "json"
33
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
44

55
rn_minor_version = 0
6+
rn_major_version = 0
67
[
78
'../react-native/package.json',
89
'../../react-native/package.json',
@@ -16,8 +17,11 @@ rn_minor_version = 0
1617
version = package1 ['version']
1718
if version == '*' || version.include?('*')
1819
rn_minor_version = 80
20+
rn_major_version = 0
1921
else
20-
rn_minor_version = version.split('.')[1].to_i
22+
version_parts = version.split('.')
23+
rn_major_version = version_parts[0].to_i
24+
rn_minor_version = version_parts[1].to_i
2125
end
2226
break
2327
rescue => e
@@ -39,8 +43,11 @@ if rn_minor_version == 0
3943
version = package1 ['version']
4044
if version == '*' || version.include?('*')
4145
rn_minor_version = 80
46+
rn_major_version = 0
4247
else
43-
rn_minor_version = version.split('.')[1].to_i
48+
version_parts = version.split('.')
49+
rn_major_version = version_parts[0].to_i
50+
rn_minor_version = version_parts[1].to_i
4451
end
4552
break
4653
rescue => e
@@ -52,8 +59,9 @@ end
5259
# Fallback if still not found
5360
if rn_minor_version == 0
5461
rn_minor_version = 77
62+
rn_major_version = 0
5563
end
56-
puts ("Found react native minor version as #{rn_minor_version}").yellow
64+
puts ("Found react native minor version as #{rn_major_version}.#{rn_minor_version}").yellow
5765

5866
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
5967

@@ -79,7 +87,7 @@ puts ("HyperSDK Version: #{hyper_sdk_version}")
7987
source_files_array = ["ios/**/*.{h,m,mm,swift}"]
8088
exclude_files = []
8189

82-
if rn_minor_version >= 78
90+
if rn_minor_version >= 78 || (rn_major_version > 0)
8391
source_files_array << "ios/latest/**/*.{h,m,mm,swift}"
8492
exclude_files << "ios/rn77/**/*"
8593
else

0 commit comments

Comments
 (0)