框架整理系列二十四(Database 调试Android-Debug-Database)

感谢大神:https://github.com/amitshekhariitbhu/Android-Debug-Database

All these features work without rooting your device -> No need of rooted device

Check out another awesome library for fast and simple networking in Android.

Using Android Debug Database Library in your application

Add this to your app's build.gradle

debugCompile 'com.amitshekhar.android:debug-db:1.0.1'

Use debugCompile so that it will only compile in your debug build and not in your release build.

That’s all, just start the application, you will see in the logcat an entry like follows :

  • D/DebugDB: Open http://XXX.XXX.X.XXX:8080 in your browser

  • You can also always get the debug address url from your code by calling the method DebugDB.getAddressLog();

Now open the provided link in your browser.

Important:

  • Your Android phone and laptop should be connected to the same Network (Wifi or LAN).
  • If you are using it over usb, run adb forward tcp:8080 tcp:8080

Note : If you want use different port other than 8080.
In the app build.gradle file under buildTypes do the following change

debug {
    resValue("string", "PORT_NUMBER", "8081")
}

You will see something like this :

Seeing values

<img src=https://raw.githubusercontent.com/amitshekhariitbhu/Android-Debug-Database/master/assets/debugdb.png >

Editing values

<img src=https://raw.githubusercontent.com/amitshekhariitbhu/Android-Debug-Database/master/assets/debugdb_edit.png >

Working with emulator

  • Android Default Emulator: Run the command in the terminal - adb forward tcp:8080 tcp:8080 and open http://localhost:8080
  • Genymotion Emulator: Enable bridge from configure virtual device (option available in genymotion)

Getting address with toast, in case you missed the address log in logcat

As this library is auto-initialize, if you want to get the address log, add the following method and call (we have to do like this to avoid build error in release build as this library will not be included in the release build)

public static void showDebugDBAddressLogToast(Context context) {
    if (BuildConfig.DEBUG) {
       try {
            Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
            Method getAddressLog = debugDB.getMethod("getAddressLog");
            Object value = getAddressLog.invoke(null);
            Toast.makeText(context, (String) value, Toast.LENGTH_LONG).show();
       } catch (Exception ignore) {

       }
    }
}

Adding custom database files

As this library is auto-initialize, if you want to add custom database files, add the following method and call

public static void setCustomDatabaseFiles(Context context) {
    if (BuildConfig.DEBUG) {
        try {
            Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
            Class[] argTypes = new Class[]{HashMap.class};
            Method setCustomDatabaseFiles = debugDB.getMethod("setCustomDatabaseFiles", argTypes);
            HashMap<String, File> customDatabaseFiles = new HashMap<>();
            // set your custom database files
            customDatabaseFiles.put(ExtTestDBHelper.DATABASE_NAME,
                    new File(context.getFilesDir() + "/" + ExtTestDBHelper.DIR_NAME +
                            "/" + ExtTestDBHelper.DATABASE_NAME));
            setCustomDatabaseFiles.invoke(null, customDatabaseFiles);
        } catch (Exception ignore) {

        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容