android adb, retrieve database using run-as
2016-04-18 13:22
561 查看
adb shell and piping binary data
If you're unaware, running "adb shell" creates a pseudoterminal. This means that all LF characters are rewritten as CRLFs. If you're piping binary data back to your local machine, this will corrupt stdout.
For example, this results in a corrupted PNG file:
adb shell screencap -p > test.png
I was looking to fix this in adb, then noticed it was already done last year. There's an unadvertised exec service that bypasses the creation of the pseudoterminal. Invoke as follows:
adb exec-out screencap -p > test.png
Change was made by +Jeff Sharkey
https://android.googlesource.com/platform/system/core/+/5d9d434efadf1c535c7fea634d5306e18c68ef1f
5d9d434efadf1c535c7fea634d5306e18c68ef1f - platform/system/core - Git at Google
Add "exec" service: shell commands with no pty. To facilitate device scripts that want to read/write binary data from the host side, this change introduces a new "exec" service that behaves like "shell" but without creating
a pty, which would otherwise mangle binary data.
android.googlesource.com
156 plus ones
android
adb, retrieve database using run-as
up vote40down votefavorite 43 | One a non-rooted android device, I can navigate to the data folder containing the database using the run-ascommand with my package name. Most files types I am content with just viewing, but with the database I would like to pull if from the android device. Is there a download copyor movecommand from this part of adb shell? I would like to download the database file and view its content using a database browser. One answer here involves turning entire application package into a compressed archive, but there is no further answer on how to extract that archive once this is done and moved to the machine, leaving me very sidetracked when there might be a more direct solution to begin with thank you ![]() android database shell adb android-sqlite
| ||||||||
a comment | |||||||||
start a bounty |
6 Answers
activeoldestvotesup vote92down voteaccepted | adb shell "run-as package.name chmod 666 /data/data/package.name/databases/file" adb pull /data/data/package.name/databases/file . adb shell "run-as package.name chmod 600 /data/data/package.name/databases/file" UPDATE the command for Android 5.0+ to save /data/data/package.name/databases/file would be: adb exec-out run-as package.name cat databases/file > file
| ||||||||||||||||||||||||||||||||
a comment | show 7 more comments |
up vote26down vote | The accepted answer doesn't work anymore for me (blocked by Android?) So instead I did this: > adb shell shell $ run-as com.example.package shell $ chmod 666 databases/file shell $ exit ## exit out of 'run-as' shell $ cp /data/data/package.name/databases/file /sdcard/ shell $ run-as com.example.package shell $ chmod 600 databases/file > adb pull /sdcard/file .
| ||||||||||||||||||||||||||||||||
a comment | show 2 more comments |
up vote5down vote | I've published a simple shell script for dumping databases: https://github.com/Pixplicity/humpty-dumpty-android It performs two distinct methods described here: First, it tries to make the file accessible for other users, and attempting to pull it from the device. If that fails, it streams the contents of the file over the terminal to the local machine. It performs an additional trick to remove \rcharacters that some devices output to the shell. From here you can use a variety of CLI or GUI SQLite applications, such as sqlite3or sqlitebrowser, to browse the contents of the database.
| |||
add a comment |
up vote0down vote | Here's a solution that works on a device running Android 5.1. The following example is for Windows. You need sed ([]https://www.gnu.org/software/sed/manual/sed.html] or sed.exe on windows, e.g. from cygwin.) ( On Unix, it'll just be there ;) ). To remove bad '\r' characters, at least on windows. Now just run the following command: adb exec-out "run-as com.yourcompany.yourapp /data/data/com.yourcompany.yourapp/databases/YourDatabaseName" | c:\cygwin\bin\sed.exe 's/\x0D\x0A/\x0A/'>YourDatabaseName.db The sed command strips out trailing /r characters. Of course you should replace "com.yourcompany.yourapp" with the package name of the app and "YourDatabaseName" with the name of the database in the app.
| |||
add a comment |
up vote0down vote | For app's debug version, it's very convenient to use command adb exec-out run-as xxx.yyy.zzz cat somefile > somefileto extract a single file. But you have to do multiple times for multiple files. Here is a simple script I use to extract the directory. #!/bin/bash P= F= D= function usage() { echo "$(basename $0) [-f file] [-d directory] -p package" exit 1 } while getopts ":p:f:d:" opt do case $opt in p) P=$OPTARG echo package is $OPTARG ;; f) F=$OPTARG echo file is $OPTARG ;; d) D=$OPTARG echo directory is $OPTARG ;; \?) echo Unknown option -$OPTARG usage ;; \:) echo Required argument not found -$OPTARG usage ;; esac done [ x$P == x ] && { echo "package can not be empty" usage exit 1 } [[ x$F == x && x$D == x ]] && { echo "file or directory can not be empty" usage exit 1 } function file_type() { # use printf to avoid carriage return __t=$(adb shell run-as $P "sh -c \"[ -f $1 ] && printf f || printf d\"") echo $__t } function list_and_pull() { t=$(file_type $1) if [ $t == d ]; then for f in $(adb shell run-as $P ls $1) do # the carriage return output from adb shell should # be removed mkdir -p $(echo -e $1 |sed $'s/\r//') list_and_pull $(echo -e $1/$f |sed $'s/\r//') done else echo pull file $1 [ ! -e $(dirname $1) ] && mkdir -p $(dirname $1) $(adb exec-out run-as $P cat $1 > $1) fi } [ ! -z $D ] && list_and_pull $D [ ! -z $F ] && list_and_pull $F Hope it would be helpful. This script is also available at gist. Typical usage is $ ./exec_out.sh -p com.example.myapplication -d databases then it will extract all files under your apps databases directory, which is /data/data/com.example.myapplication/databases, into current directory.
| |||
add a comment |
up vote0down vote | why dont you just view the database within the adb shell ? in adb on most phones or on emulator call sqlite3 now run standard sql commands right on the prompt.
| ||||||||||||||
a comment |
相关文章推荐
- [Android][Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法
- 可以无限循环,自动旋转,停靠的3D旋转布局控件
- Android 之 Drawable
- 第四章--android核心组件之Activity与ListActivity中使用ListView的区别
- Android自定义View(一)
- Android 安全卫士 第二天
- android客户端学习-activity
- 读书笔记之Android绘图机制及图像处理值色彩处理的相关技巧
- Android 调用手机相册、摄像头拍照及剪裁照片
- 9patch背景导致控件内容异常
- Android——多线程
- Android之利用RemoteViews自定义Notification
- Android Studio安装配置详细步骤(图文)
- android studio 编译NDK android studio 生成.so文件
- Android开发-Sqlite操作
- Android中Bitmap,byte[],Drawable,InputStream相互转化工具类
- Activity之间切换(跳转)方向
- Android学习笔记之RatingBar1>简介 RatingBar为评分条控件,默认效果为若干个绿色的星星,如果想将其换成其他自定义图片就要自定义它的style。 RatingBar是SeekBa
- Android程序中访问sdcard
- Android开发中,那些让你相见恨晚的方法、类或接口