16

I am making an app and I am using Android Room Persistence Library to handle my database layer. Room Library works like charm and everything is fine with it. But I want the database that room creates to be removed when the user uninstall the app. I tried uninstalling the app and then installed again, but somehow the database was still there and app was able to get the old data from it.

I thought maybe because my app data backup is set to auto in the settings and android is backing it up on cloud and bringing it back again but turning off backup from settings didn't help. Even if that worked that doesn't sound like a good solution to me.

I have created a very simple class that extends RoomDatabase and below is the code if that helps answering the question.

I know that I can use fallbackToDestructiveMigration() on the database builder and increase the database version. It will clear the data from the database. That is not what I want.

@Database(entities = {UnleashedEntity.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public abstract DaoContract MyDao();

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")
                            .build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

Edit I know I can use ACTION_PACKAGE_REMOVED intent to make my app aware of the uninstall. What I wanted to know, Is there a configuration of the databasebuilder that does the job, and How come the database persists after app uninstall.

Sam
  • 2,935
  • 1
  • 19
  • 26
  • Possible duplicate of [Is it possible to detect Android app uninstall?](https://stackoverflow.com/questions/6209730/is-it-possible-to-detect-android-app-uninstall) – creativecreatorormaybenot Apr 05 '18 at 18:14
  • 1
    @creativecreatorormaybenot that doesn't answer my question of deleting the room database. – Sam Apr 06 '18 at 09:45

2 Answers2

19

Is there a configuration of the databasebuilder that does the job

AFAIK room does not know about app getting uninstall so Room probably won't wipe the database for you on uninstall.

How come the database persists after app uninstall

Beginning with Android 6.0 (API level 23), Android offers the Auto Backup feature which is enabled by default. According to the documentations, it backup the following to google drive:

  • Shared preferences files.
  • Files saved to your app's internal storage, accessed by getFilesDir() or getDir(String, int).
  • Files in the directory returned by getDatabasePath(String), which also includes files created with the SQLiteOpenHelper class.
  • Files on external storage in the directory returned by getExternalFilesDir(String).

This backup your room database too.

How to disable auto backup

In your manifest:

<manifest ... >
    ...
    <application android:allowBackup="false" ... >
        ...
    </application>
</manifest>
Weizhi
  • 1,027
  • 8
  • 22
  • Shouldn't turning backup off from settings have the same effect ? – Sam Apr 09 '18 at 07:41
  • @Sam I have not tested this but from the documentation https://developer.android.com/guide/topics/data/autobackup.html#BackupSchedule it seems like turning off backup will only prevent new backup from being upload to google drive but does not prevent app from downloading and restoring existing backup from google drive. It might be possible that your app already has a backup existing in google drive. – Weizhi Apr 09 '18 at 07:53
  • 1
    Alright makes sense. And how can the backup from google drive be removed only for a specific app ? – Sam Apr 09 '18 at 08:53
  • @Sam "how can the backup from google drive be removed only for a specific app" You should create a separate question for this. – Weizhi Apr 09 '18 at 09:10
0
  1. Make your App Uninstall aware like described in this answer from AnniJais.
  2. Make a method in the Dao with the @Query Annotation that deletes the Database.
@Query("Drop Database myDatabaseName")
public void nukeDatabase();
  1. Call your Dao method in the part previously added:

if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) { // User has clicked on the Uninstall button under the Manage Apps settings

Step 2 is not really testet of me.

Bytehawks
  • 271
  • 4
  • 11
  • 1
    Can you elaborate your second step ? AFAIK `@Delete` is used to delete data from the tables, not the database itself. – Sam Apr 06 '18 at 09:39