Search code examples
androidexceptionandroid-libraryandroid-package-managersandroid-threading

How to get package name of the library in Android?


I've an android application where I depend on few libraries internally. Now I'm trying to log the crash happening at individual component level (which means individual library level) so that it would help me in analyzing which component is causing the crash.

public class MyApplication extends Application implements Thread.UncaughtExceptionHandler {

    public static final String TAG = "myapplication";

    public static MyApplication applicationInstance;

    public MyApplication() {
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public Context getApplicationContext() {
        return super.getApplicationContext();
    }

    public static MyApplication getInstance() {
        return applicationInstance;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }


    @Override
    public void uncaughtException(Thread t, Throwable e) {
        Log.i(TAG, "Inside uncaught exception..." + Thread.currentThread().getId());
        //getPackageName() here returns the name of the application instead of the dependent library's one. 
    }
}

Is there any way available to get the package name of the library added in their android manifest?


Solution

  • Not really. Libraries do not have application IDs. The package attribute in the manifest is just used for code generation, nothing more.

    If you happen to have an object from the library, you can get the Java package associated with that class, and perhaps from there infer which library it is that you are referring to.