Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing the Android emulator to store changes to /system

I know this is a recurrent question when working with the Android emulator, but is there a way to force the emulator to accept persistent changes to /system?

The emulator is based on QEMU, so it should be possible, in theory, to force the system image to behave the same way userdata (for instance) does, but I'm not familiar with how QEMU handles things. Any pointers?

like image 540
F.X. Avatar asked Mar 14 '13 18:03

F.X.


People also ask

Does Android Emulator save data?

On Android Studio 4.0 and above you can select Device File Explorer then select sdcard folder. Select a folder you want to save your files to, then right click and select upload. To download content from emulator select the folder or file and click save as option.

Where does the Android Emulator store files?

The android tools create an . android folder in your users home folder. This folder contains an avd folder in which a separate folder for each virtual device exists. In each of this folders a file called sdcard.

How do I change my device on emulator?

In Android Studio go to “Tools (Menu Bar) >Android > AVD Manager. Click on the “Create Virtual Device” button. Select “Phone” or “Tablet” as Category and select the device which you want to use to make a Virtual Device. Then click on the “Next” button.


4 Answers

At the moment (with Build Tools v26, if Google doesn't change things as often as they do) if you use the -writable-system directive while booting up the emulator from the command line, it will allow persistence to the /system partition through reboots. That is you will be able to write to the /system partition and if you reboot, the changes will still be maintained.

emulator -avd <AVD_NAME> -writable-system

This will also persist your changes to a qcow2 image file usually in .android/avd/<AVD_NAME>.avd/system.img.qcow2

You can even copy this system.img.qcow2 file, wipe the data off the AVD using the -wipe-data directive, place this file back into the directory, reboot and the system changes you made initially will still be persisted. (Caveat: at least for now, coz Google keeps changing things)

like image 119
Irvin H. Avatar answered Oct 18 '22 18:10

Irvin H.


Update: The -nand option was apparently removed from the most recent versions of QEMU, so the following may not work anymore.

Update: See the accepted answer for the current way of doing things.


Yury's answer is the common one, but if you want to be able to script things (which is what I want in the end), you need to be able to find the emulator image in the /tmp directory.

I discovered that you can override QEMU's behavior. This is a bit hackish, but it works, so I ended up doing this :

  1. Copy system.img from the platform directory to your AVD directory.
  2. Convert the amount of space you need to hex. For instance, if you need 500M, this is 0x1f400000.
  3. Run the emulator in read-write mode :

    emulator -avd Galaxy_Nexus -qemu -nand system,size=0x1f400000,file=/home/fx/.android/avd/Galaxy_Nexus/system.img
    

    (If you put the emulator in verbose mode, you'll notice that it will, by default, use initfile= instead of just file=)

  4. Make your changes, they are immediately saved to the local system.img

  5. Now you can just start the emulator with emulator -avd Galaxy_Nexus, and it'll use your changes

Update: If scripting, QEMU does not sync changes immediately to the image, so if you're rebooting immediately after changing something, chances are you will lose data. I'm looking for a way around this...

Next update: Use adb -e emu kill to stop the emulator. reboot will just do bad things.

like image 45
F.X. Avatar answered Oct 18 '22 16:10

F.X.


It's actually a very good question. I had the same troubles. Here are my findings. Actually, I use Ubuntu and I'll describe the steps for Ubuntu. If you use Windows, you should just change the paths.

  1. Create new AVD, for instance example.avd
  2. Copy system.img from android-sdk-linux/platforms/android-10/images to ~/.android/avd/example.avd
  3. Make system.img as writable and readable (either in the properties or simply using terminal)
  4. Run your AVD using command emulator -avd example
  5. Remount your system as rw using adb shell mount -o rw,remount -t yaffs2 /dev/block/mtd0 /system (to discover the partition use command cat /proc/mtd)
  6. Make your changes...
  7. Now during the run of emulator find tmp emulator in /tmp/android-<your_computer_name> with strange name like: emulator-PQ7LgE and copy it in ~/.android/avd/example.avd
  8. Delete system.img and rename copied tmp emulator into system.img
  9. Close emulator
  10. Delete cache.img, userdata.img and userdata-qemu.img from ~/.android/avd/example.avd
  11. Run your emulator once again

Good luck!

like image 42
Yury Avatar answered Oct 18 '22 18:10

Yury


The solution #2 is amazing.

Here are some hints if you are using MS Win as host AVD directories are located as below, but the "short" path names should be used as file= path parameter. The quoted path variant doesn't work for some reason.

Win XP: C:\Documents and Settings\ (username) \ .android\avd\ ... Short C:\DOCUME~1\ (username) \ANDROI~1\avd\ ...

Win 7 C:\Users\ (username) \ .android\avd\ ...

You can create an own bat file, say "startrw.bat" as per following example:

@echo off
C:\<ADTFOLDER>\sdk\tools\emulator -avd <AVDNAME> -qemu -nand system,size=0x1f400000,file=C:\DOCUME~1\<USERNAME>\ANDROI~1\avd\<AVDNAME>.AVD\system.img
cd C:\<ADTFOLDER>\sdk\platform-tools
echo .
echo Wait ... 
echo .
echo When emulator is FULLY loaded press any key to connect adb shell to it
echo To make /system writeable type in adb shell:
echo .
echo -----------------------------
echo mount -o rw,remount /system 
echo -----------------------------
echo .
echo You can use the Eclipse ADT DDMS File Browser to browse or push-pull files now.
echo .
echo Closing this window closes the emulator ! 
echo .
echo Wait emulator to load Android. When done
pause
C:\<ADTFOLDER>\sdk\platform-tools\adb shell

This way you can load in one click. Once finished modifying, just close the current command window to kill the emulator.

It takes long time for most of things to load like Emulator, the ADT editor, Hooking DDMS file browser (you need to click on the emulator line left side to see the files tree on right window) and so on.

like image 35
user2343527 Avatar answered Oct 18 '22 17:10

user2343527