Share SQLite database between 2 android apps?

I need to share a single database between 2 apps. I know that the database will be created on /data/data/MY_PACKAGE/databases/ . Since the packages names are different is it possible to define the path to one package name when I create the database on either app?
Thanks.

  • Adding widgets to a launcher page without bindAppWidgetId()
  • AlarmManager does not always execute BroadcastReceiver
  • Camera preview freezes after screen lock
  • How can I adb install an apk to multiple connected devices?
  • ScrollView not scrolling to the end of inner LinearLayout's bottom margin
  • DefaultHttpClient to AndroidHttpClient
  • Related posts:

    How to show only message from log (hide time ,pid etc.) in Android Studio
    Sticky immersive mode disabled after soft keyboard shown
    Android WebView focus problem
    Android - Camera preview is sideways
    Credit card for Google Play test
    Populate list of custom view using ListFragment
  • How to implement search feature with SearchView, Retrofit and RxJava (RxBindings)?
  • How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)
  • How to show enable location dialog like Google maps?
  • Animate view added on WindowManager
  • Android encryption
  • How to use Holo.Light theme, and fall back to 'Light' on pre-honeycomb devices?
  • 3 Solutions collect form web for “Share SQLite database between 2 android apps?”

    You certainly can share a single database between 2 apps.

    In order to share data between apps (provided they are issued by the same publisher) you will need to specify a shared user id in the AndroidManifest.xml of both apps.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:sharedUserId="my.app" ... >
    

    (It’s undocumented, but the shared user id need to be a string with at least one dot separator)

    The rest is easy, and you don’t need to mess around with the database path. Just use the same DBAdapter in both apps. In the app that hosts the database, call the DBAdapter with the native context.

    DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
    performerDBadapter.open();
    

    In the second app, access the database with the context of the database hosting app.
    First, define the shared context:

    Context sharedContext = null;
        try {
            sharedContext = this.createPackageContext("replace.with.host.package.name", Context.CONTEXT_INCLUDE_CODE);
            if (sharedContext == null) {
                return;
            }
        } catch (Exception e) {
            String error = e.getMessage(); 
            return;
            }   
    

    Then open the DBAdapter with the shared context:

    DbAdapter sharedDBadapter = new PerformerDbAdapter(sharedContext);
    sharedDBadapter.open();
    

    As a final note, if your database exists previous to setting the shared user id in the manifest, you will need to uninstall/reinstall the apps on a physical device, lest you will lock yourself out of your database (sqlite error 14). The emulator, on the other hand, might prove to be more forgiving. Bottom line, if your apps are published on the Android market, setting a shared user id in an afterthought will not work.

    Hope this helps.

    The database path is private for each application and as far as i know it’s not possible to access it directly across applications.

    However one approach is that one application makes it’s database accessible to the other one using a ContentProvider. Check out if that works for you.

    Content providers store and retrieve data and make it accessible to
    all applications. They’re the only way to share data across
    applications; there’s no common storage area that all Android packages
    can access.

    As long as you use the same certificate on both applications, your applications will run on same process and behave as being same application
    check this section of android documentation
    http://developer.android.com/tools/publishing/app-signing.html#strategies

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.