Skip to content
DeveloperMemos

Fixing Broadcast Receiver Error in Android 14

Android 14, Broadcast Receiver, Security1 min read

Android 14 introduces stricter security measures for broadcast receivers. You might encounter the error "One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts" if your app registers a broadcast receiver without specifying its access.

Here's how to fix it:

Understanding the Issue:

  • Broadcast receivers are components that listen for specific events in the Android system.
  • Android 14 (and above) requires developers to explicitly declare whether a receiver is accessible by other apps (RECEIVER_EXPORTED) or only by your app (RECEIVER_NOT_EXPORTED).

Fixing the Error:

  1. Update your targetSdkVersion:

    • The easiest solution is to update your app's targetSdkVersion in the build.gradle file to at least 34 (Android 14). This ensures your app adheres to the latest security standards(but this is usually what causes this error in the first place).
  2. Specify Receiver Access with ContextCompat.registerReceiver:

    • ContextCompat.registerReceiver is a convenient way to register broadcast receivers within your code. It provides an additional parameter for specifying the receiver flags:
    1MyBroadcastReceiver receiver = new MyBroadcastReceiver();
    2IntentFilter filter = new IntentFilter("com.example.custom.broadcast");
    3ContextCompat.registerReceiver(context, receiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED);

    In this example, ContextCompat.RECEIVER_NOT_EXPORTED clarifies that the receiver is only intended for internal use within your app.

Using ContextCompat.registerReceiver offers these benefits:

  • Clarity: It explicitly defines the receiver's access scope, improving code readability and maintainability.
  • Compatibility: It automatically handles the flag requirement based on the targeted Android version, ensuring backward compatibility.

An Additional Tip

  • If you're using third-party libraries, ensure they are updated to support Android 14's requirements. Outdated libraries might cause the error, I specifically got this error when the Android Billing Library version I was using was out of date.

By following these steps, you should be able to resolve the "RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED" error and ensure your app functions smoothly on Android 14 and beyond.