ContentProvider in Android library project

Is it possible to have my own (exported = false) ContentProvider for a library project that is used by many different applications?

The problem is that even when the CP is not exported, it must have unique privileges. If it is not unique, you cannot install multiple applications with the same library on the same phone (INSTALL_FAILED_CONFLICTING_PROVIDER).

I know that I can use the application identifier to determine the provider in AndroidManifest as follows:

<provider android:authorities="${applicationId}.provider.test" android:name=".storage.db.MyContentProvider" android:exported="false" /> 

but I can’t find a solution for generating permissions in the code at runtime to properly initialize the UriMatcher.

BuildConfig.APPLICATION_ID returns the ID of the library project, not the application. I could try to extract the package from the application context, but then this is not the best solution if the application uses flavors with different applications.

So my ideas for solving this problem are:

  • finding the correct appplicationId in my library code at runtime (also when using flavors with different application identifiers)
  • find a way to properly match URIs in my UriMatcher without the knowledge of authority.
+5
source share
2 answers

I was able to get runtime credentials based on the answer found here . The solution is as follows (API 9 +)

 private static String getAuthority(final Context appContext) throws PackageManager.NameNotFoundException { final ComponentName componentName = new ComponentName(appContext, MyContentProvider.class.getName()); final ProviderInfo providerInfo = appContext.getPackageManager().getProviderInfo(componentName, 0); return providerInfo.authority; } 
+4
source

Unfortunately, the only way is to ask application developers who used your library to add a tag to their AndroidManifest.xml with their unique authority.

0
source

Source: https://habr.com/ru/post/1265372/


All Articles