跳到主要内容
新架构实战课 实操 + 基建 + 原理全维度包揽,抢先掌握 React Native 新架构精髓 立即查看 >

移除不需要的权限

默认情况下我们会给最终打包的 APK 加入一些权限申请,具体如下:

  • android.permission.INTERNET - 调试模式所需。
  • android.permission.SYSTEM_ALERT_WINDOW - 调试模式所需。
  • android.permission.READ_PHONE_STATE - 非必需。
  • android.permission.WRITE_EXTERNAL_STORAGE - 非必需。
  • android.permission.READ_EXTERNAL_STORAGE - 非必需。

这些权限申请会在用户安装或运行时弹出提示打断用户甚至使用户感到反感,所以我们应该尽量移除不必要的权限申请,以改进用户体验和提升用户好感。

  1. 我们首先可以考虑移除READ_PHONE_STATEWRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE三项非必需的权限。即便你使用了AsyncStorage也不需要这三个权限,所以移除是很安全的。当你以后需要使用时,还可以再加回来。

  2. 打开android/app/src/main/AndroidManifest.xml文件。

  3. Even though these three permissions are not listed in the manifest they get added in. We add the three permissions with tools:node="remove" attribute, to make sure it gets removed during build. Note that the package identifier will be different, for below it is "com.myapp" because the project was created with npx react-native init myapp.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myappid"
    + xmlns:tools="http://schemas.android.com/tools"
    >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    + <uses-permission tools:node="remove" android:name="android.permission.READ_PHONE_STATE" />
    + <uses-permission tools:node="remove" android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    + <uses-permission tools:node="remove" android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:allowBackup="false"
    android:theme="@style/AppTheme">
    <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>

    </manifest>

That's it. We did not remove the INTERNET permission as pretty much all apps use it. Now whenever you create a production APK all these 4 permissions will be removed. When you create a debug APK (npx react-native run-android) it will install the APK with all four permissions removed.

提示

If your App is free to use in the App-Store and there is no "In-App-Purchase" possible in your App, you also can remove:

  • android.vending.CHECK_LICENSE