The usual way to obtain the application package (as a string) on Android is invoking getPackageName() on an Activity or Application instance:
This method is not static, i.e. you must first have a Context instance at hand in order to obtain the package name.
But it feels like it should be possible to obtain the application package in a static way too.. After all the code is executing in the context of a specific APK, which determines the application package. The package is also globally specified in the AndroidManifest.xml.
Yet after some investigation it becomes clear that the android API overlords specifically wanted to make this task inaccessible to the mortal developers.
After some more searching one solution comes to light:
static String android.app.ActivityThread.currentPackageName();
As you see, this method is static, as we wanted it; but there are two caveats:
- ActivityThread.currentPackageName() must be invoked from the main thread of the application
- the android.app.ActivityThread class is API-hidden, so you need tricks to access it