Android 具有 SyncAdapter 的库未 运行 在同一 phone 的超过 1 个应用程序中

Android library with SyncAdapter not running in more than 1 app in the same phone

我正在创建一个 Android 库,它使用 SyncAdapter 每天在后台执行一个小任务。如果仅在一个应用程序中使用,则效果非常好。如果同一个 phone 中的两个应用程序使用同一个库,则会出现 CONFLICTING_PROVIDER 错误。

解决这个问题。我在库清单中使用了 ${applicationId},例如:

<provider
    android:name=".sync.DummyProvider"
    android:authorities="${applicationId}.mysdk"
    android:exported="false"
    android:syncable="true" />

这会为提供者生成动态权限。

现在,问题是 SyncAdapter 需要在 xml 文件夹中有 contentAuthority

<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/sync_account_type"
    android:allowParallelSyncs="false"
    android:contentAuthority="<-----NOT SURE WHAT TO ADD HERE----->"
    android:isAlwaysSyncable="true"
    android:supportsUploading="false"
    android:userVisible="false"/>

我试过 android:contentAuthority="${applicationId}.mysdk" 但 applicationId 没有被应用程序包名称替换。相反,它照原样给出错误:

E/ActivityThread: Failed to find provider info for ${applicationId}.mysdk

这些是我到目前为止尝试过的步骤

  1. 正在从库的 build.gradle 文件中的 build.gradle 创建 stringValue,但这里的 applicationId 不能在那里使用。
  2. 在 Manifest 中移动了 contentAuthority,因为 ${applicationId} 在那里工作,但在执行此操作后没有任何反应。 contentAuthority 需要位于 xml 文件夹中。

首先您需要在 build.gradle 文件中声明应用程序 ID

 defaultConfig {
    applicationId "com.my.package"
    ....
    }

然后为您的 syncAdapter 使用以下 xml

<sync-adapter
xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="@string/account_authorities"
android:accountType="@string/account_type"
android:userVisible="false"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="true"/>

@pranav-agraval 的回答有效。

我没有更改 SDK 中的任何内容。我使用的字符串资源有一个键 content_provider.

我不得不请任何使用 SDK 的人将此密钥添加到他们的 string.xml 文件中。这应该是模式 application_package_name.sdk_name.