起因
最近从错误日志中检查到一个异常崩溃:
java.lang.RuntimeException: Package manager has died
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3151)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
at android.app.ActivityThread.access$1000(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1735)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.RuntimeException: Package manager has died
at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:167)
at com.umeng.socialize.utils.DeviceConfig.isAppInstalled(DeviceConfig.java:46)
at com.umeng.socialize.sso.UMTencentSsoHandler.isClientInstalled(UMTencentSsoHandler.java:456)
at com.haodf.android.posttreatment.doctorhonnrreflect.fragment.UmengShareUtil.shareContentToQQZone(UmengShareUtil.java:220)
at com.haodf.android.basic.home.extension.AdDetailActivity.init(AdDetailActivity.java:74)
at com.haodf.android.base.activity.AbsBaseActivity.onCreate(AbsBaseActivity.java:116)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3104)
... 10 more
Caused by: android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at android.content.pm.IPackageManager$Stub$Proxy.getPackageInfo(IPackageManager.java:2222)
at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:162)
... 18 more
java.lang.RuntimeException: Package manager has died
at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:167)
at com.umeng.socialize.utils.DeviceConfig.isAppInstalled(DeviceConfig.java:46)
at com.umeng.socialize.sso.UMTencentSsoHandler.isClientInstalled(UMTencentSsoHandler.java:456)
at com.haodf.android.posttreatment.doctorhonnrreflect.fragment.UmengShareUtil.shareContentToQQZone(UmengShareUtil.java:220)
at com.haodf.android.basic.home.extension.AdDetailActivity.init(AdDetailActivity.java:74)
at com.haodf.android.base.activity.AbsBaseActivity.onCreate(AbsBaseActivity.java:116)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3104)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
at android.app.ActivityThread.access$1000(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1735)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at android.content.pm.IPackageManager$Stub$Proxy.getPackageInfo(IPackageManager.java:2222)
at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:162)
... 18 more
android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at android.content.pm.IPackageManager$Stub$Proxy.getPackageInfo(IPackageManager.java:2222)
at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:162)
at com.umeng.socialize.utils.DeviceConfig.isAppInstalled(DeviceConfig.java:46)
at com.umeng.socialize.sso.UMTencentSsoHandler.isClientInstalled(UMTencentSsoHandler.java:456)
at com.haodf.android.posttreatment.doctorhonnrreflect.fragment.UmengShareUtil.shareContentToQQZone(UmengShareUtil.java:220)
at com.haodf.android.basic.home.extension.AdDetailActivity.init(AdDetailActivity.java:74)
at com.haodf.android.base.activity.AbsBaseActivity.onCreate(AbsBaseActivity.java:116)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3104)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
at android.app.ActivityThread.access$1000(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1735)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
分析
该异常由友盟SDK抛出,友盟SDK在进行QQ分享之前调用下方代码判断是否已安装该应用。
public static boolean isAppInstalled(String var0, Context var1) {
if(var1 == null) {
return false;
} else {
PackageManager var2 = var1.getPackageManager();
boolean var3 = false;
try {
var2.getPackageInfo(var0, 1);
var3 = true;
} catch (NameNotFoundException var5) {
var3 = false;
}
return var3;
}
}
在这段代码中,var2.getPackageInfo(var0, 1);调用时可能引发java.lang.RuntimeException: Package manager has died这个异常。原因是目前版本的QQ(v6.6.9)PackageInfo中数据量太大了,在部分手机上超出了Binder可传递的最大容量,进而导致PacakgeManager崩溃。
验证
我在三星S6 Android6.0.1这部手机上重现了这个崩溃。当我将QQ换为一个较早的版本后,上面检测的这段代码不会引起崩溃,因此验证了我的推断:近期QQ的更新使得PackageInfo增大引发这个异常。另外,这个崩溃不会在所有机型上出现,也不是偶现,只在部分机型上必现。推测和系统层中对Binder传递数据最大容量的不同限制大小有关。
解决
如果是使用友盟分享SDK时发生的问题,请将SDK更新到Android 社会化组件SDK v6.2.3(2017-1-23)以上版本即可修复。
如果是自己写的判断方法报错,继续往下看。
最直接的想法是catch住这个异常,这样处理的确能够避免引发崩溃,可以用来紧急解决崩溃的问题。但是这样做还是无法正常判断QQ是否已经安装。
既然是PackageInfo过大那么我们可以尝试改变getPackageInfo(var0, 1)它的第二个参数flag,使得该方法返回的对象容量减小。
/**
* Retrieve overall information about an application package that is
* installed on the system.
* <p>
* Throws {@link NameNotFoundException} if a package with the given name can
* not be found on the system.
*
* @param packageName The full name (i.e. com.google.apps.contacts) of the
* desired package.
* @param flags Additional option flags. Use any combination of
* {@link #GET_ACTIVITIES}, {@link #GET_GIDS},
* {@link #GET_CONFIGURATIONS}, {@link #GET_INSTRUMENTATION},
* {@link #GET_PERMISSIONS}, {@link #GET_PROVIDERS},
* {@link #GET_RECEIVERS}, {@link #GET_SERVICES},
* {@link #GET_SIGNATURES}, {@link #GET_UNINSTALLED_PACKAGES} to
* modify the data returned.
* @return Returns a PackageInfo object containing information about the
* package. If flag GET_UNINSTALLED_PACKAGES is set and if the
* package is not found in the list of installed applications, the
* package information is retrieved from the list of uninstalled
* applications (which includes installed applications as well as
* applications with data directory i.e. applications which had been
* deleted with {@code DONT_DELETE_DATA} flag set).
* @see #GET_ACTIVITIES
* @see #GET_GIDS
* @see #GET_CONFIGURATIONS
* @see #GET_INSTRUMENTATION
* @see #GET_PERMISSIONS
* @see #GET_PROVIDERS
* @see #GET_RECEIVERS
* @see #GET_SERVICES
* @see #GET_SIGNATURES
* @see #GET_UNINSTALLED_PACKAGES
*/
public abstract PackageInfo getPackageInfo(String packageName, int flags)
throws NameNotFoundException;
在注释中可以看到GET_SIGNATURES这个flag,它的作用是返回应用的签名,我们知道任何应用都只有一个签名,它的大小应该是相对固定的。那么我们只需要改变参数flag的值即可在正确检查是否安装应用的同时避免引发崩溃。
public static boolean isAppInstalled(String var0, Context var1) {
if(var1 == null) {
return false;
} else {
PackageManager var2 = var1.getPackageManager();
boolean var3 = false;
try {
var2.getPackageInfo(var0, PackageManager.GET_SIGNATURES);
var3 = true;
} catch (NameNotFoundException var5) {
var3 = false;
}
return var3;
}
}
总结
虽然这次引发崩溃的是QQ,但今后我们在应用中获取其它应用的PackageInfo时都应该注意这个问题,传递恰当的flag避免同类问题的产生。
网友评论