简体   繁体   中英

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

I am creating an Android library that uses SyncAdapter to do a small task in the background every day. This works perfectly fine if used in only one app. If the same library is used by two apps in the same phone, it gives CONFLICTING_PROVIDER error.

To fix this. I used ${applicationId} in library manifest like:

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

This generates dynamic authority for the provider.

Now, the problem is that SyncAdapter needs to have contentAuthority in the xml folder.

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

I tried android:contentAuthority="${applicationId}.mysdk" but applicationId doesn't get replaced by the app's package name. Instead, it take it as it is and gives error as:

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

These are the step I have tried till now

  1. Creating stringValue from build.gradle in library's build.gradle file, but here applicationId can not be used there.
  2. Moved contentAuthority in the Manifest, as ${applicationId} works there but nothing happens after doing this. contentAuthority needs to be in the xml folder.

First you need to declare the application id in your build.gradle file

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

Then use the following xml for your 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"/>

@pranav-agraval 's answer worked.

I didn't change anything in the SDK. The string resource I was using had a key content_provider .

I would have to ask any one who is using the SDK to add this key to their string.xml file. This should be of pattern application_package_name.sdk_name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM