问题描述
我刚刚通过选项 Refactor->迁移到androidx;在Android Studio中移至Androidx
选项.默认情况下, build.gradle
文件使用的是:
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'
我在项目的许多地方都使用LocalBroadcastManager.我可以完美地将 import androidx.localbroadcastmanager.content.LocalBroadcastManager
与上述 androidx.appcompat
的v1.0.0结合使用.但是当我将其更新为1.1.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'
导入无法正常进行.我收到 Unresolved reference:localbroadcastmanager
错误.
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.
有解决方案吗?谢谢.
推荐答案
AppCompat 1.0.0对 legacy-support-core-utils
(其中包括 localbroadcastmanager
>,以便与最新的Support Library 28.0.0版本保持完全兼容.
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删除了该传递依赖项,现在仅依赖于所需的确切库.
AppCompat 1.1.0 removed that transitive dependency and now only depends on the exact libraries it needs.
因此,如果您的应用程序代码仍然需要 LocalBroadcastManager
,则需要手动添加对LocalBroadcastManager的依赖项:
Therefore if your application code still needs LocalBroadcastManager
, you need to manually add the dependency on LocalBroadcastManager:
implementation "androidx.localbroadcastmanager:localbroadcastmanager:1.0.0"
请注意,按照 LocalBroadcastManager 1.1.0-alpha01发行说明:
原因
-
LocalBroadcastManager
是应用程序范围的事件总线,在您的应用程序中包含违反层的行为;任何组件都可以侦听来自任何其他组件的事件. - 它继承了系统
BroadcastManager
的不必要的用例限制;开发人员必须使用Intent
,即使对象只存在于一个进程中,也永远不会离开它.出于同样的原因,它不遵循按功能分类的BroadcastManager
.
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 useIntent
even though objects live in only one process and never leave it. For this same reason, it doesn’t follow feature-wiseBroadcastManager
.
这些加在一起使开发人员感到困惑.
These add up to a confusing developer experience.
替换
您可以将 LocalBroadcastManager
的用法替换为可观察模式的其他实现.根据您的使用情况,合适的选项可能是 LiveData
或响应流.
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.
这篇关于在androidx.appcompat:appcompat:1.1.0中找不到LocalBroadcastManager,但在1.0.0中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!