One Android application's activity running in

2019-07-21 13:30发布

问题:

Big picture

I have two Android application, Host and Guest. I am trying to run an activity from Guest such that it immediately opens an activity in Host. The intention is that launching the Guest app should run in the Host, but appear to be running as the Guest (i.e. the user should, to all intents and purposes, not know of the host's existence despite it doing the heavy lifting for the guest).

This question is about getting that Host activity to run in the process belonging to Host.

(why I want to do this is probably out of scope for this question, but it's definitely something I want to do).

What I have tried before

To start with, it was thought that process separation from the rest of the Host activities was enough.

To do this, the process was named in the <activity> element in the manifest.

<activity android:name=".MainHostActivity" android:process="host.process" />

For two reasons, this proved to be problematic:

  • it kind've worked, but it gets very hacky and messy in the face of multiple guests.
  • the standard Android tools to enable the user to manage the apps don't work (e.g. swiping away the app from the Recent Apps List).
  • the process names have to be known at compile time.

android:multiprocess

Looking at the AndroidManifest.xml documentation, I have found this activity attribute which seems helpful:

android:multiprocess

Whether an instance of the activity can be launched into the process of the component that started it — true if it can be, and false if not. The default value is false.

Normally, a new instance of an activity is launched into the process of the application that defined it, so all instances of the activity run in the same process. However, if this flag is set to true, instances of the activity can run in multiple processes, allowing the system to create instances wherever they are used (provided permissions allow it), something that is almost never necessary or desirable.

My reading of this documentation is that this could be what I am looking for. I would prefer it to say "the activity is launched" rather than "the activity can be launched" "into the process of the component that started it".

Unfortunately, this doesn't seem to be the case. Launching Host activity from Guest always ends up with different processes, as measured by android.os.Process.myPid().

I have put up a pair of dummy applications which should show what I am trying to do:

  • install Host.
  • install and run Guest
  • The activity launched will have a number (the pid of the guest) and three buttons. Clicking on either of the first two should launch a second activity. The second activity should show the same number as the first, and true, which shows that the host and guest are running in the same process.

Unfortunately, this does not appear to be the case.

TL;DR

How do I get the activities from the different activities to run in the same process owned by the the caller application?

What does android:multiprocess on the activity element actually do?

Edit: I have a minimal example which should show what I'm wanting. Success is when the MainHostActivity shows the same number as MainGuestActivity. The number is android.os.Process.myPid().

  • Github link to minimal example
  • Stackoveflow question

回答1:

"android:process" and "android:taskAffinity" allows you to have different processes and affinities within the same package (application).

"android:multiprocess" simply means if you want to allow the activity to have a different process-id. This only works within the same app, not when the activity is started from a different a different package.

What are you exactly trying to do? If you want to share the same context and private files, you should use sharedUserId instead (you have to sign them using the same certificate as well).



回答2:

Try signing both apps with the same certificate. From http://developer.android.com/guide/components/processes-and-threads.html:

You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

If you can't control Guest later on, you might be SOOL