From 9c093ee278de6757b315605e33b57c24bdda434f Mon Sep 17 00:00:00 2001 From: Franco Zalamena Date: Tue, 7 Apr 2026 16:08:27 +0100 Subject: [PATCH] fix: auto-set IterableApi context in RN module initialization (#793) Users on RN 0.76+ with new architecture could not resolve IterableApi from MainApplication.kt. The SDK now automatically calls setContext() during native module registration, removing the need for manual setup. Co-Authored-By: Claude Opus 4.6 --- CHANGELOG.md | 8 +++++++ README.md | 24 +++++++++++++++++++ .../reactnative/RNIterableAPIPackage.java | 6 +++++ .../reactnativesdk/example/MainActivity.kt | 2 -- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74bcf9f30..b6d11b3fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## Unreleased + +### Fixes +- [#793] Fixed `IterableApi` unresolved reference in `MainApplication.kt` when using + new architecture (RN 0.76+, Expo 52). The SDK now automatically calls + `IterableApi.setContext()` during module initialization, so users no longer need + to add this call manually in their Android application code. + ## 2.2.0 ### Updates diff --git a/README.md b/README.md index 16266121f..a995a6aac 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,30 @@ Notes: - Ensure your app is configured for New Architecture per the React Native docs. - The example app in this repository is configured with New Architecture enabled. +### Android Setup (RN 0.76+ / New Architecture) + +Starting with version 2.2.1, the SDK **automatically** calls `IterableApi.setContext()` +when the React Native module is initialized. You no longer need to manually add +`IterableApi.setContext(this)` in your `MainApplication.kt` or `MainActivity.kt`. + +If you are upgrading from an older version, you can safely **remove** the following +from your Android code: + +```kotlin +// No longer needed - remove this import +import com.iterable.iterableapi.IterableApi + +// No longer needed - remove this call +IterableApi.setContext(this) +``` + +The SDK's initialization (via JavaScript/TypeScript `Iterable.initialize()`) handles +all necessary Android context setup automatically. + +> **Note for Expo users (SDK 52+):** No additional native Android configuration is +> required beyond installing the package. The SDK handles context initialization +> automatically through autolinking. + ## Beta Versions To opt into beta versions of the SDK, you can install the latest beta version by using the `beta` tag: diff --git a/android/src/main/java/com/iterable/reactnative/RNIterableAPIPackage.java b/android/src/main/java/com/iterable/reactnative/RNIterableAPIPackage.java index 3fade361b..d5523439d 100644 --- a/android/src/main/java/com/iterable/reactnative/RNIterableAPIPackage.java +++ b/android/src/main/java/com/iterable/reactnative/RNIterableAPIPackage.java @@ -10,12 +10,18 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.module.model.ReactModuleInfo; import com.facebook.react.module.model.ReactModuleInfoProvider; +import com.iterable.iterableapi.IterableApi; public class RNIterableAPIPackage extends BaseReactPackage { @Nullable @Override public NativeModule getModule(@NonNull String name, @NonNull ReactApplicationContext reactContext) { if (RNIterableAPIModuleImpl.NAME.equals(name)) { + // Automatically set the context so users don't need to call + // IterableApi.setContext() manually from MainApplication or MainActivity. + // This resolves issues with IterableApi being unresolvable in + // MainApplication.kt when using the new architecture (RN 0.76+). + IterableApi.setContext(reactContext.getApplicationContext()); return new RNIterableAPIModule(reactContext); } else { return null; diff --git a/example/android/app/src/main/java/iterable/reactnativesdk/example/MainActivity.kt b/example/android/app/src/main/java/iterable/reactnativesdk/example/MainActivity.kt index ad3cf31b4..947c0742c 100644 --- a/example/android/app/src/main/java/iterable/reactnativesdk/example/MainActivity.kt +++ b/example/android/app/src/main/java/iterable/reactnativesdk/example/MainActivity.kt @@ -6,7 +6,6 @@ import com.facebook.react.ReactActivity import com.facebook.react.ReactActivityDelegate import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled import com.facebook.react.defaults.DefaultReactActivityDelegate -import com.iterable.iterableapi.IterableApi class MainActivity : ReactActivity() { @@ -28,7 +27,6 @@ class MainActivity : ReactActivity() { * This being in Kotlin **may** cause issues with react-native-screens */ override fun onCreate(savedInstanceState: Bundle?) { - IterableApi.setContext(this) // Call super.onCreate with null to prevent savedInstanceState restoration issues super.onCreate(null) }