简体   繁体   English

如何在保持向后兼容性的同时将 foregroundServiceType 添加到库清单?

[英]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 进行编译。看来我有两个选择:

  • Provide a separate library so that developers compiling against API 28 and lower don't get impacted;提供单独的库,以便开发人员针对 API 28 及更低版本进行编译不会受到影响;
  • warn developers targeting the new version to replace the service definition in the manifest using 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.

相关问题 在使用 Jetpack Compose 时保持向后兼容性? - Maintaining backwards compatibility while using Jetpack Compose? 如何管理 Android 11 中的文件和文件夹保持向后兼容性 - How to manage files and folder in Android 11 maintaining backwards-compatibility 如何在保持兼容性低于api 19的同时将图像uri转换为android kitkat中的文件路径? - How can I convert image uri to file path in android kitkat while maintaining compatibility lower than api 19? 如何在使用 Android API Level 15 时保持向后兼容性? - How to maintain backwards compatibility while utilising Android API Level 15? 如何在Android上添加Java库兼容性的持续集成检查? - How can I add a continuous integration check for Java library compatibility on Android? CardView:如何在保持半径的同时添加渐变背景 - CardView: How do I add a gradient background while maintaining the radius 如何测试应用程序的向后兼容性? - How to test an app for backwards compatibility? 如何在维持比例的同时在Android中缩放位图? - How can I scale a Bitmap in Android while maintaining the proportions? VerifyError,或使用overridePendingTransition,同时保持兼容性? - VerifyError, or using overridePendingTransition while maintaining compatibility? 我可以在库清单中声明抽象活动吗? - Can I declare an abstract Activity in the manifest of a library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM