[Android Software Development] STRYDER~007:
Introduction:-

Hey guys! Here's a complete tutorial for building or porting MIUI to your device! I know there are already some guides for it but I felt none of them were noob-friendly and many were confusing and missing some important instructions. So I decided to share this complete guide which will teach you how to build MIUI from scratch. This is a basic guide in which I have taken building latest MIUI v7 as example. So let's start! :D

POST 1- Guide to build MIUI using PatchROM Sources

POST 2- Modifying boot.img

POST 3- Guide to port MIUI from another device

Basically,There are two methods by which you can build MIUI ROM for your device.

Build from source using PatchROM Method

Port manually from another device which already has MIUI

Let's start with PatchROM Method:-

Requirements:-

Ubuntu 14.04 64-bit. (Installed as virtual machine like in VMware will also work)

Base ROM for your device.

NOTE-

Preferably take pure AOSP built for your device as base ROM rather than CyanogenMod. You'll get less rejects while patching MIUI framework. But CM works too! Your choice.. ;)

Apktool. You can use Advanced Apktool by BDFreak. It's easier to use.

Working brain and a lot of patience!! :D

Prerequisites:-

First of all, take framework-res.apk, framework.jar, framework2.jar, services.jar, android.policy.jar and telephony-common.jar from your base ROM zip.

Now using apktool, decompile all above files.

If you're using advanced apktool then go to 3-Out folder and rename the respective folders to-

framework-res
framework.jar.out
framework2.jar.out
services.jar.out
android.policy.jar.out
telephony-common.jar.out

We'll need these folders further for MIUI framework patching.

STEP-1: Preparing build environment

Switch to Ubuntu 14.04. We'll have to prepare ROM building environment.

Next step is to setup android sdk. Follow THIS guide. (Look at 2nd answer graphical image explanation. Follow till step 5.)

Now run the following commands as superuser from terminal-

sudo apt-get update

Then after run this-

All the necessary packages are now installed. Now the next step is to configure repository.

In terminal,run following commands-

mkdir -p ~/bin

PATH=~/bin:$PATH

cd ~/bin

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo

chmod a+x ~/bin/repo

mkdir ~/patchrom

cd ~/patchrom

repo init -u git://github.com/MiCode/patchrom.git -b miui-7 -m android-4.4.4.xml

Now you'll have to configure git. Run following commands in terminal-

git config --global user.email [COLOR="Red"]ENTER_YOUR_EMAIL_ID_HERE[/COLOR]

git config --global user.name [COLOR="Red"]ENTER_YOUR_REAL_NAME_HERE[/COLOR]

Now it's time to download the MIUI source code! Run the following command in terminal-

repo init -u git://github.com/MiCode/patchrom.git -b miui-7 -m android-4.4.4.xml

repo sync

Now grab a cup of coffee while the MIUI source gets downloaded. Required time depends on speed of your internet connection. The MIUI source is of size about 600-700 MB (With .repo total is 1.5 GB).

After the source is downloaded,create a new directory in /home. Name it whatever you want. (Preferably codename of your device) In my case, it's taoshan.

cd /home/stryder/

mkdir taoshan

cd taoshan

Now create a makefile for your device. If you want, you can make changes to this my makefile and use.

gedit makefile

You can copy the following content in makefile (Don't forget to make changes in it according to your device) and save it.

#
# Makefile for taoshan
#

# The original zip file, MUST be specified by each product
local-zip-file := stockrom.zip

# The output zip file of MIUI rom, the default is porting_miui.zip if not specified
local-out-zip-file := MIUI7_taoshan.zip

# All apps from original ZIP, but has smali files chanded
local-modified-apps :=

local-modified-priv-apps :=

local-modified-jars :=

# All apks from MIUI
local-miui-removed-apps :=

local-miui-removed-priv-apps :=

local-miui-modified-apps :=

# All apps need to be removed from original ZIP file
local-remove-apps := Gallery2 Exchange2

# Config density for co-developers to use the aaps with HDPI or XHDPI resource,
# Default configrations are HDPI for ics branch and XHDPI for jellybean branch
local-density := XHDPI

PORT_PRODUCT := taoshan

# To include the local targets before and after zip the final ZIP file,
# and the local-targets should:
# (1) be defined after including porting.mk if using any global variable(see porting.mk)
# (2) the name should be leaded with local- to prevent any conflict with global targets
#local-pre-zip := local-pre-zip-misc
#local-after-zip:= local-put-to-phone

# The local targets after the zip file is generated, could include 'zip2sd' to
# deliver the zip file to phone, or to customize other actions

#include phoneapps.mk

include $(PORT_BUILD)/porting.mk

local-pre-zip-misc:
@echo Update boot.img
cp other/boot.img $(ZIP_DIR)/boot.img

@echo Update build.prop
cp other/build.prop $(ZIP_DIR)/system/build.prop

@echo Delete some unneeded files
rm -rf $(ZIP_DIR)/system/etc/.has_su_daemon

rm -rf $(ZIP_DIR)/system/priv-app/Dialer.apk

rm -rf $(ZIP_DIR)/system/priv-app/Keyguard.apk

rm -rf $(ZIP_DIR)/system/priv-app/SystemUI.apk

rm -rf $(ZIP_DIR)/system/priv-app/VoiceDialer.apk

rm -rf $(ZIP_DIR)/system/priv-app/Launcher3.apk

rm -rf $(ZIP_DIR)/system/priv-app/ParanoidOTA.apk

rm -rf $(ZIP_DIR)/system/priv-app/ThemeChooser.apk

rm -rf $(ZIP_DIR)/system/priv-app/ThemesProvider.apk

Explanations:-

'local-zip-file' = Name of your original Base ROM zip

'local-out-zip-file' = Name for your finished MIUI rom

'local-modified-apps' = Apps that will be modified to fit in with the framework. They need to be modified because they contact with the system

'local-remove-apps' = Apps that will be removed from the original ROM

'local-pre-zip' = Refers to code-block that should be executed before the zip is done.

'local-after-zip' = Refers to code-block executed after the zip is completed

'local-zip-misc' = The code-block just described at 'local-pre-zip'. It adds some Google Apps and a build.prop

'local-test' = Another code-block

Now, manually copy the previously decompiled framework folders to your device folder.

framework-res
framework.jar.out
framework2.jar.out
services.jar.out
android.policy.jar.out
telephony-common.jar.out

Next,copy your Base ROM zip in your device folder.

Now move to patchrom folder.

cd ~/patchrom

source build/envsetup.sh

Again move to your device folder and start MIUI framework patching.

cd /home/stryder/taoshan

make firstpatch

Now a new folder folder temp will be created. In that you'll find the reject folder in which there will be rejects which you have to fix manually. Follow THIS excellent guide by [MENTION=5469171]JavierAlonso[/MENTION] to fix rejects. Also see THIS guide.

After fixing all the rejects, type following command to create a flashable zip of your MIUI ROM.

make fullota

or you can also use

make zipfile

With this, the flashable zip file is built. Now follow the tutorial from next post to modify your boot.img and include this new boot.img in your flashable MIUI ROM zip. Then flash the zip from recovery and enjoy your source built MIUI ROM!

Sometimes, your device might get stuck on bootanimation so in this case, take LOGCAT and look for the error and solve it. You can use some tools like Advanced Logcat Viewer to read the log. :)