javaandroidandroid-sqliteandroid-contentproviderandroid-contentresolver

Accessing SQLite DB in multiuser app in Android


I've a requirement to access database created by a system user from other service via a foreground user. Both services are part of the same application.

Created a system service, which creates a database and exposes it via ContentProvider. In another application, foreground user tries to access it via ContentResolver.

Added permissions

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />

manifest declarations

<service
android:name=".EXDBService"
android:enabled="true"
android:singleUser="true"
android:exported="true" />

<service
android:name=".EXService"
android:enabled="true"
android:exported="true"
android:permission="com.example.EX_SERVICE" />
<provider
android:name=".EXDBMananger"
android:authorities="com.example.provider"
android:exported="true"
android:readPermission="com.example.provider.READ_DATABASE"
android:writePermission="com.example.provider.WRITE_DATABASE" />

But when making the request like insert/update with Contentresolver, it is throwing an error couldn't open database.

ContentResolver resolver = getContentResolver();
Uri uri = DbContract.BASE_CONTENT_URI;
// initiate contentValues with valid data
Cursor cursor = resolver.insert(uri, contentValues);

Wish I could share more code here, but any guesses for what this could cause this issue?

I saw the user id 11 trying to access database on other application which is running on user 0. Does this cause any permission issue?

2023-04-20 21:01:48.745 8530-8530/com.example.xxxxx E/SQLiteDatabase: Failed to open database '/data/data/com.example.xxxxx/databases/My.DB'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: Cannot open database '/data/data/com.example.xxxxx/databases/MY.DB': Directory /data/data/com.example.xxxxx/databases doesn't exist
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:254)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:205)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:505)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:198)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:919)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:899)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:763)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:752)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:383)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:340)

Solution

  • I had the issue with manifest tag sharedId, removing which resolved the issue. Also here's how the application looks like after the problem is solved. Hopefully this helps.

    enter image description here