[英]How can I add foregroundServiceType to a library manifest while maintaining backwards compatibility?
My library project has a location service, and per Android Q requirements it sets the android:foregroundServiceType="location"
attribute in the manifest.我的库项目有一个位置服务,并且根据 Android Q 要求,它在清单中设置了
android:foregroundServiceType="location"
属性。 When an app module uses my library and compiles against API level 28, it fails with the following error:当应用模块使用我的库并针对 API 级别 28 进行编译时,它会失败并显示以下错误:
AndroidManifest.xml:57: AAPT: error: attribute android:foregroundServiceType not found.
How can my library maintain compatibility with older versions, while making sure the functionality works on Android Q?我的库如何保持与旧版本的兼容性,同时确保该功能适用于 Android Q?
I had same error and after migrating to Androidx
and updating compileSdkVersion
from 28 to 29 my issue was resolved.我遇到了同样的错误,在迁移到
Androidx
并将compileSdkVersion
从 28 更新到 29 后,我的问题得到了解决。 So please do these changes and you can get your solution所以请做这些改变,你就可以得到你的解决方案
My goal is to avoid breaking anyone's build when the library version is updated, and at the same time avoid forcing the developer to compile against API 29. It seems I have two choices:我的目标是避免在更新库版本时破坏任何人的构建,同时避免强迫开发人员针对 API 29 进行编译。看来我有两个选择:
tools:node="replace"
.tools:node="replace"
清单中的服务定义。 The problem with the first approach is that I will need to maintain two libraries for some time.第一种方法的问题是我需要在一段时间内维护两个库。 The problem with the second is that developers must remember to revert the change once they update the SDK version.
第二个问题是开发人员必须记住在更新 SDK 版本后恢复更改。
In my case, I will go with the second approach.就我而言,我将采用第二种方法。 By passing an explicit foreground service type to the
startForeground
method when targeting Android Q, I can cause a crash if the type is not set in the manifest.通过在面向 Android Q 时将显式前台服务类型传递给
startForeground
方法,如果未在清单中设置该类型,我可能会导致崩溃。 The developer can therefore catch this when targeting Android Q and revert the manifest change to fix it.因此,开发人员可以在定位 Android Q 时捕获此问题并恢复清单更改以修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.