[Android General] Nrin:
Hi all,

As the title says, the question is how to test a custom rom (the zip file that you should flash on your device) on an Android emulator. I opened this thread because also if this is a common problem with several threads about it (also here on xda), there isn't a real answer, i.e. a working method. I think I'm close to the solution, but it still doesn't work. So, I decided to share here my knowledge with the hope that someone can help me. This method is only for Linux.

Initial setup

Download and open Android Studio. Go to Tools -> Android -> AVD Manager. Create a new device with the same specs of your device. This means that you should choose the same screen size/resolution and the right cpu architecture. Android Studio suggests to install x86 images, because the emulation is faster. Nowadays, the most common architecture on smartphones is armeabi-v7a, so choose this one. I'll use as example a device with Android 4.2 (armeabi-v7a).

Replace system.img

If you go into ~/Android/Sdk/system-images folder, you'll find the folders related to the images that you've installed previously (in the initial setup). In my case I have ~/Android/Sdk/system-images/android-17/google_apis/armeabi-v7a/ which contains system.img. The system.img is the system partition. It's an ext4 filesystem image that you can mount in a folder (i.e. it's a way to "extract" its content):

$ mkdir system
# mount -o loop system.img system

Now you can edit its content, e.g. replacing some files. When you've done, you can unmount the image with the following command:

# umount /absolute/path/to/the/system.img

Our goal is to replace the system.img file with the system.img of our custom rom. Unzip the custom rom. If you go into its folder, you'll see the system folder that we need to transform into system.img.

First of all, we should rename the original system.img file (the stock android image):

$ cd ~/Android/Sdk/system-images/android-17/google_apis/armeabi-v7a
$ mv system.img original_system.img

Now we'll create a new ext4 filesystem (with the same number of blocks) where we'll put the content of the system folder of our custom rom.

$ dd if=/dev/null of=system.img bs=1M seek=550
$ mkfs.ext4 -F system.img
$ mkdir custom_system
# mount -o loop system.img custom_system
# cp -r /path/to/our/custom/rom/system/* custom_system/
# chmod 755 -R custom_system/
# umount /absolute/path/to/system.img

Note that we are using seek=550, because if the original system.img has 140800 blocks with a total size of 576.7 MB, we need to allocate the same amount of space. In fact, also our (custom) system.img will have a size of 576.7 MB with the same number of blocks. The last command (umount) isn't mandatory, you can proceed keeping mounted the image.

Start the emulator

At this point we should execute the emulator hoping that our system.img is ok.

Run the emulator following this command (I've a 64 bit system):

$ LD_LIBRARY_PATH=~/Android/Sdk/tools/emulator/lib64 ~/Android/Sdk/tools/emulator/emulator64-arm -show-kernel -avd Name_of_your_avd

I defined LD_LIBRARY_PATH because otherwise it can't find the libraries for the gpu emulation, an alternative solution is to use -gpu off to disable gpu emulation. The -show-kernel parameter is useful to see if there is some problem while the device is booting, instead of -debug-all that is useful to see all the stuff related to the emulator itself and not to the operating system (Android).

-----------------

Small Off-Topic:

Note that on my Linux system to start the emulator for an x86 image I should use:

LD_PRELOAD="/usr/lib/libstdc++.so.6" ./emulator -avd Avd_Name

--------------

In my case, it seems there is some problem during the boot (black screen all the time). The kernel messages are:

I created the system.img from a custom rom for huawei u9508 based on Android 4.2.2. I hope this post could be useful.