Search code examples
javaandroidandroid-contentproviderandroid-libraryauthority

Android - Having Provider authority in the app project


An android library project contains a few providers whose authority is defined like the following in a contract class :

public static final String CONTENT_AUTHORITY = "my.com.library.providers.tester";
private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

Now there are a lot of app projects which uses this library project. The problem I am currently having is that for every app project, I need to have a separate branch in the library project for every app just for having a unique content authority. This is creating some version management problems (like propagating features/bug fixes from one branch to every other branch etc.,). Instead I would like to delegate the responsibility of defining the content authority to the app project. Is there a way to accomplish this?


Solution

  • The app is the only one that absolutely needs to know about the authority, as it is the one that declares the <provider> in the manifest with the android:authorities attribute.

    Hence, in principle, it should "just work", so long as you remove all authority-specific logic from the provider, such as:

    • those static data members (which now move to the hosting app)
    • UriMatcher (roll something yourself that does not examine the authority, but focuses on the rest of the Uri)

    If, for some reason, you are absolutely sure that your provider needs to know its authority, then the app will have to supply that to the provider before the provider is used for real work. Possible ways to do that include:

    • Since a ContentProvider is a natural singleton, assign it to a static data member, and then supply the authority string to it by a custom method from a custom Application class (as providers are initialized first, so this should work)

    • If you are only supporting API Level 11+, have the custom Application class use call() on ContentResolver to supply the authority to the ContentProvider

    • Assume that the only real calls (e.g., to query(), insert()) are valid, and just lazy-initialize your authority based on what comes in on the first Uri you see