1

How to read Package Name in Application Class ? Is it possible.

I tried as below.

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

Nothing works

Bullet
  • 87
  • 1
  • 11

3 Answers3

2

In Application class Constructor i need to read application package name

Get rid of the Application constructor. You should not have one, just as you do not put a constructor on an Activity.

Your entry point in Application is onCreate(). After you chain to the superclass in super.onCreate(), getPackageName() should work. It definitely will not work before then, such as from the constructor you are erroneously creating.

As Blundell notes, if you using Android Studio or Gradle for Android, you are welcome to use BuildConfig.APPLICATION_ID from anywhere.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

put it in onCreate() in the Application class

RoiBar
  • 81
  • 7
0
public class myapp implements application{}

public class exeapp implements myapp {  }

Assume myapp is a custom application class from another.

By implements of myapp class in new application class is it possible to read package name in onCreate()

Bullet
  • 87
  • 1
  • 11