Search code examples
androidadb

adb copy file from /sdcard/ to /data/data/package without root permission


I connect my android device to PC and connect through adb. What I want to do is copy a file from my PC to the internal storage of my app (/data/data/com.example.app/databases) without root or su. I first push the file from my PC to the /sdcard/ folder of my android device.

adb push database.sqlite /sdcard/

Then I want to copy that file to the internal storage of my app. I can succesfully run-as my app package and see the internal storage.

adb shell
$ run-as com.example.app
$ ls databases

This causes no permission issues. I can also view the /sdcard/ folder if i do not use run-as with no problem

adb shell
$ ls sdcard

The problem is that when I first use run-as I do not have access to /sdcard/ folder, and when I do not use run-as I can not access the internal storage folder. So, if I execute run-as and then execute this:

cp /sdcard/database.sqlite /data/data/com.example.app/databases/

I get this permission denied error:

cp: /data/data/com.example.app/databases/database.sqlite: Permission denied

I also tried withh dd but did not work:

dd if=/sdcard/database.sqlite of=/data/data/com.example.app/databases/
# output
dd: /sdcard/database.sqlite: Permission denied

How can I solve this problem? I have no root access nor superuser (su). I think I can achieve this without root because using android studio device file explorer I managed to upload the database in the same android device. I just want to do this using command line (Windows).

Any help would be appreciated :D


Solution

  • My suggestion is to use a different approach while copying database file into the app's private area.

    In particular, you can copy the file into /data/local/tmp with:

    adb push database.sqlite /data/local/tmp
    

    and then copy/move the db file into the app's databases folder with:

    adb shell run-as com.example.app cp /data/local/tmp/database.sqlite databases
    

    Hope this helps.