android – Context.getPackageName() vs BuildConfig.APPLICATION_ID-ThrowExceptions

Exception or error:

I can retrieve the package name using:




Is there any difference between those 2 ways?
Is one preferred to another in particular situations?
Is it safe to assume that they always give the same result?

Basically, which one is better?

How to solve:

BuildConfig is provided by Gradle. If you are not building using Gradle then you cannot access the package name using BuildConfig.

Using Context to get package name might not be applicable at times if there is no context or context is null, so you might then use BuildConfig.APPLICATION_ID.

I would use Context.getPackageName() because the result is provided from the operating system, rather than a constant in build parameters.


As of Android Studio 3.5, BuildConfig.APPLICATION_ID is deprecated and replaced with BuildConfig.LIBRARY_PACKAGE_NAME.

From Android Google Source:

BuildConfig: Deprecate APPLICATION_ID in libraries.

It is at best misleading, so it is marked as deprecated and replaced by


I used getPackageName() before – but only use BuildConfig.APPLICATION_ID since it became available. Should be faster as it is only a variable and no call – but should not really matter. Also in libraries using getPackageName() might be still needed.


It might possible to use both elements as basic measure against package tampering.
In your application you might insert some code like:

if (!BuildConfig.APPLICATION_ID.equals(getPackageName())) {
  insert code to report information about tampering to remote server

Sometime I get weird reports coming from some variant of my application that has been tampered by adding a prefix or suffix to the original packagename

Leave a Reply

Your email address will not be published. Required fields are marked *