1

I just migrated to androidx via the option Refactor -> Move to Androidx option in Android Studio. By default the build.gradle file was using:

implementation 'androidx.appcompat:appcompat:1.0.0'

I am using LocalBroadcastManager in many places in the project. I could perfectly use import androidx.localbroadcastmanager.content.LocalBroadcastManager with the above v1.0.0 of the androidx.appcompat. But when I update it to 1.1.0 as:

implementation 'androidx.appcompat:appcompat:1.1.0'

The import is not working. I am getting Unresolved reference : localbroadcastmanager error.

I have tried to clean project and also rebuild project multiple times and the issue still exists.

Any solution? Thanks.

SayantanRC
  • 799
  • 7
  • 23

1 Answers1

8

AppCompat 1.0.0 had a transitive dependency on legacy-support-core-utils (which includes localbroadcastmanager so as to maintain exact compatibility with the last Support Library 28.0.0 release.

AppCompat 1.1.0 removed that transitive dependency and now only depends on the exact libraries it needs.

Therefore if your application code still needs LocalBroadcastManager, you need to manually add the dependency on LocalBroadcastManager:

implementation "androidx.localbroadcastmanager:localbroadcastmanager:1.0.0"

Note that as per the LocalBroadcastManager 1.1.0-alpha01 release notes:

androidx.localbroadcastmanager is being deprecated in version 1.1.0-alpha01.

Reason

  • LocalBroadcastManager is an application-wide event bus and embraces layer violations in your app; any component may listen to events from any other component.
  • It inherits unnecessary use-case limitations of system BroadcastManager; developers have to use Intent even though objects live in only one process and never leave it. For this same reason, it doesn’t follow feature-wise BroadcastManager.

These add up to a confusing developer experience.

Replacement

You can replace usage of LocalBroadcastManager with other implementations of the observable pattern. Depending on your use case, suitable options may be LiveData or reactive streams.

Community
  • 1
  • 1
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I see. But my app heavily uses LocalBroadcastManager. It won't be possible to switch to something else right now. – SayantanRC Nov 02 '19 at 06:12
  • 2
    There's no requirement to switch to anything else - being an unbundled library, the 1.0.0 release will continue to function as it always has. Just something to consider when building out new features that perhaps there are way better ways of doing things than `LocalBroadcastManager`. – ianhanniballake Nov 02 '19 at 14:45
  • I added this "implementation "androidx.localbroadcastmanager:localbroadcastmanager:1.0.0" but getting another error "method does not exist on module hmrclient callfunctionreturnflushedqueue". can you help me please. thanks – Alok Singh Nov 11 '22 at 05:29