You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native+Expo环境下RNPermissions为null的问题求助

解决react-native-permissions在Expo Android环境下NativeModule.RNPermissions为空的问题

问题重现

启动Expo Dev Client构建的React Native应用时,Android模拟器和浏览器端均出现以下错误:

index.ts:6 Uncaught 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

已尝试无效操作

  • 在AndroidManifest.xml中添加相机权限
  • 多次重装node_modules和yarn.lock
  • 降级react-native-permissions至3.6.1
  • 安装pod-install(仅使用Android环境)
  • 尝试react-native link(提示命令无效)

有效解决方案

1. 配置Expo权限声明

在项目根目录的app.json(或app.config.js)中,明确添加Android所需权限,确保Expo预构建时能正确注入权限配置:

{
  "expo": {
    "android": {
      "permissions": [
        "CAMERA" // 根据业务需求添加其他权限
      ]
    }
  }
}

2. 重新预构建Android原生项目

由于使用的是Expo Dev Client,需通过expo prebuild生成适配的原生代码,替代传统的react-native link:

expo prebuild --platform android

3. 清理缓存并重新构建

  1. 清理Android构建缓存:
    cd android && ./gradlew clean && cd ..
    
  2. 重置Metro缓存并启动开发服务器:
    yarn start --reset-cache
    
  3. 重新运行Android应用:
    yarn android
    

4. Web环境适配(可选)

react-native-permissions不支持Web环境,若需在浏览器中运行,需添加mock模块:
创建mock.js文件,内容如下:

import { NativeModules } from 'react-native';

NativeModules.RNPermissions = {
  check: () => Promise.resolve('granted'),
  request: () => Promise.resolve('granted'),
  requestMultiple: () => Promise.resolve({}),
  checkMultiple: () => Promise.resolve({}),
  openSettings: () => Promise.resolve(true),
};

在Web环境入口文件中导入该mock:

import { Platform } from 'react-native';
if (Platform.OS === 'web') {
  require('./mock.js');
}

内容的提问来源于stack exchange,提问作者ellen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 18:32:23