Google APK ANR 优化方案

640?wx_fmt=gif

640?wx_fmt=gif

极力推荐文章:欢迎收藏

Android 干货分享 640?wx_fmt=gif

阅读五分钟,每日十点,和您一起终身学习,这里是 程序员Android

本篇文章主要介绍 Android 开发中的部分GMS 包相关APK ANR,闪退问题解决方案知识点,通过阅读本篇文章,您将收获以下内容:

一、开机向导时 Google DUO 概率ANR

一、开机向导时 DUO 概率ANR

Log中分析主要原因是android.intent.action.LOCALE_CHANGED 广播接收超时导致的ANR

ANR Log 如下:

640?wx_fmt=other

ANR Log

ANR 规避方案如下:

BroadcastQueue类的 processNextBroadcast方法中,当第一次开机时候,跳过此Action。

修改类路径如下:

/alps/frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java

public final class BroadcastQueue {
  ... ...
    final void processNextBroadcastLocked(boolean fromMsg, boolean skipOomAdj) {
        BroadcastRecord r;
          ... ...
         // import android.provider.Settings;
         //when frist boot , ingore Google Duo anr when receive broadcast : android.intent.action.LOCALE_CHANGED
            if (info.activityInfo.name.contains ("com.google.android.apps.tachyon") &&
                               r.intent.getAction().equals("android.intent.action.LOCALE_CHANGED")){
                 int deviceProvisioned = Settings.Global.getInt(mService.mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0);
                 if (deviceProvisioned == 0) {
                     Slog.e(TAG,"switch users or first boot google duo ANR ignore");
                     skip = true;
                 }
            }

        // This is safe to do even if we are skipping the broadcast, and we need
        // this information now to evaluate whether it is going to be allowed to run.
        final int receiverUid = info.activityInfo.applicationInfo.uid;
        // If it's a singleton, it needs to be the same app or a special app
          ... ...
  }
    ... ...
}

git diff 修改如下:

640?wx_fmt=other

git 修改记录

二、开机向导时 Calendar 概率 ANR

ANR Log 如下:

640?wx_fmt=other

Calendar ANR log

ANR 规避方案如下:

主要原因是 android.intent.action.LOCALE_CHANGED 广播接收超时导致的ANR。

修改方案

请参考修改一

三、 开机向导时,ANR 弹框不show的解决方案

刷机或者恢复出厂设置是,开机向导过程中不应该显示ANRframeworks/base/services/core/java/com/android/server/am/AppErrors.javaAppErrors 类中 handleShowAnrUi方法,控制在开机向导时ANR弹窗。

class AppErrors {
        ... ...
        void handleShowAppErrorUi(Message msg) {
                             ... ...
                // If we've created a crash dialog, show it without the lock held
        
                if (d != null) {
                    int deviceProvisioned = Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED,0);
                        if(proc.userId == 0){
                            if(deviceProvisioned == 0 && !proc.processName.equals("com.google.android.setupwizard")){
                       
                               mService.killAppAtUsersRequest(proc, null);
                            }else{
                             d.show();
                           }
                        } else {
                          d.show();
                        }
                }
            ... ...
        }
        ... ...
}

git 解决方案

640?wx_fmt=other

git 修改差别的

四、开机向导时 Google Music 概率 ANR

开机向导时候 接收android.intent.action.LOCALE_CHANGED 广播超时导致的ANR。

ANR Log 如下:

640?wx_fmt=other

ANR Log

修改方案

请参考修改一

五、开机向导时 Google Play Store 概率 ANR

开机向导时候 接收android.intent.action.LOCALE_CHANGED 广播超时导致的ANR。

ANR Log 如下:

640?wx_fmt=other

ANR Log

ANR 规避方案如下:

请参考修改一

六、Google play Store 下载apk 概率性闪退

低内存情况下,使用play Store下载多个apkPlaystore 概率性ANR。

闪退 Log 信息

640?wx_fmt=other

Google Play Store 被kill Log信息

解决闪退问题方法

ActivityManagerService中的applyOomAdjLocked方法中修改adj值,防止apk 低内存情况下被杀掉。frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

public class ActivityManagerService extends IActivityManager.Stub
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
        ... ...
        protected boolean applyOomAdjLocked(ProcessRecord app, boolean doingAll, long now,
            long nowElapsed) {
              ... ...
            // add by wangjie for google play store was killed in sometime
            if(app.curAdj>3){
                if( app.processName.equals("com.android.vending") ||app.processName.equals("com.google.android.gms")){
                    app.curAdj = 3;
                }
            }
            // add by wangjie for google play store was killed in sometime
            ... ...
         }
        ... ...
}

解决方案如下

640?wx_fmt=other

解决闪退方案

640?wx_fmt=png

坚持就有惊喜

posted @ 2019-08-20 22:00  程序员Android的博客  阅读(102)  评论(0编辑  收藏  举报