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

北战南征 提交于 2019-12-18 04:42:30

问题


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?


回答1:


Might Be helpfull

package org.kodejava.example.lang;
import java.util.Date;

public class ObtainingPackageName {
public static void main(String[] args) {
    //
    // Create an instance of Date class, and then obtain the class package
    // name.
    //
    Date date = new Date();
    Package pack = date.getClass().getPackage();
    String packageName = pack.getName();
    System.out.println("Package Name = " + packageName);

    //
    // Create an insatnce of our class and again get its package name
    //
    ObtainingPackageName o = new ObtainingPackageName();
    packageName = o.getClass().getPackage().getName();
    System.out.println("Package Name = " + packageName);
   }
} 



回答2:


try this,

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

        Object o = c.newInstance();

        Package p = o.getClass().getPackage();

        System.out.println("Package Name :: " + p.getName());

    } catch (ClassNotFoundException e) {            
        e.printStackTrace();
    } catch (InstantiationException e) {            
        e.printStackTrace();
    } catch (IllegalAccessException e) {            
        e.printStackTrace();
    }



回答3:


String packageName = this.getClass().getPackage().getName();

will work definitely.




回答4:


Simple example of some class NotificationListener:

Timber.d(NotificationListener.class.getPackage().getName());
Timber.d(NotificationListener.class.getSimpleName());
Timber.d(NotificationListener.class.getName());

Output:

D/PermissionsUtil: com.example.receivers
D/PermissionsUtil: NotificationListener
D/PermissionsUtil: com.example.receivers.NotificationListener



回答5:


The Kotlin solution:

val packageName = this.javaClass.`package`?.name


来源:https://stackoverflow.com/questions/14889694/how-to-get-package-name-of-source-class-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!