Skip to content

Commit 9c093ee

Browse files
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 <noreply@anthropic.com>
1 parent e69c305 commit 9c093ee

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## Unreleased
2+
3+
### Fixes
4+
- [#793] Fixed `IterableApi` unresolved reference in `MainApplication.kt` when using
5+
new architecture (RN 0.76+, Expo 52). The SDK now automatically calls
6+
`IterableApi.setContext()` during module initialization, so users no longer need
7+
to add this call manually in their Android application code.
8+
19
## 2.2.0
210

311
### Updates

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ Notes:
7676
- Ensure your app is configured for New Architecture per the React Native docs.
7777
- The example app in this repository is configured with New Architecture enabled.
7878

79+
### Android Setup (RN 0.76+ / New Architecture)
80+
81+
Starting with version 2.2.1, the SDK **automatically** calls `IterableApi.setContext()`
82+
when the React Native module is initialized. You no longer need to manually add
83+
`IterableApi.setContext(this)` in your `MainApplication.kt` or `MainActivity.kt`.
84+
85+
If you are upgrading from an older version, you can safely **remove** the following
86+
from your Android code:
87+
88+
```kotlin
89+
// No longer needed - remove this import
90+
import com.iterable.iterableapi.IterableApi
91+
92+
// No longer needed - remove this call
93+
IterableApi.setContext(this)
94+
```
95+
96+
The SDK's initialization (via JavaScript/TypeScript `Iterable.initialize()`) handles
97+
all necessary Android context setup automatically.
98+
99+
> **Note for Expo users (SDK 52+):** No additional native Android configuration is
100+
> required beyond installing the package. The SDK handles context initialization
101+
> automatically through autolinking.
102+
79103
## Beta Versions
80104
81105
To opt into beta versions of the SDK, you can install the latest beta version by using the `beta` tag:

android/src/main/java/com/iterable/reactnative/RNIterableAPIPackage.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
import com.facebook.react.bridge.ReactApplicationContext;
1111
import com.facebook.react.module.model.ReactModuleInfo;
1212
import com.facebook.react.module.model.ReactModuleInfoProvider;
13+
import com.iterable.iterableapi.IterableApi;
1314

1415
public class RNIterableAPIPackage extends BaseReactPackage {
1516
@Nullable
1617
@Override
1718
public NativeModule getModule(@NonNull String name, @NonNull ReactApplicationContext reactContext) {
1819
if (RNIterableAPIModuleImpl.NAME.equals(name)) {
20+
// Automatically set the context so users don't need to call
21+
// IterableApi.setContext() manually from MainApplication or MainActivity.
22+
// This resolves issues with IterableApi being unresolvable in
23+
// MainApplication.kt when using the new architecture (RN 0.76+).
24+
IterableApi.setContext(reactContext.getApplicationContext());
1925
return new RNIterableAPIModule(reactContext);
2026
} else {
2127
return null;

example/android/app/src/main/java/iterable/reactnativesdk/example/MainActivity.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.facebook.react.ReactActivity
66
import com.facebook.react.ReactActivityDelegate
77
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
88
import com.facebook.react.defaults.DefaultReactActivityDelegate
9-
import com.iterable.iterableapi.IterableApi
109

1110
class MainActivity : ReactActivity() {
1211

@@ -28,7 +27,6 @@ class MainActivity : ReactActivity() {
2827
* This being in Kotlin **may** cause issues with react-native-screens
2928
*/
3029
override fun onCreate(savedInstanceState: Bundle?) {
31-
IterableApi.setContext(this)
3230
// Call super.onCreate with null to prevent savedInstanceState restoration issues
3331
super.onCreate(null)
3432
}

0 commit comments

Comments
 (0)