1. 程式人生 > >Android windows+chrome 檢視Realm資料庫

Android windows+chrome 檢視Realm資料庫

Stetho

Stetho是Facebook出品的一個非常強大的Android除錯工具。 啟用後,開發人員可以使用Chrome瀏覽器的Chrome開發者工具功能。 開發人員還可以選擇啟用可選的dumpapp工具,它為應用程式內部提供了強大的命令列介面。 
Stetho可以監聽網路請求,從而快速除錯和分析傳送和接受的資料包。可以檢視手機中的資料庫,包括Sqlite和Realm資料庫。可以檢視分析檢視樹等等。 
網頁介紹:http://facebook.github.io/stetho/

步驟

Realm資料庫使用.realm檔案來儲存資料庫資訊,在日常開發過程中,需要資料庫視覺化工具來檢視。但Realm現在暫時只支援了mac系統可以檢視.realm資料庫,在win上則沒有提供工具,只能通過Stetho藉助Chrome瀏覽器來進行查看了。

Step1:新增依賴 
在專案根目錄下的build.gradle中的dependencies新增classpath “io.realm:realm-gradle-plugin:5.1.0”

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath "io.realm:realm-gradle-plugin:5.1.0"//新增這一行
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Step2:初始化Stetho 
建立一個MyApplication,繼承Application,重寫onCreate方法,在onCreate方法裡面進行初始化:

public class MyApplication extends Application {

    @Override
    public void onCreate(){
        super.onCreate();

        Realm.init(this);//Realm資料庫初始化

        Stetho.initialize(//Stetho初始化
                Stetho.newInitializerBuilder(this)
                .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())
                .build()
        );
    }
}

然後在AndroidManifest.xml中宣告自定義的Application:

<application
        android:name="application.MyApplication"//宣告你的application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

Step3:完成上述的步驟之後,AndroidStudio重新run一下你的應用程式。開啟Chrome瀏覽器輸入chrome://inspect/#devices ,檢視device狀態,可以看見你應用的程序出現在了此處,點選inspect按鈕即可進入除錯介面。如圖:

可以看到包名就是你現在執行的App的程序,然後點選inspect 按鈕就可以進入到Chrome開發工具介面了,如圖:

選擇工具欄上面的Resources選單進入檢視當前App資料庫資訊,如圖: 

在Web SQL中可以看到default.realm,也就是Realm預設的資料庫名稱了,點選就可以看到這個資料庫裡面的表了,通常表名都是class_TableName 的格式了。只能檢視該資料庫中的資訊,但不能操作資料庫,也就是不能進行插入,更新,刪除操作。

注意:每次重新run程式碼的時候,都會斷開Chrome除錯工具,也就是Connection Lose。因此只能重新Inspect進入除錯工具。

使用參考:https://blog.csdn.net/huangxiaoguo1/article/details/78851323

原文地址