5

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?

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
Keselme
  • 3,779
  • 7
  • 36
  • 68
  • 1
    "what can be causing this to happen?" -- something crashed in an OS process. "How can I prevent it from happening again?" -- I doubt that is possible. – CommonsWare Mar 20 '19 at 14:43
  • 1
    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. – JensV Mar 20 '19 at 14:44

1 Answers1

2

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.

JensV
  • 3,997
  • 2
  • 19
  • 43
  • The code snippet is part of SDK, that later is added as a dependency into another app. Will your suggested solution still work in that case? – Keselme Mar 20 '19 at 14:53
  • If you are able to change that SDK yes. If you're unable to change it, you will have to find a work around so that you can implement your own code instead of calling this function – JensV Mar 20 '19 at 14:54
  • Just to clarify, " .. and the version is defined in your app.gradle .." it should be defined in the Gradle of the app that uses my SDK? If that's the case I can't guaranty that. – Keselme Mar 20 '19 at 14:58
  • Oh I misunderstood. In that case it wouldn't work sorry. – JensV Mar 20 '19 at 14:59
  • @Keselme You're probably better off if you tried to limit `getPackageInfo` with it's flags – JensV Mar 20 '19 at 15:00
  • 1
    @JensV your theory seems to be correct. Making a getPackageInfo() call with PackageManager.GET_META_DATA flag fixes the issue. In Kotlin: val packageInfo = getPackageInfo(packageName, PackageManager.GET_META_DATA) – Milan Sep 14 '20 at 13:14
  • @Milan Adding PackageManager.GET_META_DATA should add the metaData to the applicationInfo returned which is otherwise null when we pass 0. This should return more data and not less. How is this fixing the issue – v3rt1ag0 Nov 11 '22 at 20:58
  • @v3rt1ag0 Test it... – Milan Nov 14 '22 at 06:37
  • @v3rt1ag0 this has been a while now, but if I remember correctly, passing zero flags will by default return a default set of data and as soon as you pass at least one flag, it will retrieve only what you specify by flags. But as Milan mentionend, you should test it for yourself. – JensV Nov 14 '22 at 08:27