95

In lib com.thingclips.smart of tuya have 1 error One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts of but I can't fix it.This is version 5.1.0 . I encountered this problem on Android 14 During runtime.Can you help me?

+I found the latest version which is 5.1.0 but it didn't solve the problem. + Tuya library also does not mention this on Android 14.

10 Answers 10

121

As discussed at Google I/O 2023, registering receivers with intention using the RECEIVER_EXPORTED / RECEIVER_NOT_EXPORTED flag was introduced as part of Android 13 and is now a requirement for apps running on Android 14 or higher (U+).

If you do not implement this, the system will throw a security exception.

To allow the broadcast receiver to receive broadcasts from other apps, register the receiver using the following code:

context.registerReceiver(broadcastReceiver, intentFilter, RECEIVER_EXPORTED);

To register a broadcast receiver that does not receive broadcasts from other apps, including system apps, register the receiver using the following code:

context.registerReceiver(broadcastReceiver, intentFilter, RECEIVER_NOT_EXPORTED);

Note: That call will need minSdkVersion to be 26 (Android 8) al least

Check https://www.delasign.com/blog/android-studio-kotlin-broadcast-recievers-export-or-not/#:~:text=As%20discussed%20at%20Google%20I,will%20throw%20a%20security%20exception.

11
  • 29
    Let me add a small addition to your answer: in order to avoid the API 26 limitation, you can use ContextCompat.registerReceiver(context, br, filter, receiverFlags). More details here: developer.android.com/guide/components/… Commented Oct 24, 2023 at 15:04
  • 2
    Note that older versions of some Android libraries may also call registerReceiver without including the mandatory flag and trigger the security exception. For example, if your app is using an older version of the Google Play Billing API, the BillingClient developer.android.com/reference/com/android/billingclient/api/… can crash your app because of this. Fix is to upgrade to a newer release of the library.
    – gb96
    Commented Nov 30, 2023 at 10:59
  • 7
    how to resolve this in a react native android project Commented Dec 21, 2023 at 11:10
  • 5
    RECEIVER_EXPORTED and RECEIVER_NOT_EXPORTED require API level 33
    – algrid
    Commented Jan 19 at 11:43
  • 3
    Where does this get added into the app? I am racking my brain and I can;t figure out where to put it without getting errors Commented Jun 12 at 12:48
44

You can also add SDK Version Check

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
     registerReceiver(broadcastReceiver, intentFilter, RECEIVER_EXPORTED)
}else {
     registerReceiver(broadcastReceiver, intentFilter)
}
3
  • 8
    Please, add @Suppress("UnspecifiedRegisterReceiverFlag") to else branch.
    – CoolMind
    Commented Feb 1 at 7:17
  • 1
    Pls can someone tell me which file should i edit in a Flutter project?
    – Mbebwo
    Commented Mar 22 at 5:53
  • 1
    For React Native in which file i need to add this change? Commented Aug 8 at 15:07
33

You can register a broadcast receiver without branching for Android versions using ContextCompat:

ContextCompat.registerReceiver(context, broadcastReceiver, intentFilter, ContextCompat.RECEIVER_EXPORTED)

or

ContextCompat.registerReceiver(context, broadcastReceiver, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED)
2
  • 2
    Interesting. I didn't know that. So far, I have been doing registerReceiver(broadcastReceiver, intentFilter, RECEIVER_EXPORTED) without branching for Android versions and without using ContextCompat. And it works without incident. What am I missing?
    – ususer
    Commented Jul 25 at 13:20
  • 1
    for react native in which i need to add this change Commented Aug 8 at 16:03
12

I got this error when using Android Billing. It was solved by changing this:

implementation 'com.android.billingclient:billing:6.0.0'

...to this:

implementation 'com.android.billingclient:billing:6.1.0'
0
3

If anybody needs this for Android .NET (formerly known as Xamarin).

use it like this:

RegisterReceiver(broadcastReceiver, intentFilter, ReceiverFlags.Exported);
1
  • For people who are still using Xamarin forms can use "Context.RegisterReceiver(receiver, filter, (ActivityFlags)ReceiverFlags.Exported);" Reference : github.com/dotnet/android/issues/7503
    – Hasitha
    Commented Sep 6 at 11:58
2

React Native 0.60.5

https://medium.com/@md.Azeem/reac-native-android-app-crash-after-upgrade-targetsdkversion-34-e521f0dd0e19

This method work for me without upgrade react native

But those changes in MainApplication.java should be in MainActivity.java for Xiaomi, I follow code in above link, only Samsung work

path:projectRoot/android/app/src/main/java/com/appName/MainActivity.java

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import org.jetbrains.annotations.Nullable;
import android.content.Context;

 @Override
    public Intent registerReceiver(@Nullable BroadcastReceiver receiver, IntentFilter filter) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            return super.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED);
        } else {
            return super.registerReceiver(receiver, filter);
        }
    }
1
  • 1
    This worked for me on a capacitor app, just add the code in the MainActivity
    – Daniel S.
    Commented Sep 11 at 0:25
0

If you are using App update feature or core dependency then update the dependencies as follows.It will work even your targetSdkVersion is 34.

implementation 'com.google.android.play:app-update:2.1.0'
implementation 'com.google.android.play:app-update-ktx:2.1.0'
0

You can try with this code if using intenFilter.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                Objects.requireNonNull(getContext()).registerReceiver(syncReceiver, intentFilter, Context.RECEIVER_NOT_EXPORTED);
            }else{
                Objects.requireNonNull(getContext()).registerReceiver(syncReceiver, intentFilter);
            }
-1

Add ContextCompat.registerReceiver( this, mMessageReceiver, filter, ContextCompat.RECEIVER_EXPORTED )

It works for me

3
  • 1
    where do I add this? In my MainActivity? Commented May 6 at 16:20
  • @winfredadrah add in the OnResume MainActivity Commented May 8 at 3:33
  • 1
    Add in OnCreate or it will be added every single time the screen comes back to the front Commented Jun 12 at 20:08
-9

I have added a Try Catch block to the lines displayed in the error logs, and it is now operational.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.