启动React Native Android应用时遇NativeModule.RNPermissions null错误求助
启动React Native Android应用时遇到NativeModule.RNPermissions为null的错误
错误信息
Error: react-native-permissions: NativeModule.RNPermissions is null. To fix this issue try these steps: • If you are using CocoaPods on iOS, run `pod install` in the `ios` directory and then clean, rebuild and re-run the app. You may also need to re-open Xcode to get the new pods. • If you are getting this error while unit testing you need to mock the native module. You can use this to get started: https://github.com/react-native-community/react-native-permissions/blob/master/mock.js If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-permissions
已尝试的解决步骤
- 执行
pod install - 清理并重建应用
- 运行
npx react-native-clean-project
当前Podfile内容
require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' permissions_path = '../node_modules/react-native-permissions/ios' pod 'Permission-Camera', :path => "#{permissions_path}/Camera" # pod 'Permission-Microphone', :path => "#{permissions_path}/Microphone.podspec" pod 'Permission-PhotoLibrary', :path => "#{permissions_path}/PhotoLibrary" platform :ios, '11.0' target 'BayQi' do pod 'react-native-contacts', :path => '../node_modules/react-native-contacts' rn_maps_path = '../node_modules/react-native-maps' pod 'react-native-google-maps', :path => rn_maps_path pod 'GoogleMaps' pod 'Google-Maps-iOS-Utils' config = use_native_modules! use_react_native!(:path => config["reactNativePath"]) target 'BayQiTests' do inherit! :complete # Pods for testing end # Enables Flipper. # # Note that if you have use_frameworks! enabled, Flipper will not work and # you should disable these next few lines. # use_flipper! # post_install do |installer| # flipper_post_install(installer) # end end target 'BayQi-tvOS' do # Pods for BayQi-tvOS target 'BayQi-tvOSTests' do inherit! :search_paths # Pods for testing end end
解决方案
你是在Android环境下触发的错误,但当前仅提供了iOS的Podfile配置,重点排查Android端的原生集成问题,同时修正iOS Podfile的错误配置:
1. 检查Android原生集成配置
- 打开
android/app/src/main/java/[你的应用包名]/MainApplication.java,确认RNPermissionsPackage已添加到包列表:import com.reactnativecommunity.permissions.RNPermissionsPackage; @Override protected List<ReactPackage> getPackages() { List<ReactPackage> packages = new PackageList(this).getPackages(); packages.add(new RNPermissionsPackage()); // 确认此行存在 return packages; } - 打开
android/settings.gradle,确认包含以下配置:include ':react-native-permissions' project(':react-native-permissions').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-permissions/android')
2. 修正iOS Podfile配置(避免后续iOS端出现同类问题)
当前Podfile中权限组件的声明放在了target外部,属于错误配置,需移到target 'BayQi'内部:
target 'BayQi' do pod 'react-native-contacts', :path => '../node_modules/react-native-contacts' rn_maps_path = '../node_modules/react-native-maps' pod 'react-native-google-maps', :path => rn_maps_path pod 'GoogleMaps' pod 'Google-Maps-iOS-Utils' config = use_native_modules! use_react_native!(:path => config["reactNativePath"]) # 移动权限pod声明到此处 permissions_path = '../node_modules/react-native-permissions/ios' pod 'Permission-Camera', :path => "#{permissions_path}/Camera" # pod 'Permission-Microphone', :path => "#{permissions_path}/Microphone" pod 'Permission-PhotoLibrary', :path => "#{permissions_path}/PhotoLibrary" target 'BayQiTests' do inherit! :complete # Pods for testing end # ... 其他代码 end
修改后执行cd ios && pod install,清理Xcode缓存后重新编译。
3. 彻底清理缓存并重建
执行以下命令清理全链路缓存,再重新构建应用:
npm cache clean --force cd android && ./gradlew clean cd .. npm install npx react-native run-android
4. 版本兼容性校验
确认react-native与react-native-permissions版本适配,若版本差异过大,尝试升级或降级到官方文档标注的兼容版本。
内容的提问来源于stack exchange,提问作者nouman-stackone
相关产品推荐
相关产品推荐

