起因
最近從錯誤日志中檢查到一個異常崩潰:
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分享之前調(diào)用下方代碼判斷是否已安裝該應(yīng)用心铃。
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);調(diào)用時可能引發(fā)java.lang.RuntimeException: Package manager has died這個異常挫剑。原因是目前版本的QQ(v6.6.9)PackageInfo中數(shù)據(jù)量太大了去扣,在部分手機上超出了Binder可傳遞的最大容量,進而導(dǎo)致PacakgeManager崩潰樊破。
驗證
我在三星S6 Android6.0.1這部手機上重現(xiàn)了這個崩潰愉棱。當我將QQ換為一個較早的版本后,上面檢測的這段代碼不會引起崩潰哲戚,因此驗證了我的推斷:近期QQ的更新使得PackageInfo增大引發(fā)這個異常奔滑。另外,這個崩潰不會在所有機型上出現(xiàn)顺少,也不是偶現(xiàn)朋其,只在部分機型上必現(xiàn)王浴。推測和系統(tǒng)層中對Binder傳遞數(shù)據(jù)最大容量的不同限制大小有關(guān)。
解決
如果是使用友盟分享SDK時發(fā)生的問題梅猿,請將SDK更新到Android 社會化組件SDK v6.2.3(2017-1-23)以上版本即可修復(fù)氓辣。
如果是自己寫的判斷方法報錯,繼續(xù)往下看袱蚓。
最直接的想法是catch住這個異常钞啸,這樣處理的確能夠避免引發(fā)崩潰,可以用來緊急解決崩潰的問題癞松。但是這樣做還是無法正常判斷QQ是否已經(jīng)安裝爽撒。
既然是PackageInfo過大那么我們可以嘗試改變getPackageInfo(var0, 1)它的第二個參數(shù)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硕勿,它的作用是返回應(yīng)用的簽名,我們知道任何應(yīng)用都只有一個簽名枫甲,它的大小應(yīng)該是相對固定的源武。那么我們只需要改變參數(shù)flag的值即可在正確檢查是否安裝應(yīng)用的同時避免引發(fā)崩潰。
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;
}
}
總結(jié)
雖然這次引發(fā)崩潰的是QQ想幻,但今后我們在應(yīng)用中獲取其它應(yīng)用的PackageInfo時都應(yīng)該注意這個問題粱栖,傳遞恰當?shù)膄lag避免同類問題的產(chǎn)生。