What is sharedUserId in Android, and how is it used?

60,317

Solution 1

SharedUserId is used to share the data,processes etc between two or more applications. It is defined in AndroidManifest.xml like,

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:sharedUserId="android.uid.shared"
    android:sharedUserLabel="@string/sharedUserLabel"
    ...>

and define the shared parameter in Android.mk for that app, like

LOCAL_CERTIFICATE := shared

Hope its helpful to you.

Solution 2

By default, Android assigns a user id to an application. It is the unique id for your application and means that nobody except the user with this id can reach your application's resources. You cannot access the data of an other application or run it in your current process. when, from an activity, an activity of another application is called android passes the control to the new activity called and they run in totally different processes.

However, in your manifest file, you can explicitly identify a user id for your application. When you declare the same user id for more than one application, they can reach each other's resources (data fields, views, etc.). You can display data from another application or run it in your process.

this is how you use it: from http://developer.android.com/guide/topics/manifest/manifest-element.html

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="string"
    android:sharedUserId="string"
    android:sharedUserLabel="string resource" 
    android:versionCode="integer"
    android:versionName="string"
    android:installLocation=["auto" | "internalOnly" | "preferExternal"] >
    . . .</manifest>
Share:
60,317
Bhargav Panchal
Author by

Bhargav Panchal

Updated on March 09, 2020

Comments

  • Bhargav Panchal
    Bhargav Panchal over 4 years

    I am confused in sharedUserID.what is use of sharedUserId?How to use?Where to use in android?

  • njzk2
    njzk2 over 12 years
    an important addition is that you can only install two applications with the same shareduserid is both the applications were signed with the same certificate.
  • Piyush Agarwal
    Piyush Agarwal almost 11 years
    How many applications can have same sharedUserId in a device? If I am having five applications with same userId in a device will it affect the applications performance as they are running in same process ?
  • IgorGanapolsky
    IgorGanapolsky over 10 years
    How do you modify Android.mk? I haven't seen any references to it informing us on what that file is all about...
  • Lo-Tan
    Lo-Tan about 10 years
    @pyus13 I don't believe they are forced to run in the same process. That is just an option if you desire to do so.
  • Piyush Agarwal
    Piyush Agarwal about 10 years
    I have shared user id in many of my apps to share private preferences. The surprising thing is they works fine if I have targetSDK version 9 but if i have targetSK version 14 it stopped working.
  • Hartok
    Hartok over 9 years
    @IgorGanapolsky Android.mk is a makefile for Android NDK (C/C++). If your app uses Java only, you don't have one.
  • Hartok
    Hartok over 9 years
    Works fine for me (target API 21) with at least 3 apps sharing the same UserId.
  • RocketRandom
    RocketRandom about 8 years
    That is incorrect. This file is required for ALL applications which are to be built as part of AOSP. Including java only ones.
  • The Dude
    The Dude about 7 years
    No its not necessary to set the mk file
  • Prudhvi
    Prudhvi over 4 years
    From Android documentation, "Shared user IDs cause non-deterministic behavior within the package manager. As such, its use is strongly discouraged and may be removed in a future version of Android. Instead, apps should use proper communication mechanisms, such as services and content providers, to facilitate interoperability between shared components."