Skip to content
Matthias Urhahn edited this page Aug 5, 2024 · 39 revisions

Setting up SD Maid SE

Information about different SD Maid SE setup steps.

SD Maid adapts to what access is available. While all permissions are optional, if you don't grant any permissions, most features will not work. SD Maid will perform best when all setup items are completed.

SD Maid will automatically try to complete some of the setup steps if root or Shizuku is available.

Some permissions overlap or are supersets of each other, e.g. you can give SD Maid access to both Root and Shizuku. SD Maid will always try to use the best possible access method for any action, and if that fails, retry with the second best access method and then third best etc. Granting SD Maid overlapping permissions improves reliability.

Manage Storage

The "manage storage" permission is a broadly scoped storage access permission.

  • On Android 12 and earlier it grants access to all public storage.
  • On Android 13+ it still grants access to all public storage, but not to sub-directories under Android/data.

Usage statistics

Usage statistics permissions give SD Maid access to data, cache sizes and if an app is running or when it was last used.

Storage Access Framework

SD Maid uses the Android Storage Access framework (SAF) to gain access to additional storage areas. SD Maid will ask you to grant access to specific paths that are not accessible by just having the "Manage storage" permission (Just Android Things™️).

  • Press the Grant access button
  • A new window opens
  • If the correct path is already selected, otherwise you have to navigate to the path that SD Maid wants.
  • Then press the Use this folder button, the setup entry for this path will unlock and be colored green.

Repeat this process until all paths are green.

SAF: Troubleshooting

For when things don't go as planned.

SAF: Android/data|obb access

Granting access to Android/data|obb is not possible with newer versions of the system's Files app (com.google.android.documentsui) on Android 11 and Android 12. If the installed Files app targets Android 14 (API-level 34) or newer, then SD Maid will hide the SAF access request for these paths (like on Android 13+).

Google Play updates the Files app through the Google Play system update service. The original "Files" app (build for API-level 31, e.g. aml_doc_310851020 (310851020)) that came with Android 12 OS, allows SD Maid to open the SAF path selection window on Android/Data while the latest "Files" app (build for API-level 34 or later, e.g. 14-10492947 (340916000)) prevents this.

Workaround:

You can uninstall the update to the Files and then set up SD Maid. Updating the Files app again will not break already granted permissions.

  • Open device settings
  • Go to "Apps & notifications" or "Application Manager"
  • Find the Files system app
  • Tap on the three-dot menu, usually on the top right corner
  • Choose "Uninstall updates"

or

adb uninstall com.google.android.documentsui

SAF: OPEN_DOCUMENT_TREE ActivityNotFoundException

Granting access to paths via the Storage Access Framework requires an app that lets the user confirm the paths to which access will be granted. This app is the "Documents" app. It's a system app that is built into every Android device since Android 5.0 (com.android.documentsui or com.google.android.documentsui). If you get this error, then either someone deleted or disabled this app. In most cases someone ran an overzealous "de-bloating" tool that disabled it. You can re-enable it via ADB:

adb shell pm enable com.android.documentsui
or
adb shell pm enable com.google.android.documentsui

SAF: Empty window

If the window used to grant access to paths is empty, then your external storage provider package (com.android.externalstorage) is maybe disabled (likely through an overzealous "de-bloating tool). You can re-enable it via ADB:

adb shell pm enable com.android.externalstorage

Running the Google Play system update again will restore the update. You can also manually download an updated version from APKMirror.com.

Notifications

Without this permission SD Maid won't be able to post notifications. On some ROMs this may negatively affect reliability when executing in the background.

App inventory

SD Maid requires the permission QUERY_ALL_PACKAGES to check which apps are installed on your device (i.e. "visibility into the inventory of installed apps"). Also see "Usage Stats Permission"

The QUERY_ALL_PACKAGES permission is mandatory for:

  • CorpseFinder: To understand which files can be deleted, SD Maid needs to know if you have apps installed that might need them.
  • AppCleaner: Determine caches for each app
  • AppControl: Being able to list all installed apps
  • StorageAnalyzer: Attribute used storage to installed apps

This permission exists since Android 11 and is normally granted by default. Since Android 14 some devices (e.g. #1015, #1354) exist where the permission is not granted by default or can be revoked.

Accessibility Service

The accessibility service allows SD Maid to interact with your device like a human, i.e. viewing the screen and pressing buttons. SD Maid can use it to automate tedious actions like pressing "Clear cache" on hundreds of apps. The accessibility service is a service that the app itself can not launch, it must be launched by the system, because this type of service grants an app additional access to your device.

ACS: Troubleshooting

The service may stop running and require you to enable it again in various circumstances:

  • You update the app.
  • The app crashes. If this happens you should report this as a bug. If this happens too often you may not be able to restart it by turning it off/on again until the device is rebooted.
  • The system kills the app due to overly aggressive "battery optimizations" (also see https://dontkillmyapp.com/)

ACS: Service is disabled

Some ROMs (e.g. MIUI) are notorious for aggressively stopping everything that is not whitelisted by the device manufacturer. If you are constantly asked to re-enable the accessibility service then the system may be disabling it every time SD Maid is not in active use.

Try to disable battery optimisations and on MIUI ROMs there is an extra setting called "Autostart" enable that for SD Maid. For Xiaomi/POCO/Redmi devices:

  • Go to Security>Manage applications>Auto start in background and allow this for SD Maid.
  • Go to Security>Increase speed>Lock apps and select SD Maid.

You can also consider granting additional permission that allow SD Maid to restart the service automatically.

ACS: Enable via ADB

If your device doesn't have a way to enable accessibility services via screen (e.g. a TV device), it's possible to enable the service using ADB by running:

adb shell settings put secure enabled_accessibility_services eu.darken.sdmse/.automation.core.AutomationService

ACS: Automatic service restart

Note: This is an advanced technique and targeted towards power users.

You can grant an additional permission that allows SD Maid to restart the service on its own. The permission is called WRITE_SECURE_SETTINGS and can only be granted via ADB. If you have this set up, run this command to grant the permission:

adb shell pm grant eu.darken.sdmse android.permission.WRITE_SECURE_SETTINGS

With this permission granted, SD Maid can use the command described here to enable the service when necessary. SD Maid will only use this if you have enabled the accessibility service at least once manually. If you go into setup again and tell SD Maid to not use the accessibility service, then this automatic restart technique will not be used until you manually enable the accessibility service again.

To remove this permission from SD Maid again, use this command:

adb shell pm revoke eu.darken.sdmse android.permission.WRITE_SECURE_SETTINGS

If you are getting Operation not allowed: java.lang.SecurityException, see here. Some devices restrict ADB permission. See if there is an extra setting (e.g. Disable Permission Monitoring) in the developer settings that needs to be toggled, after using the command, you can disable it again.

ACS: AppOps Restrictions

Android 13+ may apply additional restrictions to apps that have been side-loaded. So if SD Maid is not installed from a known (to Google) source like Google Play or other app stores, then you from may not be able to directly grant some permissions to SD Maid, e.g. start an accessibility service.

To lift these restrictions open SD Maids app settings screen in the system. Open the menu in the top right corner and select Allow restricted settings, afterwards you can go back to the list of accessibility services and enable SD Maid's service.

Note: To improve usability (this is sarcasm), Google decided that this setting will only be visible if:

  • You at least saw the "Restricted setting" once after clicking SD Maids accessibility service entry
  • You have opened the system settings and navigated to SD Maid's settings page by hand, and not through another app or shortcuts.

😮‍💨

Some additional background info can be found here.

Root access

With root access, SD Maid has full access to your device and can perform the most thorough scan possible. Usually this means that you unlocked your bootloader and set up Magisk.

If SD Maid thinks that your device may be rooted, then SD Maid will ask for root access first. If root access is granted, SD Maid will use the root access to grant as many of the other permission as possible. Root access is the highest level of access you could give an app, the other permission are then only required for ease of use and performance, which is why they don't require extra consent during setup if SD Maid already has root.

Shizuku

Shizuku is a special app that you can install and set up that can be used to grant elevated access/permissions to apps. It is a modification you can make to your phone that other apps can take advantage of.

It's similar to root, but not the same. The permissions are slightly less powerful, but the modification is not permanent and doesn't require you to unlock your bootloader.

The user manual to set up Shizuku can be found here.

After you have set up Shizuku:

  • Give consent for SD Maid to use Shizuku. This is internal to SD Maid, you can use it to enable/disable SD Maid's use of Shizuku
  • After giving consent, a Grant access dialog from Shizuku should show
  • Confirm the dialog
  • SD Maid should display a new status indicator that show whether SD Maid can connect to the Shizuku service

Setting up Shizuku, if you already have granted SD Maid Root access doesn't enable additional features, but improves reliability. If actions fail with Root permission, then SD Maid will retry with Shizuku.

Shizuku: Troubleshooting

You can check SD Maid's Shizuku status by going to Settings > Setup and viewing the Shizuku setup card. It should say Shizuku link is ready. If it says Waiting for link with Shizuku check that the Shizuku app itself is working, it might need to be set up again.