Below command is to pull a file:
adb -d shell "run-as com.myapp cat /data/data/com.myapp/databases/file.db" > file.db
But how to push it back like Android Studio does via Device File Explorer?
There is no simple command for uploading the file. What Android Studio does when uploading a file using Device File Explorer is this:
Upload the file via adb push to /data/local/tmp/<random file name>
Execute adb shell run-as com.myapp sh -c 'cp /data/local/tmp/<random file name> /data/data/com.myapp/<path>/<final file-name>'
Delete the temp file via adb shell rm /data/local/tmp/<random file name>
Get the updated view for Device File Explorer using adb shell run-as com.myapp sh -c 'ls -al /data/data/com.myapp/<path>/'
I discovered this by capturing the adb traffic on TCP port 5027 using Wireshark. An interesting detail is that each command executed using adb shell command uses the form <command-to-be executed in adb shell> || echo ERR-ERR-ERR-ERR
From Robert's answer now I can do like this:
function dbpull() {
adb shell run-as "com.$1.debug" cat "/data/data/com.$1.debug/databases/$2.db" > "/Users/username/Desktop/$2.db"
}
function dbpush() {
adb push "/Users/username/Desktop/$1.db" "/sdcard/db/tmp/"
}
function dbpush2() {
adb shell run-as "com.$1.debug" cp "/sdcard/db/tmp/$2.db" "/data/data/com.$1.debug/databases/$2.db"
}
function dbcheck() {
adb shell run-as "com.$1.debug" ls -al "/data/data/com.$1.debug/databases/"
}
Just write above code lines in your .bash_profile and then call it in terminal.
dbpull myapp mydata
At this moment I prefer to use Visual Studio Code than Android Studio to develop my Android apps. So, I need to know more about commands in terminal, e.g. adb, gradle, etc.
I hope this would be useful for everyone.
Use this command
adb push <file_path> <android_device_path>
adb pull <android_device_path>
I want to retreive android database using run-as like in here, like this way :
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
but my database file has space like "my db", what kind of punctuation that I must used ?
/ or " " or ' ' or something else ?
You should use the quotation mark "
chmod 666 databases/"my db"
so I'm trying to run some ADB commands through a powershell script.
This is a simple example of what I am trying to do:
adb shell "
echo "in adb shell"
su root
echo "you are now root."
ls
cd /data/data
echo "in /data/data"
ls
"
I saw in a previous post to add the "" next to shell and at the bottom but that still didn't work for me. I can start the shell and the first ls works. but it just prints the rest of the commands out instead of doing them. The output I am getting looks like:
PS C:\Scripts> & .\test.ps1
: not found/sh:
in adb shell
su root
echo you are now root.
ls
cd /data/data
echo in /data/data
MSM8960_lpm.rc
acct
cache
.
.
.
ueventd.qcom.rc
ueventd.rc
vendor PS C:\Scripts>
Any help would be greatly appreciated. Thanks in advance!
Note: the path to ADB is within the powershell $env:path so the adb commands are working
I was able to get around this by doing:
adb shell "su -c '[cmd]; [cmd]' "
On a non-rooted android device, I can navigate to the data folder containing the database using the run-as command 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 copy or move command 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
By design user build of Android (that's what you have on your phone until you unlock the bootloader and flash the phone with userdebug or eng software) restricts access to the Internal Storage - every app can only access its own files. Fortunately for software developers not willing to root their phones Google provides a way to access the Internal Storage of debuggable versions of their packages using run-as command.
To download the /data/data/debuggable.app.package.name/databases/file from an Android 5.1+ device run the following command:
adb exec-out run-as debuggable.app.package.name cat databases/file > file
To download multiple files in a folder under the /data/data/debuggable.app.package.name/ at once - use tar:
adb exec-out run-as debuggable.app.package.name tar c databases/ > databases.tar
adb exec-out run-as debuggable.app.package.name tar c shared_prefs/ > shared_prefs.tar
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 .
If anyone looking for pulling database from debug application may use the procedure below:
search and open device file explorer
Select your handset and then browse to data/data directory
Now find your application package and go to databases folder. You can see the databases there and upon right click, you will get option
to save this in your drive.
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 \r characters that some devices output to the shell.
From here you can use a variety of CLI or GUI SQLite applications, such as sqlite3 or sqlitebrowser, to browse the contents of the database.
I couldn't get anything else to work for me but this:
adb shell
run-as package.name
cat /databases/databaseFileName.db > /sdcard/copiedDatabaseFileName.db
exit
exit
adb pull /sdcard/copiedDatabaseFileName.db /file/location/on/computer/
The first exit is to exit out of the run-as, the second exit is to exit out of adb shell to do the pull.
For app's debug version, it's very convenient to use command adb exec-out run-as xxx.yyy.zzz cat somefile > somefile to 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.
Much much simpler approach to download the file onto your local computer:
In your PC shell run:
adb -d shell 'run-as <package_name> cat /data/data/<package_name>/databases/<db_name>' > <local_file_name>
#!/bin/bash
#export for adb
export PATH=$PATH:/Users/userMe/Library/Android/sdk/platform-tools
export PATH=$PATH:/Users/userMe/Library/Android/sdk/tools
adb -d shell 'run-as com.android.app cp /data/data/com.android.app/files/db.realm /sdcard'
adb pull sdcard/db.realm /Users/userMe/Desktop/db
You can use this script for get Realm database.
The database file is emtpy when using adb run-as. This can be resolved by calling close() on the RoomDatabase instance. Call close() to let SQLite write its journal to disk.
I've created this button that closes the database connection on request: via GIPHY
Here is how to call close on the RoomDatabase instance.
Steps to pull app db(installed in debug mode) from device
Close DB connection if opened
Open cmd (command prompt) (Change dir to your adb path)
cd C:\Program Files (x86)\Android\android-sdk\platform-tools
(list the app files)
adb -d shell "run-as com.xyz.name ls
/data/data/com.xyz.name/files/"
(copy required file to sdcard)
adb -d shell "run-as com.xyz.name cp
/data/data/com.xyz.name/files/abc.db /sdcard/abc.db"
(copy from sdcard to machine adb folder)
adb pull /sdcard/abc.db
Open DB connection
Destination file path in my case C:\Users{userName}\AppData\Local\VirtualStore\Program Files (x86)\Android\android-sdk\platform-tools
Or Device storage
If someone is looking for another answer that can be used to retrieve Database as well as Shared Preferences then follow this step:
In your build.gradle file of your app add line
debugCompile 'com.amitshekhar.android:debug-db:1.0.0'
now when you run your app in non-release mode then your app will automatically open 8080 port from your device IP address make sure your device is connected via wifi and your laptop is sharing the same network. Now simply visit the url
http://your_mobile_device_ip:8080/
to watch all data of database along with shared preferences.
Here's a solution that works on a device running Android 5.1. The following example is for Windows.
You need sed (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.
I have created a script to mount partitions and do some stuff in my Android system. I saved the script as install.sh in the /bin folder of Android.
I want to call the script from ADB, which is itself called from a batch file on Windows, but it needs to be executed as root.
The first solution I tried was to call the script using
adb shell "su -c sh /bin/script.sh"
but it does not work as it gives me a shell access (with root permissions), but nothing is executed.
I also tried to call
adb root "sh /bin/script.sh"
but I got the following error
adbd cannot run as root in production builds
I then tried to write
su -c "command"
for all the commands which need a root access in my script, but I have the same problem.
When I run the script I only obtain a root shell and nothing is executed.
If I use the first solution by hand (e.g. I call adb shell su, then my script), it works. However the whole point is to automate the process, so that adb shell can be called from another script.
Do you have any idea of how I could achieve this ?
Thanks !
This works for me:
Create myscript.bat and put into it (note the single quotes around the commands to be executed in superuser mode):
adb shell "su -c 'command1; command2; command3'"
then run myscript.bat from a DOS shell.
Note: it doesn't appear that the the DOS line continuation character (^) works in this situation. In other words, the following doesn't work for me:
adb shell "su -c '^
command1; ^
command2; ^
command3'"
This results in "Syntax error: Unterminated quoted string"
This works :
adb shell echo command which needs root privileges \| su
If you need redirection:
adb shell echo 'echo anytext > /data/data/aforbiddenfolder/file' \| su
For "copying" a local file to an android path needing root privileges (but alocalfile must not contain '):
cat alocalfile | adb shell echo "echo '`cat`' > /data/data/aforbiddenfolder/file" \| su
If you have a better way (even for su versions which don't have -c), I am interested.
This works for me:
adb shell "su -c ./data/local/tcpdump-arm -s 0 -v -w /data/local/appxpress_dump.pcap"
I am not sure if I provided a solution or asked for a better one.
I wanted to run some 200 command in batch mode to be sent to adb
I followed this approach
adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "
and I saved them in a batch file
This command
adb shell "su -c 'command1; command2; command3'"
will not work beyond a certain max size . It did not work
error: service name too long
but it does not work as it gives me a shell access (with root permissions), but nothing is executed.
How do you know that you are given root permissions? I assume you are attempting to execute the script on a device? Has your device been rooted?
You may need to give execute permissions via chmod to the file.
chmod ugo=rwx /bin/script.sh
It appears that I was using a very simple version of su which did not accept the -c argument.
I copied another su which did work. AndyD is totally right though, so I am accepting his answer instead of mine :)