22

Can two app with shared UserID access each other resources like drawables or strings?

Can they access each other assets?

Can they enable or disable components of the other?

If any of these is possible please explain how it must be done.

I searched a lot but couldn't find any example about userId sharing.

Ali
  • 21,572
  • 15
  • 83
  • 95
  • Duplicate of http://stackoverflow.com/questions/9783765/what-is-shareduserid-in-androidhow-can-i-used-shareduserid – bleater Nov 04 '13 at 23:42

1 Answers1

50

You can use android:sharedUserId in AndroidManifest.xml to let your application share the same user id with another application.

android:sharedUserId

The name of a Linux user ID that will be shared with other applications. By default, Android assigns each application its own unique user ID. However, if this attribute is set to the same value for two or more applications, they will all share the same ID — provided that they are also signed by the same certificate. Application with the same user ID can access each other's data and, if desired, run in the same process.

Notice that they need to be signed by the same certificate.

Two applications share the same user id may access each other's resource.

For example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shareusertesta"
    android:versionCode="1"
    android:versionName="1.0" 
    android:sharedUserId="com.example">

Then we can init a new context of com.example by:

Context friendContext = this.createPackageContext( "com.example",Context.CONTEXT_IGNORE_SECURITY);

And access some resources of that application:

friendContext.getResources().getString(id);
friendContext.getResources().getDrawable(id);
friendContext.registerReceiver(...);
StarPinkER
  • 14,081
  • 7
  • 55
  • 81
  • I get `android.content.pm.PackageManager$NameNotFoundException: Application package com.example not found`. Did you mean full package name? – coyer Nov 28 '16 at 09:51
  • 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." – Prudhvi Dec 23 '19 at 18:31