[General Topics] shoaibsaikat:
Though it's not necessary to build AOSP to build AOSP emulator, but I will discuss it here anyway.

Note: From here on we are considering, $HOME/AOSP is our AOSP repo download folder.

Android Build:

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

Everything are already explained in source.andorid clearly except some step. I am going to explain those.

1.	Follow this url, "https://source.android.com/setup/build/requirements".

2.	Replace branch name of,

repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1

by going "https://android.googlesource.com/platform/manifest" and select chosen branch. Download may take up to 20 hours or more.

3.	If not sure what argument to choose from lunch command, just type lunch and a bunch of options will be shown. Then select the correct one.

4.	Replace "make –jN" to the number of threads you have in your PC. For example for a 4 thread processor "make –j4" will be the command.

5.	If jack server has any memory issue run following command,

export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"

./prebuilts/sdk/tools/jack-admin kill-server

./prebuilts/sdk/tools/jack-admin start-server

Remember to replace –Xmx4g accordingly, here 4g means 4GB. Total build may take up to 9hrs.

6.	After that, for every incremental build, type source and lunch command then make command for fresh started terminal otherwise only make will do.

Adding Default Application to android Source:

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

1.	Create an application in Android Studio.

2.	We don’t need any gradle related folders for AOSP build. Take only java, assets and res folder and copy that in a new folder. Then take that folder to "AOSP/packages/apps" folder.

3.	Add "Android.mk" file to that folder and add following lines (replace AppName accordingly and if app has assets un-comment line starting with "LOCAL_ASSETS_DIR"),

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_PACKAGE_NAME := AppName

LOCAL_SDK_VERSION := current

LOCAL_SRC_FILES := $(call all-java-files-under, java)

# Include libraries

#LOCAL_JAVA_LIBRARIES := <Java lib dependencies>

LOCAL_STATIC_JAVA_LIBRARIES := android-common

LOCAL_STATIC_JAVA_LIBRARIES += android-support-v4

LOCAL_STATIC_JAVA_LIBRARIES += android-support-v7-appcompat

LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res

#LOCAL_ASSETS_DIR := $(LOCAL_PATH)/assets

LOCAL_AAPT_FLAGS := --auto-add-overlay

LOCAL_AAPT_FLAGS += --extra-packages android.support.v7.appcompat

include $(BUILD_PACKAGE)

4.	Don't use newly introduced layouts, which may create run-time exception (use linear, relative or other old layouts). Also use properly scoped style. For example,

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:id="@+id/textView"

android:layout_width="50dp"

android:layout_height="30dp"

android:layout_weight="1"

android:text="Hello World!"

android:textAppearance="@android:style/TextAppearance.Large" />

</LinearLayout>

In andorid studio "android:layout_width" may be written as "layout_width" and "@android:style/TextAppearance.Large" may be written as "TextAppearance.Large". We've to add full scope to every item, as AOSP build doesn't use gradle build system.

5.	Add app name to "PRODUCT_PACKAGES" variable of "AOSP/build/target/product/core.mk" and "AOSP/device/generic/armv7-a-neon/mini_common.mk" files.

6.	Build app using make appName then use make –jN or make snod.

NOTE: In emulator, settings page might crash. To fix this, go to "AOSP/packages/apps/Settings/src/com/android/settings/wfd/WifiDisplaySettgins.java", replace existing code by code below,

public static boolean isAvailable(Context context) {

return false;

//return context.getSystemService(Context.DISPLAY_SERVICE) != null

// && context.getSystemService(Context.WIFI_P2P_SERVICE) != null;

}

Emulator Build:

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

1.	Android emulator is build upon QEMU. Branch for QEMU on "https://android.googlesource.com" is named qemu and generic kernel is in goldfish branch. Note that though we can build goldfish kernel using simple make commands, we've used prebuilt kernel for our approach.

2.	Emulator source:

git clone https://android.googlesource.com/platform/external/qemu

(note there is another branch named qemu-android, this branch is not used any more so avoid cloning that branch)

Goldfish source:

git clone https://android.googlesource.com/device/generic/goldfish

(not needed to build emulator)

3.	Remember we've to use the appropriate branch after repo name for example for emulator we are using latest 2.8 version which is in branch, "emu-2.8-release". So our command will be,

git clone https://android.googlesource.com/platform/external/qemu-android -b emu-2.8-release

or we can switch to that branch after cloning for master.

4.	After cloning, copy the whole directory to "$HOME/AOSP/external" folder, rename older to qemu (if we've cloned qemu into our chosen named folder). We need this because qemu build uses "AOSP/external" and "AOSP/prebuilt" folder for its dependencies. By doing this we can avoid copying multiple repos for emulator, as we've already pulled it during AOSP cloning.

5.	Run,

./android/configure.sh

./configure

6.	We need to clone some other branches to build the emulator. Below branches should be copied to external folder,

git clone https://android.googlesource.com/platform/external/gtest	-b emu-2.8-release

git clone https://android.googlesource.com/platform/external/tinyobjloader	-b emu-2.8-release

put below branches in prebuilts/android-emulator-build folder,

git clone https://android.googlesource.com/platform/prebuilts/android-emulator-build/archive	-b emu-2.8-release

git clone https://android.googlesource.com/platform/prebuilts/android-emulator-build/common	-b emu-2.8-release

git clone https://android.googlesource.com/platform/prebuilts/android-emulator-build/curl	-b emu-2.8-release

git clone https://android.googlesource.com/platform/prebuilts/android-emulator-build/mesa	-b emu-2.8-release

git clone https://android.googlesource.com/platform/prebuilts/android-emulator-build/mesa-deps	-b emu-2.8-release

git clone https://android.googlesource.com/platform/prebuilts/android-emulator-build/protobuf	-b emu-2.8-release

git clone https://android.googlesource.com/platform/prebuilts/android-emulator-build/qemu-android-deps	-b emu-2.8-release

git clone https://android.googlesource.com/platform/prebuilts/android-emulator-build/qt	-b emu-2.8-release

also we may need below branch, (we've to put it in "prebuilts/clang/host/linux-x86" folder)

git clone https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86	-b emu-2.8-release

Note: if we look through git path of each branch they are same as AOSP folder structure. So we can get the hint, where to put them :)

If any ssl problem occures to download repo, run below command,

export GIT_SSL_NO_VERIFY=1

7.	Run "make -jN". if any other things are missing we will be shown the message and please clone those items from googlesource and put it in the correct location.

8.	If emulator build failed for warning the run this "./configure --disable-werror", then make again.

9.	After successful build we will get obs folder. Replace contents of "$home/AOSP/prebuilts/android-emulator/linux-x86_64" by the contents of our built folder.

10.	Go back to AOSP root folder, now if we run "emulator" command, our custom built emulator will be used from now on.

11.	We can run "emulator -verbose" to see generated commands.

Note: to buid AOSP again, we need to remove qemu, gtest, tinyobjloader from AOSP source, as they interfere with AOSP prebuilt packages. So after buiding emulator remove those folders from AOSP source folder.

Some Commands:

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

This commands may or may not be needed, depending on the status of Ubuntu or source code.

1.	Adding qt library in path

export PATH=/home/saikat/AOSP/prebuilts/android-emulator/linux-x86_64/lib64:/home/saikat/AOSP/prebuilts/android-emulator/linux-x86_64/lib64/gles_swiftshader:/home/saikat/AOSP/prebuilts/android-emulator/linux-x86_64/lib64/gles_angle:/home/saikat/AOSP/prebuilts/android-emulator/linux-x86_64/lib64/gles_angle9:/home/saikat/AOSP/prebuilts/android-emulator/linux-x86_64/lib64/gles_angle11:/home/saikat/AOSP/prebuilts/android-emulator/linux-x86_64/lib64/libstdc++:/home/saikat/AOSP/prebuilts/android-emulator/linux-x86_64/lib64/qt/lib:$PATH

export QT_QPA_PLATFORM_PLUGIN_PATH=/home/saikat/AOSP/prebuilts/android-emulator/linux-x86_64/lib64/qt/plugins

2.	Library installation commands

apt-get install build-essential zlib1g-dev pkg-config libglib2.0-dev binutils-dev libboost-all-dev autoconf libtool libssl-dev libpixman-1-dev libpython-dev python-pip python-capstone virtualenv

sudo apt-get install libsdl1.2-dev libsdl1.2debian libsdl-gfx1.2-5 libsdl-gfx1.2-dev libsdl-gfx1.2-doc libsdl-image1.2 libsdl-image1.2-dbg libsdl-image1.2-dev libsdl-mixer1.2 libsdl-mixer1.2-dbg libsdl-mixer1.2-dev libsdl-net1.2 libsdl-net1.2-dbg libsdl-net1.2-dev libsdl-sound1.2 libsdl-sound1.2-dev libsdl-ttf2.0-0 libsdl-ttf2.0-dev

sudo apt-get install libgtk-3-dev libspice-server-dev libusb-1.0-0-dev libusbredirparser-dev

3. QEMU default command, (not working just given for reference)

qemu-system-x86_64 \

-enable-kvm -smp 2 \

-append "console=ttyS0 vt.global_cursor_default=0 androidboot.selinux=permissive debug drm.debug=0 -device VGA" \

-m 1024 \

-serial mon:stdio \

-kernel $HOME/AOSP/out/target/product/generic_x86_64/kernel-ranchu \

-initrd $HOME/AOSP/out/target/product/generic_x86_64/ramdisk.img \

-drive index=0,if=none,id=system,file=$HOME/AOSP/out/target/product/generic_x86_64/system.img \

-device virtio-blk-pci,drive=system \

-drive index=1,if=none,id=cache,file=$HOME/AOSP/out/target/product/generic_x86_64/cache.img \

-device virtio-blk-pci,drive=cache \

-drive index=2,if=none,id=userdata,file=$HOME/AOSP/out/target/product/generic_x86_64/userdata.img \

-device virtio-blk-pci,drive=userdata \

-netdev user,id=mynet,hostfwd=tcp::5400-:5555 -device virtio-net-pci,netdev=mynet \

-device virtio-mouse-pci -device virtio-keyboard-pci \

-d guest_errors \

-nodefaults \

-display gtk,gl=on