What is sharedUserId in Android, and how is it used?
Asked Answered
H

2

73

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

Himelman answered 20/3, 2012 at 8:52 Comment(1)
sharedUserID flag is deprecated in API level 29 developer.android.com/guide/topics/manifest/…Precritical
C
39

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.

Counterinsurgency answered 20/3, 2012 at 9:40 Comment(5)
How do you modify Android.mk? I haven't seen any references to it informing us on what that file is all about...Rodrich
@Rodrich Android.mk is a makefile for Android NDK (C/C++). If your app uses Java only, you don't have one.Derogate
That is incorrect. This file is required for ALL applications which are to be built as part of AOSP. Including java only ones.Wehrle
No its not necessary to set the mk fileElaboration
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."Ciborium
E
46

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>
Ephraimite answered 20/3, 2012 at 9:10 Comment(6)
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.Tyndall
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 ?Objectivity
@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.Grishilda
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.Objectivity
Works fine for me (target API 21) with at least 3 apps sharing the same UserId.Derogate
Note that sharedUserId is deprecated so you should use other alternatives instead: developer.android.com/guide/topics/manifest/…Brachiate
C
39

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.

Counterinsurgency answered 20/3, 2012 at 9:40 Comment(5)
How do you modify Android.mk? I haven't seen any references to it informing us on what that file is all about...Rodrich
@Rodrich Android.mk is a makefile for Android NDK (C/C++). If your app uses Java only, you don't have one.Derogate
That is incorrect. This file is required for ALL applications which are to be built as part of AOSP. Including java only ones.Wehrle
No its not necessary to set the mk fileElaboration
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."Ciborium

© 2022 - 2024 — McMap. All rights reserved.