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!
9 Answers
Use : getPackageManager().getPackageInfo(getPackageName(), 0).packageName
Or just MyContext.getPackageName()
You can also create a function:
public String getPackageName(Context context) {
return context.getPackageName();
}
-
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. Commented Aug 17, 2011 at 10:21
-
2So create a function like that :
MyFunction(Context MyContext){ MyContext.getPackageName() }
Commented Aug 17, 2011 at 18:14 -
String pkg_name = getApplicationContext().getPackageName()
Commented Nov 20, 2021 at 7:22
The following constant is available without the need for a context.
BuildConfig.APPLICATION_ID
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();
-
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. Commented Aug 17, 2011 at 10:23
getApplicationContext().getPackageName()
will give the package name.
-
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
-
1This 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)– SakiboyCommented Jan 19, 2018 at 18:20
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
}
-
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. Commented Aug 17, 2011 at 10:23
-
then you can send a reference to the context and can use that this way– RaselCommented Aug 17, 2011 at 10:37
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
}
}
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?
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;
}
}
-
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.– SakiboyCommented May 17, 2023 at 2:11
I think getPackageName()
is available directly within activity without calling getApplicationContext
. Checked it myself.Working good.