10

I need to write some util class by myself and I need the packagename of android app. While I found the packageManager which only can be used in Activity and so on that has context. I only want to get packagename in my class which will be used in android app. So how can I do this? Thank you!

1
  • do u need all installed application's package name?
    – Rasel
    Commented Aug 17, 2011 at 9:58

9 Answers 9

43

Use : getPackageManager().getPackageInfo(getPackageName(), 0).packageName Or just MyContext.getPackageName()

You can also create a function:

public String getPackageName(Context context) {
    return context.getPackageName();
}
3
  • thank you,maybe I didn't describe clearly,It's an ordinary class,so it hasn't Context.The class will be packaged in a jar which used in android app.So I can't get method to reach it.
    – user898366
    Commented Aug 17, 2011 at 10:21
  • 2
    So create a function like that : MyFunction(Context MyContext){ MyContext.getPackageName() }
    – Guillaume
    Commented Aug 17, 2011 at 18:14
  • String pkg_name = getApplicationContext().getPackageName()
    – yurenchen
    Commented Nov 20, 2021 at 7:22
20

The following constant is available without the need for a context.

BuildConfig.APPLICATION_ID
4

you can get the package name of installed app:

ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
        PackageManager pm = getApplicationContext().getPackageManager();
        List<PackageInfo> packs = pm.getInstalledPackages(0);

and if you want to find the package name of your app:

context.getPackageName();
1
  • thank you,maybe I didn't describe clearly,It's an ordinary class,so it hasn't Context.The class will be packaged in a jar which used in android app.So I can't get method to reach it.thank you all,but all of your answer is same.
    – user898366
    Commented Aug 17, 2011 at 10:23
2

getApplicationContext().getPackageName() will give the package name.

2
  • why? Always give the reason if you are asked someone not to use it. I also don't know why you asked not to use this. Commented Jan 19, 2018 at 18:18
  • 1
    This will retrieve the wrong package name. See my answer below to always get the correct java package name. Your answer can be modified by gradle thus producing “inaccurate” package names (I.e. it’ll say debug, release, or whatever build flavor you have set up)
    – Sakiboy
    Commented Jan 19, 2018 at 18:20
1
List<ApplicationInfo> packages;
    PackageManager pm;
    pm = getPackageManager();
             get a list of installed apps.
            packages = pm.getInstalledApplications(0);

ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

   for (ApplicationInfo packageInfo : packages) {
   //packageInfo.packageName contains package name


                      }
2
  • thank you,maybe I didn't describe clearly,It's an ordinary class,so it hasn't Context.The class will be packaged in a jar which used in android app.So I can't get method to reach it.thank you all,but all of your answer is same.
    – user898366
    Commented Aug 17, 2011 at 10:23
  • then you can send a reference to the context and can use that this way
    – Rasel
    Commented Aug 17, 2011 at 10:37
1

you can create a Util Class this way:

public class AppInfo{
  private static Context cxt;
  public static String PACKAGE_INFO;
  public static String init(Context context){
    cxt = context;
    PACKAGE_INFO = cxt.getPackageName();
  }
}

then you can use it this way

public class MyActivity extends Activity{
  @Override
  public void onCreate(Bundle bundle){
    ...
    AppInfo.init(getApplicationContext());
    ...
  }
}

then you can call it this way:

public class someClass{
  public void someMethod(){
    String packageInfo = AppInfo.PACKAGE_INFO;
    //Do what you wish
  }
}
1

I have seen the following two ways to get the package name starting from a Context variable:

context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName

and

context.getPackageName()

Won't they always give the same answer? Is one way preferable to use over the other in certain circumstances?

1

Here add this to your util class. It makes sure to handle different “flavors” and debug/release builds, as other answers do not:

@Nullable
public static String getApplicationPackageName(@NonNull Context context)
{
    final PackageInfo packageInfo = getPackageInfo(context);
    if (packageInfo.applicationInfo == null)
    {
        return null;
    }
    // Returns the correct “java” package name that was defined in your
    // AndroidManifest.xml.
    return packageInfo.applicationInfo.packageName;
}

// Convenience method for getting the applications package info.
@NonNull
private static PackageInfo getPackageInfo(@NonNull final Context context)
{
    final PackageManager packageManager = context.getPackageManager();

    try
    {
        return packageManager.getPackageInfo(context.getPackageName(), 0);
    }
    catch (final PackageManager.NameNotFoundException ignored)
    {
        //noinspection ConstantConditions: packageInfo should always be available for the embedding app.
        return null;
    }
}
2
  • Isn't easier with BuildConfig.APPLICATION_ID? What cases doesn't cover? Commented Apr 20, 2023 at 10:08
  • Because of different build variants/flavors and also Gradle 8.0 is moving to namespaces rather than packages, which will probably affect all of this.
    – Sakiboy
    Commented May 17, 2023 at 2:11
0

I think getPackageName() is available directly within activity without calling getApplicationContext. Checked it myself.Working good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.