I am aware of the availability of Context.getApplicationContext() and View.getContext(), through which I can actually call Context.getPackageName() to retrieve the package name of an application.
They work if I call from a method to which a View
or an Activity
object is available, but if I want to find the package name from a totally independent class with no View
or Activity
, is there a way to do that (directly or indirectly)?
You can use undocumented method
android.app.ActivityThread.currentPackageName()
:Caveat: This must be done on the main thread of the application.
Thanks to this blog post for the idea: http://blog.javia.org/static-the-android-application-package/ .
If you use the gradle-android-plugin to build your app, then you can use
to retrieve the package name from any scope, incl. a static one.
If you use gradle build, use this:
BuildConfig.APPLICATION_ID
to get the package name of the application.Hope it will work.
Use: BuildConfig.APPLICATION_ID to get PACKAGE NAME anywhere( ie; services, receiver, activity, fragment, etc )
Example: String PackageName = BuildConfig.APPLICATION_ID;