SyncAdapter的Android库未在同一部手机中的多个应用中运行

时间:2017-10-25 09:35:49

标签: android android-contentprovider android-syncadapter

我正在创建一个 Android库,它使用SyncAdapter每天在后台执行一项小任务。如果只在一个应用程序中使用,这种方法非常好。如果同一部手机中的两个应用使用相同的库,则会出现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文件夹中。

2 个答案:

答案 0 :(得分:0)

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

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

然后使用以下xml作为syncAdapter

<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"/>

答案 1 :(得分:0)

@pranav-agraval的答案有效。

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

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