Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Package Name of Source Class File in Android ?

I am using only one project to port lite and full versions for that I am just updating Manifest file package names.

My project structure is like this

My App
      |_src
          |_com.example.myapp
                                              |_MyClass.java

and my Manifest package name is different.

package="com.example.myapplite"

I am using one library project in that I want package names of classes that I am using in MyApp.

I have used this

        System.out.println("Package Name " + context.getPackageName());

But its giving me a Manifest file package name value ie. com.example.myapplite

I want MyClass.java package name. ie. com.example.myapp

Also I am aware about this

 MyClass mClass = new MyClass();
 Package pack = mClass.getClass().getPackage();

But I can't use this approach because I am using following approach to get class file in my library project

Class c = Class.forName("package_name.MyClass");

How to get package name of MyClass.java file programmatically?

like image 746
Mac Avatar asked Feb 15 '13 07:02

Mac


People also ask

How do I find my Android package name?

You can find an app's package name in the URL of your app's Google Play Store listing. For example, the URL of an app page is play.google.com/store/apps/details? id=com. example.

What is the package name of class?

The package for a class can be obtained using the java. lang. Class. getPackage() method with the help of the class loader of the class.

What is Android Package name?

All Android apps have a package name. The package name uniquely identifies the app on the device; it is also unique in the Google Play store.

How do I view .class files in Android Studio?

You can find them in the build/intermediates/classes/debug/* directory. Save this answer. Show activity on this post. Rename the following .


1 Answers

The Kotlin solution:

val packageName = this.javaClass.`package`?.name
like image 70
Daniil Chuiko Avatar answered Sep 25 '22 10:09

Daniil Chuiko