facebook twitter hatena line email

Android/ErrorLogStack

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

エラーリストが取得用途

エラーレポーティング用途などで・・stacktraceエラーリストが取得できる

以下コードはfirebase-crashlyticsなどと併用可能だった。

sdkをproguardした時はクラス名は難読化される?アプリ本体をapk化されるとコードは難読化される?

CustomUncaughtExceptionHandler.java

import android.content.Context;
import android.content.SharedPreferences;
import java.io.PrintWriter;
import java.io.StringWriter;
public class CustomUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    private Context mContext;
    private Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;
    public CustomUncaughtExceptionHandler(Context context) {
        mContext = context;
        mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); // 既に登録されてるものを退避
    }
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        StringWriter stringWriter = new StringWriter();
        ex.printStackTrace(new PrintWriter(stringWriter));
        String stackTrace = stringWriter.toString();
        Log.d("error", "stackTrace=" + stackTrace);  // stackTraceこれがstacktraceエラーリスト
        mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex);
    }
}

ExampleClass.java

public class ExampleClass {
    public static void test() {
        new ArrayList<String>().get(0); //  エラーを強制的に起こす
    }
}

MainActivity.java

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomUncaughtExceptionHandler customUncaughtExceptionHandler = new CustomUncaughtExceptionHandler(
                getApplicationContext());
        Thread.setDefaultUncaughtExceptionHandler(customUncaughtExceptionHandler);
        ExampleClass.test();
    }
}

stacktraceエラーリストサンプル

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
	at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
	at java.util.ArrayList.get(ArrayList.java:308)
	at com.example.mycrashapplication.ExampleClass.test(Hogehoge.java:8)
	at com.example.mycrashapplication.MainActivity$1.onClick(MainActivity.java:33)
	at android.view.View.performClick(View.java:5231)
	at android.view.View$PerformClick.run(View.java:21190)
	at android.os.Handler.handleCallback(Handler.java:739)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:148)
	at android.app.ActivityThread.main(ActivityThread.java:5554)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)

stacktraceエラーリストサンプル(minifyオン時)

サイズminifyオン(難読処理)時

4行目がcom.example.mycrashapplication.d.aとなっている

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
	at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
	at java.util.ArrayList.get(ArrayList.java:308)
	at com.example.mycrashapplication.d.a(Unknown Source)
	at com.example.mycrashapplication.MainActivity$1.onClick(Unknown Source)
	at android.view.View.performClick(View.java:4438)
	at android.view.View$PerformClick.run(View.java:18422)
	at android.os.Handler.handleCallback(Handler.java:733)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:136)
	at android.app.ActivityThread.main(ActivityThread.java:5017)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:515)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
	at dalvik.system.NativeStart.main(Native Method)

stacktraceエラーリストサンプル(minifyオン時)

サイズminifyオン(難読処理)時でExampleClassにproguard設定時

proguard-rules.pro

-keep public class com.example.mycrashapplication.ExampleClass {
    public *;
}

ログ

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
	at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
	at java.util.ArrayList.get(ArrayList.java:308)
	at com.example.mycrashapplication.ExampleClass.test(Unknown Source)
	at com.example.mycrashapplication.MainActivity$1.onClick(Unknown Source)
	at android.view.View.performClick(View.java:4438)
	at android.view.View$PerformClick.run(View.java:18422)
	at android.os.Handler.handleCallback(Handler.java:733)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:136)
	at android.app.ActivityThread.main(ActivityThread.java:5017)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:515)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
	at dalvik.system.NativeStart.main(Native Method)

iosではここらへん参考

https://qiita.com/ruwatana/items/cc470eb229d267d693b0

参考

https://dev.classmethod.jp/smartphone/android/android-app-exception/

https://kokufu.blogspot.com/2013/01/android-uncaughtexceptionhandler-anr.html