From 4700befb26f078ba77eedf07ddb3d85f095b2b33 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 13 Feb 2018 23:27:50 -0500 Subject: [PATCH] Add Application.getProcessName() This information is already available to non-sandboxed processes via ActivityManager.getRunningAppProcesses(). However, performing an IPC and iterating processes is quite cumbersome. See other alternatives devs have tried: https://stackoverflow.com/questions/19631894/is-there-a-way-to-get-current-process-name-in-android My specific motivation for exposing this information more directly is to be able to perform process-specific initialization logic in Application.attachBaseContext(): https://cs.chromium.org/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java?rcl=ac2e180a1265f88dd4030bb35d69f5d0b2dc488d&l=54 Bug: 73344323 Test: Same code that's used in Chrome via reflection. Change-Id: I83cec468458078e3fa183427a039869f74539c3d --- api/current.txt | 1 + core/java/android/app/Application.java | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/api/current.txt b/api/current.txt index 72666062c7fee..186f66b91ccad 100644 --- a/api/current.txt +++ b/api/current.txt @@ -4265,6 +4265,7 @@ package android.app { public class Application extends android.content.ContextWrapper implements android.content.ComponentCallbacks2 { ctor public Application(); + method public static java.lang.String getProcessName(); method public void onConfigurationChanged(android.content.res.Configuration); method public void onCreate(); method public void onLowMemory(); diff --git a/core/java/android/app/Application.java b/core/java/android/app/Application.java index 81cbbcafe8c42..41eeb9acb5ec5 100644 --- a/core/java/android/app/Application.java +++ b/core/java/android/app/Application.java @@ -191,6 +191,16 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 { } } + /** + * Returns the name of the current process. A package's default process name + * is the same as its package name. Non-default processes will look like + * "$PACKAGE_NAME:$NAME", where $NAME corresponds to an android:process + * attribute within AndroidManifest.xml. + */ + public static String getProcessName() { + return ActivityThread.currentProcessName(); + } + // ------------------ Internal API ------------------ /** @@ -333,4 +343,4 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 { } return null; } -} \ No newline at end of file +}