GetPackageInfo results in DeadObjectException

1.2k Views Asked by At

I have the following code snippet:

 public static String getAppVersion(Context context) {

        String versionName = null;
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; //This is the problematic line
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

Now according to Crashlytics, there was a case when the app crashed giving the following exception:

Caused by android.os.DeadObjectException
com.tawkon.data.lib.util.ParameterUtils.getAppVersion
    android.os.BinderProxy.transactNative (Binder.java)
    android.os.BinderProxy.transact (Binder.java:503)
    android.content.pm.IPackageManager$Stub$Proxy.getPackageInfo (IPackageManager.java:2684)
    android.app.ApplicationPackageManager.getPackageInfo (ApplicationPackageManager.java:193)
    com.tawkon.data.lib.util.ParameterUtils.getAppVersion (ParameterUtils.java:44)
    com.tawkon.data.lib.helper.analytics.NetworkRequestHelper.generateHttpRequest (NetworkRequestHelper.java:28)
    com.tawkon.data.lib.service.DataThroughputScanJobIntentService$2.onTestFinished (DataThroughputScanJobIntentService.java:342)
    com.tawkon.data.lib.collector.DataThroughputManager$1.run (DataThroughputManager.java:171) 

The device specs are Samsung with OS 6.

It seems that this is a rare crash in my case. Regardless, what can be causing this to happen? How can I prevent it from happening again?

2

There are 2 best solutions below

9
JensV On BEST ANSWER

A better way to fetch your own versionName (which I assume you are trying to do, judging your code) is to call BuildConfig.VERSION_NAME. This requires that the App is built with gradle and the version is defined in your app.gradle file.

For the PackageManager Problem itself I have a theory, though only anecdotal (copying from my comment on the question):

I ran into the same problem a while back. It seems when the IPC Communication from the PackageManager tries to send too much data it somehow dies. It will stay dead until your app restarts. The only "solution" I found is to limit the data provided from getPackageInfo with it's flags.

0
Wojciech Szymak On

In my case on DeadObjectException was causing problems on Android 6. PackageInfo object of com.google.android.gms (Google Play services) on that version of Android was to big. After limiting received informations with flags all started to work as expected.