TechBuzzPro

How to Get WRITE_SECURE_SETTINGS on Android

· 6 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You cannot grant WRITE_SECURE_SETTINGS to your app from inside the app or from the normal Settings screens. The permission is signature-level, so it is only granted to apps signed with the platform key or installed on a system image. The only practical way for a normal developer or power user is to grant it once over ADB with the pm grant command, which works on a debuggable build or a device where you have enabled USB debugging. That grant survives until the app is uninstalled or the permission is revoked, but it does not survive a factory reset. You do not need root for the ADB method, and you should not try to fake the permission by editing the manifest alone: declaring it in the manifest is necessary but not sufficient.

What You'll Need

  • A Windows, macOS or Linux computer with the Android SDK Platform Tools installed (the official package that contains adb).
  • A USB cable that supports data transfer, not a charge-only cable.
  • An Android device with Developer options enabled and USB debugging turned on. On most phones this is under Settings > About phone, where you tap Build number seven times, then Settings > System > Developer options. Labels vary slightly by manufacturer and Android version.
  • The exact package name of your app, for example com.example.myapp. You can find it in your build.gradle under applicationId or by running adb shell pm list packages and filtering.
  • Your app installed on the device, ideally a debug build. Release builds work too, but the grant must be repeated if you uninstall and reinstall.

Step-by-Step

  1. Declare the permission in your manifest. Add <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /> inside the <manifest> element, outside <application>. Without this line the grant command will fail with an error saying the package does not request the permission. Keep the declaration even though the system will not grant it automatically.
  2. Install the app on the device. Build and install your debug APK, or install a release build through the Play Store or an official channel. Make sure the package name matches the one you will use in the grant command.
  3. Enable USB debugging. Open Settings > System > Developer options and turn on USB debugging. On some phones the path is Settings > Developer options directly. Connect the phone to the computer with the data cable.
  4. Authorize the computer. On the phone, a dialog asks whether to allow USB debugging from the connected computer's RSA key. Tap Allow, and tick the option to always allow if you trust the machine. If no dialog appears, unplug and replug the cable, or revoke USB debugging authorizations in Developer options and reconnect.
  5. Confirm ADB sees the device. Open a terminal or command prompt in the Platform Tools folder and run adb devices. You should see the device serial number followed by device. If it says unauthorized, repeat the authorization step. If it says offline, restart the ADB server with adb kill-server then adb start-server.
  6. Grant the permission. Run adb shell pm grant com.example.myapp android.permission.WRITE_SECURE_SETTINGS, replacing the package name with yours. If the command returns no output, that usually means success. If it prints an error, read the message: Unknown package means the package name is wrong or the app is not installed for that user; Permission ... is not a changeable permission type means the permission is not declared in the manifest or the build is not debuggable in the way the device expects.
  7. Verify the grant. Run adb shell dumpsys package com.example.myapp | grep WRITE_SECURE_SETTINGS on macOS or Linux, or the equivalent find command on Windows, and look for granted=true. Alternatively, open your app and call Settings.System.canWrite(context) or check the permission at runtime; a successful call to a secure settings write is the real test.
  8. Test the feature that needs it. Trigger the code path that writes a secure setting, such as changing a system-level toggle your app supports. If it throws a SecurityException, the grant did not apply to the running process; force-stop the app and reopen it.

Common Mistakes

  • Declaring the permission but forgetting to run the ADB grant. The manifest declaration alone never grants a signature-level permission.
  • Using the wrong package name, especially when the app has a debug suffix such as .debug. The grant applies to the exact package installed.
  • Running the command against the wrong user profile. On devices with multiple users or a work profile, add --user 0 or the correct user ID to the pm grant command.
  • Assuming the grant survives uninstall. Uninstalling the app removes the permission, and reinstalling requires running the grant again.
  • Using a charge-only USB cable or a USB hub that does not pass data, which makes adb devices show nothing.
  • Trying to use a third-party app from outside the official store to grant the permission. Only the ADB method and a platform-signed build are legitimate.

Tips

  • Keep a small script with the full adb shell pm grant line so you can re-run it quickly after each reinstall during development.
  • Check the permission at startup with Settings.System.canWrite() and show a clear message if it is missing, rather than letting the app crash with a SecurityException.
  • Use a debug build while developing. Debug builds are easier to reinstall and the package name is predictable.
  • If you are building a system app for a custom ROM or an enterprise-managed device, the permission can be granted through the platform signature or a device policy, but that is a different route from the ADB grant and requires control of the system image or management console.
  • Remember that WRITE_SECURE_SETTINGS is powerful: it can change settings that affect the whole device. Request it only when your app genuinely needs it, and document why.

Troubleshooting

If adb devices shows nothing, try a different cable and a different USB port, and make sure the phone's USB mode is set to file transfer rather than charging only. If the device shows as unauthorized, revoke USB debugging authorizations, reconnect and accept the prompt. If the grant command returns Unknown package, list installed packages with adb shell pm list packages | grep yourkeyword and copy the exact name. If the command returns a security exception, the device may be a production build that restricts pm grant for this permission; in that case the only supported route is a platform-signed build or a managed device configuration, not a workaround. If the grant succeeds but the app still fails, force-stop the app, reopen it, and confirm you are testing the same user profile. If the permission disappears after an update, re-run the grant; some installers replace the package entry.

Frequently Asked Questions

Can I grant WRITE_SECURE_SETTINGS without a computer?

Not through normal means. The permission is signature-level, and the supported grant path is the ADB command from a computer. Apps that claim to grant it without ADB are not using an official mechanism.

Does the grant survive a factory reset?

No. A factory reset wipes the grant along with the app data. You will need to reinstall the app and run the ADB command again.

Will this work on a release build from the Play Store?

The ADB grant can apply to a release build if the device allows it, but the grant is removed when the app is uninstalled. For distribution to other users, you cannot rely on them running ADB, so design the app to work without the permission or provide a documented setup step.

Why does the command say the permission is not changeable?

That usually means the permission is not declared in the app's manifest, or the device's build restricts granting that permission through pm grant. Check the manifest first, then confirm the device is a userdebug or eng build if you control it.

Is root required?

No, root is not required for the ADB grant on a device where USB debugging is enabled. Root is a separate mechanism and is not the recommended path.

Can I use this permission to change any setting?

It allows writing to the secure settings table, but not every key is safe or appropriate to change. Restrict your app to the specific keys it needs, and test on a spare device before shipping.