15

In my application I need to know the name of package name. I have no problem when I want to grab it in activities but i can't take it in other classes. Following code is working in activity but i don't know why it has problem in simple class.

String packageName = getPackageName();

In my class I tried to write this code:

Context context = getApplicationContext();
String packageName = context.getPackageName();

but compiler said getApplicationContext() method is undefined for this class.

How can I take package name within this class?

Hesam
  • 52,260
  • 74
  • 224
  • 365
  • use BuildConfig.class.getPackage().toString(); https://stackoverflow.com/questions/6589797/how-to-get-package-name-from-anywhere/51479130#51479130 – user1506104 Jul 24 '18 at 04:29

8 Answers8

19

The simple, or another way is to pass Context into the helper class constructor:

MyClassConstructor(Context context){

        String packageName = context.getPackageName(); 
}
Lumis
  • 21,517
  • 8
  • 63
  • 67
15

Using instance of the class you can get package name by using getClass().getPackage().getName() to the instance

Sample Code

ClassA instanceOfClass = new ClassA();
String packageName = instanceOfClass.getClass().getPackage().getName();
System.out.println("Package Name = " + packageName);
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • But are you not creating an entire new instance of ClassA which is actually is going to perform some code on its own for no good reason? – Lumis Dec 27 '11 at 10:40
  • I have a library in my project and I pass the context from my project to the lib.The lib invokes context.getPackageName() which gets the lib's package name not my project's package name.How could the lib get my project's package name through the project's context? – Allen Vork Dec 12 '16 at 11:19
3

If you use gradle build, use this: BuildConfig.APPLICATION_ID to get the package name of the application.

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
  • please note that ApplicationID could be different than the actual package name used by your java classes. https://stackoverflow.com/questions/6589797/how-to-get-package-name-from-anywhere/51479130#51479130 – user1506104 Jul 24 '18 at 04:31
0

Simplest answer is make a class name constructor and pass the ApplicationContext in that constructor -

ClassConstructor(Context context){

        String packageName = context.getPackageName(); 
}
NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • 1
    When posting answers, please consider whether your answer contributes something that has not already been said in other answers. – laalto Jul 28 '16 at 12:24
0

In case someone is looking for this in Kotlin -

var pName = this.packageName
Sanved
  • 921
  • 13
  • 19
0

getApplicationContext() is a method of ContextWrapper ( super class of Activity).

If you want to use it in your classes you will have to pass a reference of a Context or its subclass and then use it

http://developer.android.com/reference/android/content/ContextWrapper.html#getPackageName()

class MyClass {
    Context mContext;

    public MyClass(Context ctx) [
        this.mContext = ctx;

    }

    String getPackageName() {
        mContext.getPackageName();
    }

}
Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
0

Use following

ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);

                        String packageName2 = am.getRunningTasks(1).get(0).topActivity.getPackageName();
bindal
  • 1,940
  • 1
  • 20
  • 29
-1

MAUI:

// In class MainActivity : MauiAppCompatActivity
string? packageName = ApplicationContext.ApplicationInfo.PackageName;