Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get package name from anywhere?

I am aware of the availability of Context.getApplicationContext() and View.getContext(), through which I can actually call Context.getPackageName() to retrieve the package name of an application.

They work if I call from a method to which a View or an Activity object is available, but if I want to find the package name from a totally independent class with no View or Activity, is there a way to do that (directly or indirectly)?

like image 210
ef2011 Avatar asked Jul 05 '11 23:07

ef2011


People also ask

How do I get Apppackage?

Step 1: Download “APK Info” app from Google Play Store on your android mobile. Step 4: Click the option “Detailed Information” option. It would show the detailed log for the app. Step 5: Then to find the appActivity name of the app, scroll down to the sub-section “Activities”.

Can I change package name Android?

Step 1: To rename package name in Android studio open your project in Android mode first as shown in the below image. Step 2: Now click on the setting gear icon and deselect Compact Middle Packages. Step 3: Now the packages folder is broken into parts as shown in the below image.


2 Answers

An idea is to have a static variable in your main activity, instantiated to be the package name. Then just reference that variable.

You will have to initialize it in the main activity's onCreate() method:

Global to the class:

public static String PACKAGE_NAME; 

Then..

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      PACKAGE_NAME = getApplicationContext().getPackageName(); } 

You can then access it via Main.PACKAGE_NAME.

like image 130
John Leehey Avatar answered Oct 03 '22 01:10

John Leehey


If you use the gradle-android-plugin to build your app, then you can use

BuildConfig.APPLICATION_ID 

to retrieve the package name from any scope, incl. a static one.

like image 34
Billda Avatar answered Oct 03 '22 01:10

Billda