Solutions of the External Storage Access Problem Due to Recent Change in Minimum SDK 30 Mandate by Google for React Native Android Apps

Sohel Kabir
3 min readNov 11, 2021

Google recently changed their rule to use target SDK of Android apps to be minimum 30 after 1st November, 2021. After this date you can not publish your app to play store if you target your SDK less than 30. There are few changes to be made in your app if you set target SDK to 30. We will discuss the change related to accessing external file storage from Android app.

Up until SDK 29 developer used WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE in their manifest file. But no longer you can use this permission tag to access external storage from API level 30 (Android 11). From now on you need to use MANAGE_EXTERNAL_STORAGE in the manifest and use ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent get “All files access permission”. But here is the catch, all apps can not use this permission without showing proper justification to Google during app submission to play store otherwise they will reject the app. Which category of app can use this API google wrote this here.

If you used libraries like react-native-fs to access external file storage it will give error like “Operation not permitted” during read write in greater than Android 11 devices. So solutions to the problems are,

Solution 1: Use new All Files Access API and show proper justification to Google to use this API.

Solution 2: Use Documents directory which has access to your app by default and your files will be stored there. For example with react-native-fs

var path = RNFS.DocumentDirectoryPath + '/test.txt';

But the problem with this approach is when your app is uninstalled all data related to your app gets deleted including Documents directory we talked about. So if you need your data to persist across the install and uninstall procedure your only hope is to use solution 1.

Now if you don’t know how to ask for “All Files Access” permission in react native this part is for you. Since writing of this blog there isn’t any library which implemented this “All Files Access” permission including the popular “react-native-permissions” library. So I had to implement it myself in native java code and create a bridge to ask for permission. I took some help from Stack Overflow to implement this code. So here is the process,

All Files Access Permission Implementation:

First you need to check whether your android/build.gradle file’s target SDK and build tools versions is greater than 30 or not otherwise you can not use this code. I’m providing my code sample here

buildToolsVersion = “30.0.2”minSdkVersion = 21compileSdkVersion = 30targetSdkVersion = 30ndkVersion = “21.4.7075529”

Add this line to AndroidManifest.xml

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

Now go to this file

 android/app/src/main/java/{your_package_name}

Add a file named CustomModule.java and this code to the file.

Don’t forget to edit first line of the code and replace your app package name.

Now add another file named CustomModulePackage.java and add this code to the file.

Don’t forget to edit first line of the code and replace your app package name.

Now add the new package to the MainApplication.java file in the same directory. All you have to do is add one line to that file’s getPackages function like this

Now native code is done you need import the CustomModule package to your react native code. Go to your react native component where you want to ask “All file access” permission and write add this code like this

All is done. Now rebuild the whole project and test your app.

Don’t forget give a like. If you need professional help reach me at sohel.nkabir@gmail.com

Sources:

https://developer.android.com/about/versions/11/privacy/storage

https://developer.android.com/training/data-storage/manage-all-files

https://stackoverflow.com/questions/63392170/write-files-to-the-android-external-storage-using-react-native-fs/69820147#69820147

--

--