Q群 567530369,小伙伴们都在这里等你哦~

移除不需要的权限

默认情况下我们会给最终打包的 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 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>
    
  4. Now let's remove SYSTEM_ALERT_WINDOW from the production APK only.

  5. Go to the android/app/src/ directory. Create a new directory inside this directory, called release. (path: android/app/src/release/)

  6. Inside this android/app/src/release/ directory create a AndroidManifest.xml file. (path: android/app/src/release/AndroidManifest.xml)

  7. Inside this file paste the following conents. Note, make sure to update your package identifier from "com.myapp" to what yours is.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.myapp"
        xmlns:tools="http://schemas.android.com/tools">
    
        <uses-permission tools:node="remove" android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
    </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 (react-native run-android) it will install the APK with only the three permissions removed, and SYSTEM_ALERT_WINDOW will remain.