From bdeae70887a687877859b86cf297f99b95e4f7a0 Mon Sep 17 00:00:00 2001 From: Shiva Eravathri Date: Wed, 12 Nov 2025 14:04:20 +0530 Subject: [PATCH] fix: Ensure compatibility with new React Native version --- android/build.gradle | 49 +++++++++++++++ .../hypersdkreact/HyperSdkReactModule.java | 60 +++++++++++++++++-- hyper-sdk-react.podspec | 16 +++-- 3 files changed, 116 insertions(+), 9 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index cff9780..fa9c474 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,5 +1,6 @@ import com.android.Version import in.juspay.payments.core.ClientConfig +import groovy.json.JsonSlurper buildscript { repositories { @@ -14,6 +15,53 @@ buildscript { } } +def getRNVersion() { + def rootPackageFile = new File(rootDir, "../package.json") + if (rootPackageFile.exists()) { + try { + def json = new JsonSlurper().parse(rootPackageFile) + def version = json.dependencies["react-native"] ?: json.devDependencies["react-native"] + if (version) { + return version + } + } catch (Exception e) { + // Ignore parsing errors and continue + } + } + + def searchPaths = [ + '../react-native/package.json', + '../../react-native/package.json', + '../../../react-native/package.json', + 'node_modules/react-native/package.json', + '../node_modules/react-native/package.json', + '../../node_modules/react-native/package.json' + ] + + for (path in searchPaths) { + def packageFile = new File(project.projectDir, path).getCanonicalFile() + if (packageFile.exists()) { + try { + def json = new JsonSlurper().parse(packageFile) + def version = json.version + if (version) { + if (version == '*' || version.contains('*')) { + return "0.80.0" // Default for wildcard versions + } + return version + } + } catch (Exception e) { + // Ignore parsing errors and continue + } + } + } + return "0.77.0" +} + + +def rnVersion = getRNVersion() +println "Found react native version as ${rnVersion}" + def isNewArchitectureEnabled() { return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true" } @@ -49,6 +97,7 @@ android { minSdkVersion getExtOrIntegerDefault('minSdkVersion') targetSdkVersion getExtOrIntegerDefault('targetSdkVersion') buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString() + buildConfigField "String", "REACT_NATIVE_VERSION", "\"${rnVersion}\"" consumerProguardFiles "consumer-rules.pro" } buildTypes { diff --git a/android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java b/android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java index a1a5e8e..ae85054 100644 --- a/android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java +++ b/android/src/main/java/in/juspay/hypersdkreact/HyperSdkReactModule.java @@ -22,6 +22,7 @@ import androidx.fragment.app.FragmentActivity; import com.facebook.react.ReactApplication; +import com.facebook.react.ReactHost; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactRootView; import com.facebook.react.bridge.ActivityEventListener; @@ -89,6 +90,8 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A private boolean wasProcessWithActivity = false; + private boolean useNewApprochForMerchantView = false; + private Set registeredComponents = new HashSet<>(); @NonNull @@ -97,6 +100,7 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A HyperSdkReactModule(ReactApplicationContext reactContext) { super(reactContext); this.context = reactContext; + useNewApprochForMerchantView = setUseNewApprochForMerchantView(); reactContext.addActivityEventListener(this); } @@ -318,20 +322,56 @@ public View createReactSurfaceView(String viewName) { } private View createMerchantView(String viewName) { - if (newArchEnabled) { - return createReactSurfaceView(viewName); - } else { + + if (!useNewApprochForMerchantView) { + if (reactInstanceManager == null) { + Application app = activity.getApplication(); + if (app instanceof ReactApplication) { + reactInstanceManager = ((ReactApplication) app).getReactNativeHost().getReactInstanceManager(); + } + } + if (newArchEnabled) { + return createReactSurfaceView(viewName); + } + if (reactInstanceManager == null) { + return null; + } ReactRootView reactRootView = new ReactRootView(activity); reactRootView.startReactApplication(reactInstanceManager, viewName); return reactRootView; } + try { + ReactHost reactHost = ((ReactApplication) activity.getApplication()).getReactHost(); + if (reactHost != null) { + Object surface = reactHost.createSurface( + activity, + viewName, + null + ); + surface.getClass().getMethod("start").invoke(surface); + + return (View) surface.getClass().getMethod("getView").invoke(surface); + } + return null; + } catch (Exception e) { + SdkTracker.trackAndLogBootException( + NAME, + LogConstants.CATEGORY_LIFECYCLE, + LogConstants.SUBCATEGORY_HYPER_SDK, + LogConstants.SDK_TRACKER_LABEL, + "Exception in createMerchantView", + e + ); + return null; + } } + @Nullable @Override public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewType) { Activity activity = (Activity) getCurrentActivity(); - if (reactInstanceManager == null || activity == null) { + if (activity == null) { return super.getMerchantView(viewGroup, merchantViewType); } else { View merchantView = null; @@ -370,6 +410,17 @@ public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewTy } } + private Boolean setUseNewApprochForMerchantView() { + String version = BuildConfig.REACT_NATIVE_VERSION; + String[] parts = version.split("\\."); + if (parts.length > 1) { + int major = Integer.parseInt(parts[0]); + int minor = Integer.parseInt(parts[1]); + return major > 0 || (major == 0 && minor >= 82); + } + return false; + } + private void createHyperService(@Nullable String tenantId, @Nullable String clientId) { FragmentActivity activity = (FragmentActivity) getCurrentActivity(); if (activity == null) { @@ -384,7 +435,6 @@ private void createHyperService(@Nullable String tenantId, @Nullable String clie Application app = activity.getApplication(); if (app instanceof ReactApplication) { this.app = ((ReactApplication) app); - reactInstanceManager = ((ReactApplication) app).getReactNativeHost().getReactInstanceManager(); } if (tenantId != null && clientId != null) { hyperServices = new HyperServices(activity, tenantId, clientId); diff --git a/hyper-sdk-react.podspec b/hyper-sdk-react.podspec index c14c29e..1345198 100644 --- a/hyper-sdk-react.podspec +++ b/hyper-sdk-react.podspec @@ -3,6 +3,7 @@ require "json" package = JSON.parse(File.read(File.join(__dir__, "package.json"))) rn_minor_version = 0 +rn_major_version = 0 [ '../react-native/package.json', '../../react-native/package.json', @@ -16,8 +17,11 @@ rn_minor_version = 0 version = package1 ['version'] if version == '*' || version.include?('*') rn_minor_version = 80 + rn_major_version = 0 else - rn_minor_version = version.split('.')[1].to_i + version_parts = version.split('.') + rn_major_version = version_parts[0].to_i + rn_minor_version = version_parts[1].to_i end break rescue => e @@ -39,8 +43,11 @@ if rn_minor_version == 0 version = package1 ['version'] if version == '*' || version.include?('*') rn_minor_version = 80 + rn_major_version = 0 else - rn_minor_version = version.split('.')[1].to_i + version_parts = version.split('.') + rn_major_version = version_parts[0].to_i + rn_minor_version = version_parts[1].to_i end break rescue => e @@ -52,8 +59,9 @@ end # Fallback if still not found if rn_minor_version == 0 rn_minor_version = 77 + rn_major_version = 0 end -puts ("Found react native minor version as #{rn_minor_version}").yellow +puts ("Found react native minor version as #{rn_major_version}.#{rn_minor_version}").yellow folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32' @@ -79,7 +87,7 @@ puts ("HyperSDK Version: #{hyper_sdk_version}") source_files_array = ["ios/**/*.{h,m,mm,swift}"] exclude_files = [] -if rn_minor_version >= 78 +if rn_minor_version >= 78 || (rn_major_version > 0) source_files_array << "ios/latest/**/*.{h,m,mm,swift}" exclude_files << "ios/rn77/**/*" else