In my android emulator, couple of fonts were missing and wanted to add these fonts and retain the change. I had a difficulty retaining the change in the emulator. Finally with lots of trial and error, I found a working instruction set.
Following ideas did not work for me
- Hacking qemu option for android emulator or getting the .tmp file and renaming as system.img (http://stackoverflow.com/questions/15417105/forcing-the-android-emulator-to-store-changes-to-system)
- snapshot option enabled
Audience
A programmer with a basic understanding of Android Studio, SDK Manager, AVD Manager, Android Device Monitor, Android Emulator, pushing/pulling a file to/from emulator using Android Device Monitor/adb command
Development Environment
- Windows 7 (/Linux/Mac)
- Android Studio 1.2.2
- Android SDK with emulator
- Virtual Device/Emulator (1 GB SD card setup is needed for this solution)
Path
- Android Studio – “C:\Program Files\Android\Android Studio”
- SDK Manager – Android Studio > Tools > Android > SDK Manager
- AVD Manager – Android Studio > Tools > Android > AVD Manager
- Android Device Monitor – Android Studio > Tools > Android > Android Device Monitor
- AVD Location – C:\Users\YOUR_USERNAME\.android\avd\AVD_NAME
- Emulator Base Image Location (path may change based on SDK API and processor type for emulator) – C:\Users\YOUR_USERNAME\AppData\Local\Android\sdk\system-images\android-22\default\armeabi-v7a\
- Virtual Device Temporary File – C:\Users\YOUR_USERNAME\AppData\Local\Temp\AndroidEmulator\RANDOM_FILE_NAME.tmp
- Emulator.exe – C:\Users\YOUR_USERNAME\AppData\Local\Android\sdk\tools
- adb.exe – C:\Users\YOUR_USERNAME\AppData\Local\Android\sdk\platform-tools
How Android Emulator Works
When we create virtual device using AVD Manager, it copies certain files EXCEPT “system.img” from Emulator Base Image Location to AVD Location. When the emulator is started from command prompt (Windows) or using AVD manager, it will get the “system.img” from Emulator Base Image Location and rest of the files like “cache.img”, “sdcard.img”, “userdata.img” and “userdata-qemu.img”.
If the “system.img” file exists at AVD Location, that file will be used rather than “system.img” file available at Emulator Base Image Location.
Emulator combines all “*.img” file and creates a Virtual Device Temporary File and work off of it. When you close the emulator, this file will be deleted.
Any changes to “cache.img”, “sdcard.img”, “userdata.img” and “userdata-qemu.img” will be stored but any changes to “system.img” will not be stored when the emulator is closed.
Goal
Once we make the change in “system.img” (i.e. /system folder) within emulator, extract the “system.img” out of emulator, and place it under AVD Location. This way, all our changes will be stored.
Android Emulator File System
Android Emulator comes in two different file system flavor.
- yaffs2
- ext4
Each “*.img” belongs to one of these file systems and both are not compatible with each other. Before we begin anything, we need to find out the file system that the Virtual Device uses.
How to Find Virtual Device File System
Run Virtual Device using command prompt (Windows) only and not using AVD Manager. Replace AVD_NAME with your virtual device name in the following command
emulator -avd AVD_NAME
Do not close command prompt. Once the virtual device is up and ready, issue following command
adb shell mount
In the output, locate the text similar to “/dev/block/mtdblock0 /system” and the next word (yaffs2/ext4) notifies the file system.
Now once you know the file system, you need to download following additional tools in your computer
Close/Exit virtual device. (Don’t keep it running and go to next step otherwise next steps will not work)
For File System YAFFS2
Download “mkfs.yaffs2.arm” (or mkfs.yaffs2.x86 if you are using x86 processor for your Virtual Device) from https://code.google.com/p/android-group-korea/downloads/list in your computer
Rename “mkfs.yaffs2.arm” to “mkfs.yaffs2”
For File System EXT4
Download “make_ext4fs” from http://web.djodjo.org/article/download/android/tools/arm/alltools in your computer
Download “EXT4 Unpacker” from http://sourceforge.net/projects/androidicsjbext/ (This tools may work ICS onwards only but I have not tested for older Android version. It is working for Lollipop)
Make Changes
In order to run Virtual Device, open a command prompt (Windows) and issue following command. Please note that you have to use command prompt (Windows) only and cannot use AVD Manager to run it. You must explicitly provide the -partition-size parameter
emulator.exe -avd AVD_NAME -partition-size 300 -no-snapshot-load
Now you are ready to make changes in /system folder which is nothing but “system.img” mounted as /system folder. You can view whole Android file structure using Android Device Monitor > File Explorer tab. As of now, whole android device is readonly. Using this File Explorer, you can pull the file to your computer but you cannot push the file from your computer to virtual device. (You will get an error in the console log of Android Device Monitor if you try it). So we are making the “/system” folder as readwrite. In the same command prompt (Windows), issue following command
adb remount
Run following commands to give certain folders full permission
adb shell chmod 777 /system
adb shell chmod 777 /system/*
adb shell chmod 777 /sdcard
adb shell chmod 777 /sdcard/*
At this moment, we are ready to make any change. We may copy the fonts/apks/binaries and do whatever we want with our Virtual Device. Once you are done, proceed to the next steps
If file system is YAFFS2
Push the file “mkfs.yaffs2” from your computer to “/system/bin” using Android Device Manager > File Explorer.
On the command prompt (Windows), issue following command to generate “system-new.img” out of modified “/system” folder. This command will create “system-new.img” file
adb shell mkfs.yaffs2 /system /sdcard/system-new.img
Let the command complete. It may take several minutes.
Pull the file “/sdcard/system-new.img” from Virtual Device using Android Device Manager > File Explorer to your computer at AVD Location
Rename “system-new.img” to “system.img” at AVD Location
If file system is EXT4
Push the file “make_ext4fs” from your computer to “/system/bin” using Android Device Manager > File Explorer
On the command prompt (Windows), issue following command to generate “system-new.img” out of modified “/system” folder. This command will create “system-new.img” file
adb shell make_ext4fs -s -l 512M -a system sdcard/system-new.img system/
Let the command complete. It may take several minutes.
Pull the file “/sdcard/system-new.img” from Virtual Device using Android Device Manager > File Explorer to your computer at AVD Location
Open the file “system-new.img” in the program “EXT4 Unpacker” that you downloaded.
Click on the button “Save as EXT4” which will ask your for location and file name. Provide AVD Location and give the file name “system.img”
Close “EXT4 Unpacker”
Delete file “system-new.img” from AVD Location
Close/Exit/Turn off Virtual Device
You are all set and your changes are now permanent. It is always a good idea to take a backup of “system.img” in case you want to delete the Virtual Device which will save you from hassle of going through these steps.
Leave a Reply