将应用程序兼容性更新到 1.4.1 后 ProcessLifecycleOwner 无法工作
我正在调查一个奇怪的问题,我们将 androidx.appcompat:appcompat
从 1.3.1
更改为 1.4.1
并且所有突然我们的 LifecycleObservers
观察进程生命周期停止发出任何事件。
我也在使用 "androidx.lifecycle:lifecycle-extensions:2.2.0"
,我知道这已被弃用,但如果 appcompat 为 1.3.1
则它可以完美工作>
我已在清单中设置了正确的应用程序名称,我已根据文档的要求包含此提供程序。
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
初始化的示例,不起作用。该对象被注入到 Application
类中,并且添加了生命周期观察器,但 onStart
和 onStop
从未被调用。
class AppLifecycle @Inject constructor(
private val foregroundProxy: AppLifecycleProxy
) : LifecycleObserver {
init {
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
foregroundProxy.onStarted()
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
foregroundProxy.onStopped()
}
}
编辑: 根据 @EpicPandaForce 评论,将清单中的提供程序块更改为:
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities=\"${applicationId}.androidx-startup"
android:exported="false"
tools:node=\"merge">
<!-- If you are using androidx.startup to initialize other components -->
<meta-data
android:name="androidx.lifecycle.ProcessLifecycleInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
并将 "androidx.lifecycle:lifecycle-extensions:2.2.0"
依赖项替换为 "androidx.lifecycle:lifecycle- common:2.4.1"
已修复此问题
I'm investigating a weird issue, where we changed the androidx.appcompat:appcompat
from 1.3.1
to 1.4.1
and all of a sudden our LifecycleObservers
observing process lifecycle stopped emitting any events.
I'm also using "androidx.lifecycle:lifecycle-extensions:2.2.0"
, I know that this is already deprecated, but it works flawlessly if appcompat is 1.3.1
I have set the correct application name in the Manifest, I have included this provider as required per docs.
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
Example of initialisation, which doesn't work. This object is injected in the Application
class and lifecycle observer is getting added, but onStart
and onStop
are never called.
class AppLifecycle @Inject constructor(
private val foregroundProxy: AppLifecycleProxy
) : LifecycleObserver {
init {
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
foregroundProxy.onStarted()
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
foregroundProxy.onStopped()
}
}
EDIT:
As per @EpicPandaForce comment, changing the the provider block in Manifest to:
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities=\"${applicationId}.androidx-startup"
android:exported="false"
tools:node=\"merge">
<!-- If you are using androidx.startup to initialize other components -->
<meta-data
android:name="androidx.lifecycle.ProcessLifecycleInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
And replacing the "androidx.lifecycle:lifecycle-extensions:2.2.0"
dependency with "androidx.lifecycle:lifecycle-common:2.4.1"
has fixed this problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
很有可能,您这边发生更改的原因是因为 ProcessLifecycleOwner 的初始化被重新设计为使用 Jetpack Startup 库,该库依赖于 ContentProvider,因此仅在进程的主进程中进行初始化。
在 ProcessLifecycleOwner 的实际代码中,它表示:
在 2021 年 3 月使用
androidx.startup
的提交内容如下:因此,您专门添加的代码片段会禁用 AndroidX StartUp 进程,因此您的 ProcessLifecycleOwner 将不会被初始化。
顺便说一句,如果您禁用自动启动过程,Google 并没有提供手动安装 ProcessLifecycleOwner 的方法,但要实现它,您只需要模仿他们正在做的事情即可。例如,如果您需要在多进程应用程序中使用 ProcessLifecycleOwner,则不需要使用 ContentProvider。
在这种情况下,您可以在名为
androidx/lifecycle
的包中创建一个 Java 文件:但在您的位置,您可能只需删除删除您的 ContentProvider 的代码片段。
There's a good bet that the reason there has been changes on your side is because ProcessLifecycleOwner's initialization was reworked to use the Jetpack Startup library, which relies on a ContentProvider, therefore only does initialization in a process's main process.
In the actual code for ProcessLifecycleOwner, it says:
And the commit that made it use
androidx.startup
in 2021-03 says this:So the snippet you added specifically disables the AndroidX StartUp process and therefore your ProcessLifecycleOwner won't get initialized.
By the way, Google did NOT provide a way to manually install the ProcessLifecycleOwner if you disable the automatic start-up process, but to achieve it, you just need to mimic what they are doing. For example, if you needed to use ProcessLifecycleOwner in a multi-process app, then you'd need to not use the ContentProvider.
In that case, you can create a Java file in a package called
androidx/lifecycle
:But in your place, you probably just have to remove the snippet that removes your ContentProvider.
使用
androidx.startup
进行初始化时,androidx.lifecycle
开始了升级。因此,如果此代码位于您的清单中:
那么您可以完全删除该代码,或使用
tools:node="merge"
而不是remove
。您需要在合并清单中验证
androidx.lifecycle.ProcessLifecycleInitializer
是否正在初始化。There was an upgrade were
androidx.lifecycle
started usingandroidx.startup
to initialize.So, if this code was in your manifest:
Then you can completely remove that code, or use
tools:node="merge"
instead ofremove
.You need to verify in the Merged Manifest that
androidx.lifecycle.ProcessLifecycleInitializer
is being initialized.如果您使用生命周期库,请添加此
if you are using lifecyle library, add this
我的解决方法可能并不适合所有人。但这是基本的想法。请注意,我的应用程序正在使用
WorkManager
。步骤 1:
更改
为
步骤 2:
更改
为
My workaround might not work for everyone. But here's the basic idea. Note, my app is using
WorkManager
.Step 1:
Change
to
Step 2:
Change
to
当我禁用数据绑定时,我遇到了这个问题。
添加这个就解决了
I got this problem when I disabled databinding.
Adding this solved it