自应用发布以来,我一直在使用Android Studio开发应用。
直到最近,一切都很好,我必须调试和检查数据库文件。由于我不知道如何直接看到数据库,当我调试生成数据库文件时,我不得不将数据库文件从我的手机导出到PC上。
为此,我必须打开DDMS >文件资源管理器。一旦我打开DDMS,我必须重新连接USB,我失去了调试线程。在检查数据库文件之后,我必须关闭DDMS并重新连接USB以回到调试模式。
这太复杂了。有人有更好的方法来做到这一点在Android Studio(我知道它更容易在Eclipse) ?
不使用模拟器时最简单的方法
$ adb shell
$ run-as your.package.name
$ chmod 777 databases
$ chmod 777 databases/database_name
$ exit
$ cp /data/data/your.package.name/databases/database_name /sdcard
$ run-as your.package.name # Optional
$ chmod 660 databases/database_name # Optional
$ chmod 660 databases # Optional
$ exit # Optional
$ exit
$ adb pull /sdcard/database_name
警告:
我有段时间没测试了。API>=25时可能不起作用。
如果cp命令不适合你,试试下面的命令:
# Pick a writeable directory <dir> other than /sdcard
$ cp /data/data/your.package.name/databases/database_name <dir>
# Exit and pull from the terminal on your PC
$ exit
$ adb pull /data/data/your.package.name/databases/database_name
解释:
第一个块将数据库的权限配置为可读。这利用了run-as,它允许您模拟包的用户来进行更改。
$ adb shell
$ run-as your.package.name
$ chmod 777 databases
$ chmod 777 databases/database_name
$ exit # Closes the shell started with run-as
接下来,我们将数据库复制到一个世界可读/可写目录。这允许adb拉用户访问。
$ cp /data/data/your.package.name/databases/database_name /sdcard
然后替换现有的读写权限。这对于您的应用程序的安全性很重要,但是特权将在下次安装时被替换。
$ run-as your.package.name
$ chmod 660 databases/database_name
$ chmod 660 databases
$ exit # Exit the shell started with run-as
最后,将数据库复制到本地磁盘。
$ exit # Exits shell on the mobile device (from adb shell)
$ adb pull /sdcard/database_name
Follow the above steps to open the database folder of your app in Android Device explorer and in there you can see six files, The first three is named as android and the last three is named as your database name. You only have to work with the last three files, save these three files at your preferred location. You can save these files by right-clicking to the file and click save as button (like I have my database named as room_database and so three files are named as room_database, room_database-shm, and room_database-wal. Rename the corresponding file as follows:-
1)从room_database到room_database.db
2) room_database-shm到room_database.db-shm
3) room_database-wal到room_database.db-wal
现在在任何SQLite浏览器中打开第一个文件,这三个文件都将被填充到浏览器中。
事实上,我很惊讶没有人给出这个解决方案:
看看Stetho。
为了不同的目的(其中之一是数据库检查),我在多个场合使用过Stetho。在实际的网站上,他们还讨论了网络检查和查看视图层次结构的功能。
它只需要一个小小的设置:1个gradle依赖项添加(你可以注释掉生产构建),几行代码来实例化Stetho,和一个Chrome浏览器(因为它使用Chrome devtools来做所有事情)。
更新:
您现在可以使用Stetho查看Realm文件(如果您使用Realm而不是SQLite DB): https://github.com/uPhyca/stetho-realm
更新2:
现在可以使用Stetho查看Couchbase文档:https://github.com/RobotPajamas/Stetho-Couchbase
更新# 3:
Facebook正致力于将Stetho的所有功能添加到其新工具Flipper中。Flipper已经具备了Stetho的许多功能。
所以,现在可能是做出改变的好时机。https://fbflipper.com/docs/stetho.html