2020-02-27

How to Copy File From Host to App Data Folder in the Android Emulator

You are developing an Android app with Android Studio.
And you want to copy some files from the Android emulator to your computer (I call it host).
You can do it easily with the Android Studio by using the Device File Explorer.

But the Device File Explorer does not allow to copy files in the opposite direction
(e.g. from the host the the emulator).

Here are the steps how to do this.

Open the Android Studio terminal.

Copy your file (here for example: MyFileToCopy.txt) to a "public accessible folder" on the android emulator (i.e. /sdcard/):
OS> adb push MyFileToCopy.txt /sdcard/
MyFileToCopy.txt: 1 file pushed. 0.1 MB/s (30 bytes in 0.016s)

Logon to the adb shell:
OS> adb shell
generic_x86_64:/ $

Change to "your App account" (here as example: ch.my.app-name):
127|generic_x86_64:/ $ run-as ch.my.app-name

generic_x86_64:/data/data/ch.my.app-name $

In the Android emulator, copy (or move) the file
from the "public accessible folder" (i.e. /sdcard/)
to "your App folder" (here for example: /data/data/ch.my.app-name/files/):
generic_x86_64:/data/data/ch.my.app-name $ 

cp /sdcard/MyFileToCopy.txt /data/data/ch.my.app-name/files/