安卓调试sqlite-Android Debug Database

https://github.com/amitshekhariitbhu/Android-Debug-Database


安卓调试sqlite


Using Android Debug Database Library in your application

Add this to your app's build.gradle

debugImplementation'com.amitshekhar.android:debug-db:1.0.6'

Using the Android Debug Database with encrypted database

debugImplementation'com.amitshekhar.android:debug-db-encrypt:1.0.6'

Use debugImplementation 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")}


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) using reflection.

publicstaticvoidshowDebugDBAddressLogToast(Contextcontext) {if(BuildConfig.DEBUG) {try{Class<?>debugDB=Class.forName("com.amitshekhar.DebugDB");MethodgetAddressLog=debugDB.getMethod("getAddressLog");Objectvalue=getAddressLog.invoke(null);Toast.makeText(context, (String) value,Toast.LENGTH_LONG).show();      }catch(Exceptionignore) {      }    }}

Adding custom database files

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

publicstaticvoidsetCustomDatabaseFiles(Contextcontext) {if(BuildConfig.DEBUG) {try{Class<?>debugDB=Class.forName("com.amitshekhar.DebugDB");Class[] argTypes=newClass[]{HashMap.class};MethodsetCustomDatabaseFiles=debugDB.getMethod("setCustomDatabaseFiles", argTypes);HashMap>customDatabaseFiles=newHashMap<>();//set your custom database filescustomDatabaseFiles.put(ExtTestDBHelper.DATABASE_NAME,newPair<>(newFile(context.getFilesDir()+"/"+ExtTestDBHelper.DIR_NAME+"/"+ExtTestDBHelper.DATABASE_NAME),""));            setCustomDatabaseFiles.invoke(null, customDatabaseFiles);        }catch(Exceptionignore) {        }    }}

Adding InMemory Room databases

As this library is auto-initialize, if you want to debug inMemory Room databases, add the following method and call

publicstaticvoidsetInMemoryRoomDatabases(SupportSQLiteDatabase...database) {if(BuildConfig.DEBUG) {try{Class<?>debugDB=Class.forName("com.amitshekhar.DebugDB");Class[] argTypes=newClass[]{HashMap.class};HashMapinMemoryDatabases=newHashMap<>();//set your inMemory databasesinMemoryDatabases.put("InMemoryOne.db", database[0]);MethodsetRoomInMemoryDatabase=debugDB.getMethod("setInMemoryRoomDatabases", argTypes);            setRoomInMemoryDatabase.invoke(null, inMemoryDatabases);        }catch(Exceptionignore) {        }    }}

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