React Native 0.55.4 Release构建崩溃:react-native-android-sms-listener异常
I’ve run into similar headaches with older React Native versions and third-party native modules, so let’s break down how to resolve this Release-only crash.
The Root Cause
The error undefined is not a function (evaluating 'arguments.[0].indexOf('Requiring module \'NativeModules\' by name')') tells us there’s a problem with how the module is resolved in Release mode. This usually stems from:
- ProGuard obfuscating critical code that React Native needs to locate
NativeModules - The module’s JS wrapper not playing nice with RN 0.55.4’s Release bundling/compression rules
- Incomplete native module linking that only surfaces in Release builds (even though Debug works)
Step-by-Step Solutions
1. Update ProGuard Rules to Block Obfuscation
Open android/app/proguard-rules.pro and add these lines to protect the SMS listener module and core React Native bridge code from being mangled:
# Keep react-native-android-sms-listener classes intact -keep class com.centaurwarchief.smslistener.** { *; } # Protect core React Native NativeModules dependencies -keep class com.facebook.react.modules.core.** { *; } -keep class com.facebook.react.bridge.** { *; }
ProGuard loves stripping or renaming "unused" code in Release builds—this ensures the classes needed for JS-native communication stay intact.
2. Double-Check Native Module Linking
Even if Debug works, confirm the module is properly registered in your Android project:
- Open
android/app/src/main/java/[your-package-name]/MainApplication.java - Verify you have the import and package registration:
import com.centaurwarchief.smslistener.SmsListenerPackage; // ... @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new SmsListenerPackage() // Make sure this line exists! ); }
If it’s missing, add it and re-run react-native link react-native-android-sms-listener to ensure all links are updated.
3. Bypass the Module’s JS Wrapper (Direct NativeModules Import)
The module’s built-in JS wrapper might be causing issues in Release mode. Try importing the module directly from NativeModules instead:
Replace your existing import:
import SmsListener from 'react-native-android-sms-listener';
With:
import { NativeModules } from 'react-native'; const SmsListener = NativeModules.SmsListener;
This skips any potentially problematic wrapper code and directly accesses the native module the way React Native expects.
4. Clean and Rebuild the Release Package
Old build artifacts can cause weird Release-only bugs. Run these commands to start fresh:
# Navigate to android directory and clean gradle cd android && ./gradlew clean # Bundle JS for Release cd .. && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ # Build and run the Release variant react-native run-android --variant=release
Final Notes
RN 0.55.4 is an older version, so compatibility with newer modules can hit snags. If the above steps don’t work, you might want to check if the module has open issues for older RN versions, or consider forking it to tweak the native/JS code to match RN 0.55.4’s expectations.
内容的提问来源于stack exchange,提问作者Nicolas

