欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android8.1 将crash信息保存到sdcard

程序员文章站 2022-04-15 18:41:39
...

Android8.1 将crash信息保存到sdcard

背景:我做的是mtk平台,mtk本身有一个mtklogger可以保存日志,但是这个只在eng版本下会自动打开,user版本默认是关闭的,而且打开之后mtklogger有一个不可关闭的通知,这个看起来就很烦;
而且有一些发生crash的手机不能及时拿到,所以也无从分析问题所在,我的目标是将crash信息保存到手机中,然后在适当的时候上传的服务器,这样就可以远程分析问题了。

首先要在代码中找到crash信息,我通过crash的dialog最终找到了这个地方
framework/base/services/core/java/com/android/server/am/AppErrors.java
这个类看起来就感觉功能很明确,查看代码

void crashApplicationInner(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo,
            int callingPid, int callingUid) {
        long timeMillis = System.currentTimeMillis();
        String shortMsg = crashInfo.exceptionClassName;
        String longMsg = crashInfo.exceptionMessage;
        String stackTrace = crashInfo.stackTrace;

        ......
    }

只看需要的部分,这里可以看到一个变量 stackTrace ,基本就可以确定是这个了,保险起见,打印log可以发现正是我所需要的crash信息。
现在的问题就是怎么把这个信息保存到手机sdcard中,在这里直接进行io读写,并不能成功。网上查了下,都说framework对文件进行读写操作比较麻烦,所以我就转移思路,把这个信息传递到apk模块内,在模块内在进行读写操作,我这里参考了IKeyguardService.aidl

新建framework/base/core/java/com/android/internal/policy/IErrorInfo.aidl

package com.android.internal.policy;


oneway interface IErrorInfo {

   void setError(String error);

}

新建framework/base/services/core/java/com/android/server/policy/ErrorInfoWrapper.java

package com.android.server.policy;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import com.android.internal.policy.IErrorInfo;


public class ErrorInfoWrapper implements IErrorInfo {

    private IErrorInfo mService;

    public ErrorInfoWrapper(Context context, IErrorInfo service) {
        mService = service;
    }

    @Override
    public void setError(String error) {
        try {
            mService.setError(error);
        } catch (RemoteException e) {

        }
    }

    @Override // Binder interface
    public IBinder asBinder() {
        return mService.asBinder();
    }

}

新建framework/base/services/core/java/com/android/server/policy/ErrorInfoDelegate.java

package com.android.server.policy;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import com.android.internal.policy.IErrorInfo;
import com.android.server.UiThread;
import android.os.IBinder;
import android.os.UserHandle;

public class ErrorInfoDelegate {

    protected ErrorInfoWrapper mErrorInfoService;
    private final Context mContext;
    private final Handler mHandler;

    public ErrorInfoDelegate(Context context) {
        mContext = context;
        mHandler = UiThread.getHandler();
        bindService(context);
    }

    public void bindService(Context context) {
        Intent intent = new Intent();
        final ComponentName errorInfoComponent = ComponentName.unflattenFromString("com.android.systemui/com.android.systemui.keyguard.ErrorInfoService");
        intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
        intent.setComponent(errorInfoComponent);

        context.bindServiceAsUser(intent, mErrorInfoConnection,
                Context.BIND_AUTO_CREATE, mHandler, UserHandle.SYSTEM);

    }

    private final ServiceConnection mErrorInfoConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mErrorInfoService = new ErrorInfoWrapper(mContext,
                    IErrorInfo.Stub.asInterface(service));

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mErrorInfoService = null;

        }
    };

    public void setError(String error) {
        if (mErrorInfoService != null) {
            mErrorInfoService.setError(error);
        }
    }

}

在AppErrors.java中将crash信息传递到aidl中
services/core/java/com/android/server/am/AppErrors.java

private ErrorInfoDelegate mErrorInfoDelegate;
AppErrors(Context context, ActivityManagerService service) {
        context.assertRuntimeOverlayThemable();
        mService = service;
        mContext = context;
        /* begin */
        mService.mUiHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mErrorInfoDelegate = new ErrorInfoDelegate(context);
            }
        }, 100000);
        /* end */
    }
void crashApplicationInner(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo,
            int callingPid, int callingUid) {
        long timeMillis = System.currentTimeMillis();
        String shortMsg = crashInfo.exceptionClassName;
        String longMsg = crashInfo.exceptionMessage;
        String stackTrace = crashInfo.stackTrace;
        /* begin */
        if (mErrorInfoDelegate != null) {
            mErrorInfoDelegate.setError(shortMsg + longMsg + stackTrace);
        }
        /* end */
        if (shortMsg != null && longMsg != null) {
            longMsg = shortMsg + ": " + longMsg;
        } else if (shortMsg != null) {
            longMsg = shortMsg;
        }

        ......
}

这里实例化ErrorInfoDelegate的时候做了延时操作,主要怕影响到已有的逻辑。

到这里services中的操作已经完成,下面就是apk模块内的,我这里是在SystemUI中新增了一个service
AndroidManifest.xml中注册:

<service
                android:name=".keyguard.ErrorInfoService"
                android:exported="true"
                android:enabled="@bool/config_enableKeyguardService" />

新增ErrorInfoService.java

package com.android.systemui.keyguard;

import android.app.Service;
import android.content.Intent;
import android.os.*;
import com.android.internal.policy.IErrorInfo;
import com.android.systemui.util.LogUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ErrorInfoService extends Service {

    @Override
    public void onCreate() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IErrorInfo.Stub mBinder = new IErrorInfo.Stub() {

        @Override // Binder interface
        public void setError(String error) {
            writeText(error);
        }
    };

    private void writeText(String content) {

        if (content == null || content.equals("")) {
            return;
        }

        String filePath = "/storage/emulated/0/crashlog/";
        File dirFile = new File(filePath);
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".txt";
        File file = new File(filePath + fileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(content.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

ok,这个时候如果有app发生了crash,那么在手机里就能保存这个crash信息了

相关标签: Android crash