LoginSignup
51
48

More than 5 years have passed since last update.

デバッグ時のみStetho, LeakCanary, StrictModeを入れる

Last updated at Posted at 2015-08-15

デバッグが便利になるライブラリがいくつかあります。
例えば

  • Stethoを入れるとDBの中が見れたり、Preferenceやネットワークの通信を見るのに使えます
  • LeakCanaryを入れるとメモリリークしている部分を検出してくれます。
  • StrictModeはAndroid標準なのですが、パフォーマンスで問題になりそうな処理があると(例えばメインスレッドでファイルを読み込んだりなど)、Logcatに表示してくれたりします。

デバッグ向けのライブラリを入れる場合、デバッグのビルドの時のみコードが入っている状態したいですよね

LeakCanaryは以下のようにリリース時は何もしないパッケージを指定することで利用できます。しかしStethoやStrictModeはそうはいきません。

 dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
 }

AndroidGradlePluginの機能を利用することで、デバッグ時のみApplicationクラスを上書きして利用して有効にさせる方法があるのでそれを紹介します。

まずbuild.gradleです。
以下のようにしてライブラリをデバッグ時のみ参照します。

dependencies {
    debugCompile 'com.facebook.stetho:stetho:1.1.1'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
}

app/src/debugフォルダを作成します。

そこにAndroidManifest.xmlファイルを作成し以下のように書き、デバッグ時のみDebugApplicationというクラスにApplicationクラスを上書きします。

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.takahirom.sandboxapp">

    <application
        android:name=".DebugApplication"
        tools:replace="android:name" />

</manifest>

app/src/debug/javaにDebugApplicatoinクラスを以下のように作成します
※MyApplicationクラスはapp/src/main/javaにApplicationクラスとして作成しておいてください。


public class DebugApplication extends MyApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        new Handler().postAtFrontOfQueue(
                new Runnable() {
                    @Override
                    public void run() {
                        // ApplicationクラスでStrictModeが有効できない問題のワークアラウンド https://code.google.com/p/android/issues/detail?id=35298
                        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                                        .detectDiskReads()
                                .detectDiskWrites()
                                        .detectAll()
                                        .penaltyLog()
                                        .build());
                        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                                . detectLeakedSqlLiteObjects()
                                . detectLeakedClosableObjects()
                                . penaltyLog()
                                .penaltyDeath()
                                .build());
                    }
                }
        );


        LeakCanary.install(this);
        Stetho.initialize(Stetho.newInitializerBuilder(this)
                .enableDumpapp( Stetho.defaultDumperPluginsProvider(this))
                .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
                        .build());
    }
}

フォルダ構成はこんな感じになります
image

以上で有効にできているはずです。

何か間違いなどがありましたらコメントお願いします。

サンプルアプリ

参考

51
48
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
51
48