facebook twitter hatena line email

「Android/ErrorLogStack」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(エラーリストが取得用途)
(stacktraceエラーリストサンプル)
 
(同じ利用者による、間の14版が非表示)
行1: 行1:
 
==エラーリストが取得用途==
 
==エラーリストが取得用途==
 
エラーレポーティング用途などで・・stacktraceエラーリストが取得できる
 
エラーレポーティング用途などで・・stacktraceエラーリストが取得できる
 +
 +
以下コードはfirebase-crashlyticsなどと併用可能だった。
 +
 +
sdkをproguardした時はクラス名は難読化される?アプリ本体をapk化されるとコードは難読化される?
  
 
CustomUncaughtExceptionHandler.java
 
CustomUncaughtExceptionHandler.java
行13: 行17:
 
     public CustomUncaughtExceptionHandler(Context context) {
 
     public CustomUncaughtExceptionHandler(Context context) {
 
         mContext = context;
 
         mContext = context;
         mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
+
         mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); // 既に登録されてるものを退避
 
     }
 
     }
 
     @Override
 
     @Override
行22: 行26:
 
         Log.d("error", "stackTrace=" + stackTrace);  // stackTraceこれがstacktraceエラーリスト
 
         Log.d("error", "stackTrace=" + stackTrace);  // stackTraceこれがstacktraceエラーリスト
 
         mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex);
 
         mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex);
 +
    }
 +
}
 +
</pre>
 +
ExampleClass.java
 +
<pre>
 +
public class ExampleClass {
 +
    public static void test() {
 +
        new ArrayList<String>().get(0); //  エラーを強制的に起こす
 +
    }
 +
}
 +
</pre>
 +
 +
MainActivity.java
 +
<pre>
 +
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();
 
     }
 
     }
 
}
 
}
行27: 行60:
  
 
==stacktraceエラーリストサンプル==
 
==stacktraceエラーリストサンプル==
 +
<pre>
 +
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)
 +
</pre>
 +
 +
==stacktraceエラーリストサンプル(minifyオン時)==
 +
サイズminifyオン(難読処理)時
 +
 +
4行目がcom.example.mycrashapplication.d.aとなっている
 +
<pre>
 +
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)
 +
</pre>
 +
 +
==stacktraceエラーリストサンプル(minifyオン時)==
 +
サイズminifyオン(難読処理)時でExampleClassにproguard設定時
 +
 +
proguard-rules.pro
 +
<pre>
 +
-keep public class com.example.mycrashapplication.ExampleClass {
 +
    public *;
 +
}
 +
</pre>
 +
ログ
 +
<pre>
 +
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)
 +
</pre>
 +
 +
==iosではここらへん参考==
 +
https://qiita.com/ruwatana/items/cc470eb229d267d693b0
  
 
==参考==
 
==参考==
 
https://dev.classmethod.jp/smartphone/android/android-app-exception/
 
https://dev.classmethod.jp/smartphone/android/android-app-exception/
 +
 +
https://kokufu.blogspot.com/2013/01/android-uncaughtexceptionhandler-anr.html

2019年8月26日 (月) 17:44時点における最新版

エラーリストが取得用途

エラーレポーティング用途などで・・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