How to push a file from computer to /data/.../databases/ using adb command?

Viewed 165

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?

3 Answers

There is no simple command for uploading the file. What Android Studio does when uploading a file using Device File Explorer is this:

  1. Upload the file via adb push to /data/local/tmp/<random file name>
  2. Execute adb shell run-as com.myapp sh -c 'cp /data/local/tmp/<random file name> /data/data/com.myapp/<path>/<final file-name>'
  3. Delete the temp file via adb shell rm /data/local/tmp/<random file name>
  4. 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>

Related