LoginSignup
14
11

More than 5 years have passed since last update.

Fabricの初期設定(ログ出力方法とapp identifier)を変えてみた。

Last updated at Posted at 2015-08-09

環境

Fabric v1.3.5
Android Studio 1.3.1
Mac OS X 10.10.4

前提

Fabricに関しての詳細はAndroidStudioでプラグインは使わずにFabricを導入してみた。に記載したので、そちら参照。

初期化方法

ドキュメント通りにやる(またはplugin経由でコードを書き換える)と以下のようなコードが追加される。

MainActivity.java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());
        setContentView(R.layout.activity_main);
    }

ドキュメントやStackOverFlowにもないけど、実はFabricクラス内にBuilderクラスが存在していて、これを使った初期化方法があります。

MainActivity.java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric fabric = new Fabric.Builder(getApplicationContext())
            .kits(new Crashlytics())
            .build();
        Fabric.with(fabric);
        setContentView(R.layout.activity_main);
    }

このBuilderを使って、設定を変更してみます。

ログ設定

FabricをドキュメントやPlugin通りに使うと、logcatにログが流れる。
Android Studio(Gradle)でproguard時にログのコードを消す方法を使ってやってみたが、Crashlyticsのコード内で記載されているためか、消すことができなかった。(もしライブラリ内に記載されている、android.util.Logを削除できる方法があるのでしたらご指摘ください。)

Fabricのコードを読み解くと、デフォルトでは、DefaultLoggerを使っていて、ここではandroid.util.Logを利用しています。
このLoggerをSilentLoggerという、特に何も実装されていないLoggerに切り替えれば、release版アプリでもlogを垂れ流さなくなります。

MainActivity.java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric fabric = new Fabric.Builder(getApplicationContext())
            .kits(new Crashlytics())
            .logger(new SilentLogger())
            .build();
        Fabric.with(fabric);
        setContentView(R.layout.activity_main);
    }

AndroidのログライブラリはTimberが一番いいと思っていて、ちょっと前にこんなエントリー書いたりしているのだけど、CrashlyticsもTimberと同じような仕組みになっているようで、どうせならCrashlyticsのことも言及しておくべきでした。

Crashlyticsに送るapp identifierを変える

デフォルトでは、そのアプリのパッケージ名をapp identifierとして送信しています。

Fabric.class
            if(this.appIdentifier == null) {
                this.appIdentifier = this.context.getPackageName();
            }

このapp identifierを変えることによって、gradleによってbuild type/flavor毎にバラバラなアプリとして認識されていたけど、同じアプリとしてクラッシュレポートまとめたいとか、逆にbuild type/flavor別でも同じパッケージ名で、それぞれのレポートを分けたい!という場合、この設定を変えることによって対応することが可能なようです。

MainActivity.java
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());
        Fabric fabric = new Fabric.Builder(getApplicationContext())
            .kits(new Crashlytics())
            .appIdentifier("com.github.shiraji.fabrictest2")
            .build();
        Fabric.with(fabric);
        setContentView(R.layout.activity_main);
    }

その他の設定

他にも以下のメソッドが用意されています。

  • threadPoolExecutor(PriorityThreadPoolExecutor threadPoolExecutor) // 実行スレッドのpriority変えたい場合に利用
  • appInstallIdentifier(String appInstallIdentifier) // appInstallIdentifierを変える。
  • debuggable(boolean enabled) // DefaultLogger利用時にログレベルをLog.DEBUGにする。
  • initializationCallback(InitializationCallback initializationCallback) // 初期後のCallBack処理を設定。
14
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11