How to copy files to an Android emulator’s data directory with ‘adb push’

As an Android developer, you can normally use the adb push command to copy files from your computer’s hard drive to an Android device. However, I just ran into a problem where I couldn’t copy files to my Android emulator’s “data” directory, i.e., the /data/data filesystem. When I tried to copy a file using this command:

$ adb push foo.jpg /data/data/com.alvinalexander.myapp/files

I got this Android error:

failed to copy 'foo.jpg' to 'foo.jpg': Read-only file system

The solution

The solution to this problem was to re-mount the emulator’s filesystem as a read-write filesystem. I did that using the following series of commands. In the following sequence, I issued the first command from my Mac/Unix operating system shell, and then the subsequent commands were run inside the adb shell:

$ adb shell

root@generic_x86:/ # su

root@generic_x86:/ # mount -o rw,remount rootfs /

127|root@generic_x86:/ # ^D                                                   

127|root@generic_x86:/ #

127|root@generic_x86:/ # ^D

Now back at my local filesystem prompt, I was then able to copy my file to the Android emulator filesystem using the adb push command, like this:

$ adb push foo.jpg /data/data/com.alvinalexander.myapp/files
1282 KB/s (12321 bytes in 0.009s)

As that command infers, the file foo.jpg was located in my local Mac directory. The transfer rate and number of bytes output helps to show that the file was copied to the emulator successfully.

There may be other ways to copy files from your local computer’s filesystem onto the Android emulator’s filesystem, but I can confirm that (a) this approach works, and (b) I got the “read-only file system” error until I ran this series of commands.