Google Playで配布しないアプリにクラッシュログ機能を実装する
Google Playで配布するAndroidアプリには、クラッシュログを送信する機能があります。
クラッシュログが回収できると、不具合解決に大変役立ちます。
しかし、世の中には下記のようないろんなケースで、Google Playで配布しない場合もあるのではないでしょうか?
- まだまだ開発途中すぎるAndroidアプリ
- 一部のお客さんだけに配布したいAndroidアプリ
- Google Playの規約に沿わないので、Google Playに公開できないアプリ
などなど、さまざまな理由があると思われます。
クラッシュログ送信機能がないと、多種多様すぎるAndroidの端末、さまざまな環境で試される場合の不具合修正が大変困難です。
しかし世の中便利なもので、クラッシュログを送信できるライブラリを作ってくれている人がいます。
それが、ACRA(Application Crash Report for Android)です。
ACRA
http://www.acra.ch/
インストールと利用方法も大変簡単です!
ここでは、クラッシュログをメールで集める方法を紹介します。
私はまだEclipseを利用していますので、jarファイルをコピーする方法を使います。
- 上記のページのDownload ACRAと書いてあるところから、ファイルをダウンロードします。
- その中から、acra-4.6.1.jar(この記事を書いている時点でのバージョン名です)をコピーして、eclipseのプロジェクト内のlibsにペーストします。
- AndroidManifestに一手間加えます。Applicationのタグのところに、下記のようにandroid:name=”MyApplication”を追加します。
- MyApplication.javaというクラスを作ります。下記のように書いておきます。
- strings.xmlに次のように書いておきます。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@android:style/Theme.Holo.Light"
android:name="MyApplication"
>
下記ではクラッシュレポートを送信する時に、ダイアログを表示するモードを利用していますが、ダイアログ表示のアクティビティもAndroidManifestに書いておかないといけません。
<!-- クラッシュレポート用 --> <activity android:name="org.acra.CrashReportDialog"
android:process=”:error_report”
android:launchMode="singleInstance"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true" />
package smart.location.admin.hogehoge;
import org.acra.*;
import org.acra.annotation.*;
import android.app.Application;
@ReportsCrashes(
mode = ReportingInteractionMode.DIALOG,
mailTo = "hogeccho@onlineconsultant.jp", //送信したいメールアドレス
resToastText = R.string.crash_toast_text, // optional, displayed as soon as the crash occurs, before collecting data which can take a few seconds
resDialogText = R.string.crash_dialog_text,
resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning sign
resDialogTitle = R.string.crash_dialog_title, // optional. default is your application name
resDialogCommentPrompt = R.string.crash_dialog_comment_prompt// optional. When defined, adds a user text field input with this text resource as a label
)
public class MyApplication extends Application {
@Override
public void onCreate() {
// The following line triggers the initialization of ACRA
super.onCreate();
ACRA.init(this);
}
}
<string name="crash_toast_text">クラッシュしました</string>
<string name="crash_dialog_text">クラッシュログを送信します</string>
<string name="crash_dialog_title">アプリがクラッシュしました</string>
<string name="crash_dialog_comment_prompt">何かコメントがあればお願いします。</string>
これで終わりです!
クラッシュしたときに、次のように表示されます。

ユーザーが何かコメントを入力すると、次にメールのアプリが立ち上がり、メールを送信してきてくれます。
便利です![[smile]](https://i0.wp.com/oc-technote.com/wp-content/themes/oc/assets/images/face/smile.png?w=525&ssl=1)
しかし、このクラッシュログは動作するのはクラッシュのときだけです。
クラッシュとは、アプリが落ちるアレです。
ANRはこれでログを送信できません。
ANRとは、Application Not Respondingでアプリでの処理に時間がかかっているなどの理由から応答がない場合に発生する問題です。
ANRの場合のログを送信するのにも挑戦したいと考えました。次の記事で紹介します。
Google Playで配布しないアプリにANRログ機能を実装する
