[GUIDE][COMPLETE] Build or Port MIUI ROM to Any Device
#CREDITS:- XDA

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!
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.
Let's start with PatchROM Method:-
Requirements:-
Prerequisites:-
Modifying boot.img
#CREDITS:- XDA
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!

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!!

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-PHP Code:
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.
- 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-Then after run this-PHP Code:
sudo apt-get updateQuote:sudo apt-get install bison build-essential curl flex git git-core gnupg gperf libesd0-dev liblz4-tool libncurses5-dev libsdl1.2-dev libwxgtk2.8-dev libxml2 libxml2-utils lzop openjdk-7-jdk openjdk-7-jre pngcrush schedtool squashfs-tools xsltproc zip zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev unzip lib32readline-gplv2-dev lib32z1-dev make python-networkx zlib1g-dev:i386 phablet-tools - All the necessary packages are now installed. Now the next step is to configure repository.
- In terminal,run following commands-PHP Code:
mkdir -p ~/binPHP Code:PATH=~/bin:$PATHPHP Code:cd ~/binPHP Code:curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repoPHP Code:chmod a+x ~/bin/repoPHP Code:mkdir ~/patchromPHP Code:cd ~/patchromPHP Code: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-Code:
git config --global user.email ENTER_YOUR_EMAIL_ID_HERECode:git config --global user.name ENTER_YOUR_REAL_NAME_HERE - Now it's time to download the MIUI source code! Run the following command in terminal-PHP Code:
repo init -u git://github.com/MiCode/patchrom.git -b miui-7 -m android-4.4.4.xmlPHP Code: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.PHP Code:
cd /home/stryder/PHP Code:mkdir taoshanPHP Code:cd taoshan - Now create a makefile for your device. If you want, you can make changes to this my makefile and use.You can copy the following content in makefile (Don't forget to make changes in it according to your device) and save it.PHP Code:
gedit makefileExplanations:-Code:# # 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
- '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.PHP Code:
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.PHP Code:
cd ~/patchromPHP Code:source build/envsetup.sh - Again move to your device folder and start MIUI framework patching.PHP Code:
cd /home/stryder/taoshanPHP Code: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 @JavierAlonso to fix rejects. Also see THIS guide.
- After fixing all the rejects, type following command to create a flashable zip of your MIUI ROM.or you can also usePHP Code:
make fullotaPHP Code: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.
Now, let's modify the boot.img from base ROM zip to it make compatible with our MIUI ROM.
Port MIUI from another device
- First of all, download any boot image kitchen or editor. I used Android Image Kitchen for Windows.
- Assuming you're using the same kitchen, Extract the files from Android Image Kitchen in a separate folder. Copy your base ROM boot.img in that folder.
- Now drag and drop the boot.img on unpackimg. The kernel files will be extracted in ramdisk and split_img folder.
- We have to modify the ramdisk so move to that. In sbin folder, you'll see the extracted files.
- Now there are two chances- First, Your ramdisk is already extracted with various folders and files. In that case ignore the next steps and directly modify in respective files using Notepad++.
- While in second case, you'll get the ramdisk as ramdisk.cpio file. If you get this the you'll have to move to Ubuntu again for extracting it.
- Now in Ubuntu, copy your ramdisk.cpio in a seperate folder (suppose in stryder folder) in your home directory.
- Now, open terminal and run the following command-[No need as superuser]PHP Code:
cd stryderPHP Code:cpio -i -F ramdisk.cpio - Some output will be shown in terminal as 2320 blocks or something like that!
- Now in your folder, you'll see the extracted ramdisk files.
- Now comes the actual modification.
- First of all, download attached zip and extract the files in appropriate folders. (If the folder is not present, add that folder).
- Open default.prop and edit the lines like this-PHP Code:
#
# ADDITIONAL_DEFAULT_PROPERTIES
# ro.adb.secure=0
ro.secure=0
ro.allow.mock.location=0
ro.debuggable=1
persist.sys.usb.config=mtp,adb
persist.service.adb.enable=1
persist.service.debuggable=1 - Save the changes and open init.cm.rc (In case of CyanogenMod) or init.pa.rc (In case of AOSPA). Add # before the import line like this-PHP Code:
# Superuser
#import /init.superuser.rc - Save this file and now open init.rc.
- At the end of init.rc file, add these lines-PHP Code:
#service for shelld service shelld /system/xbin/shelld
class main
#service for su service su_daemon /system/xbin/su --daemon
class main
oneshot
service lbesec /system/xbin/lbesec
class main
oneshot - Save this file and then open seapp_contexts.
- In that file, add at the end-PHP Code:
user=theme seinfo=platform domain=platform_app type=platform_app_data_file
user=backup seinfo=platform domain=platform_app type=platform_app_data_file
user=updater seinfo=platform domain=platform_app type=platform_app_data_file
user=theme_man domain=system_app type=system_data_file - Also modify these lines (if present) to look like this-PHP Code:
user=_app domain=untrusted_app type=app_data_file
user=_app seinfo=platform domain=platform_app type=app_data_file
user=_app seinfo=shared domain=shared_app type=app_data_file
user=_app seinfo=media domain=media_app type=app_data_file
user=_app seinfo=release domain=release_app type=app_data_file - Save this file and if present, open init.qcom.rc.
- And before this line (at the end)-PHP Code:
on property:sys.boot_completed=1 - Add these lines-PHP Code:
service shelld /system/xbin/shelld
class main - Save the file. Now it's time to repack your ramdisk.
- First, move your original ramdisk.cpio from the extracted folder to /home. That is, if your extracted ramdisk is inMove original ramdisk.cpio file to-PHP Code:
/home/stryder/PHP Code:/home/ - After that, run this command in terminal-PHP Code:
cpio -i -t -F ../ramdisk.cpio | cpio -o -H newc -O ../ramdisk_new.cpio - Now a new file ramdisk_new.cpio will be created. Take a look if it's of the same size as the original one just a few bytes difference.
- Also open this ramdisk_new.cpio and make sure you have the newly added files in it. (Ex. otad in bin folder etc.)
- Now move this ramdisk_new.cpio to previously extracted Android Image kitchen ramdisk/sbin/ directory.
- Delete the original ramdisk.cpio from it and rename the ramdisk_new.cpio to ramdisk.cpio.
- Now come back and directly run repackimg. The modified image_new will be generated. Rename it to boot and include this modified boot.img in your MIUI ROM zip. Now you can proceed to flash your MIUI ROM!
Special Thanks- @Zenamp
Requirements:-
Instructions:-
First of all, make 2 folders- 1. BASE ROM 2. PORT ROM

I hope you guys understood how to build and port MIUI. I wish you good luck! If you get stuck anywhere, If you become successful building MIUI, feel free to post here. Also if you succeed, I'll appreciate if you give me credits and add link to this thread in your ROM thread on XDA.
- 7-zip, Winrar or similar software
- Notepad++
Instructions:-
First of all, make 2 folders- 1. BASE ROM 2. PORT ROM
- What is Base ROM: It is the ROM which is built for your device. (AOSP or CM etc.)
- What is Port ROM: MIUI ROM of other device which you want to port to your device.
- NOTE- It's better if the port device and your device has same CPU architecture and specifications. If you couldn't find same specification device then try the device having almost similar specification of your device.
- Now extract the Base ROM zip in BASE ROM folder and Port ROM zip in PORT ROM folder.
- Next, Delete addon.d (If present), app, framework, fonts, media and priv-app folder in System folder of BASE ROM.
- Now copy addon.d (If present), app, framework, fonts, media and priv-app folder from system folder of PORT ROM to system folder of BASE ROM.
- Now open etc folder in both ROMs folder. Go to init.d folder of PORT and copy and replace any extra files except 00banner to init.d of BASE ROM.
- Now open permissions folder in etc of both ROMs and copy and replace all the permissions files from PORT ROM to your BASE ROM folder except handheld_core_hardware.xml and platform.xml. (In case of CM based BASE ROM, also don't replace org.cyanogenmod.hardware.xml).
- Also copy any extra folder available in PORT ROM etc/ to BASE ROM etc/ e.g. miui_feature folder.
- Now open your system_fonts.xml in etc folder of BASE ROM and copy this before </familyset> end of filePHP Code:
<family>
<nameset>
<name>miui</name>
</nameset>
<fileset>
<file>Miui-Regular.ttf</file>
<file>Miui-Bold.ttf</file>
</fileset>
</family>
<family>
<nameset>
<name>miuiex</name>
</nameset>
<fileset>
<file>MiuiEx-Regular.ttf</file>
<file>MiuiEx-Bold.ttf</file>
</fileset>
</family>
<family>
<nameset>
<name>miuiex-light</name>
</nameset>
<fileset>
<file>MiuiEx-Light.ttf</file>
</fileset>
</family> - Now copy all the extra files from PORT ROM bin, lib and xbin folder to BASE ROM respective folders. Remember, DO NOT REPLACE anything. Just copy and paste and skip the files with same name.
- Now, open build.prop of BASE ROM. You can change these value to anything you like-PHP Code:
ro.build.id= ro.build.display.id= ro.build.date= ro.build.date.utc= ro.product.locale.language=en
ro.product.locale.region=US
ro.cm.version= ro.modversion= - Now copy paste these values at the end of your BASE ROM build.prop. You can edit the fields but keep the structure same.You can look at PORT ROM build.prop for reference.PHP Code:
ro.build.description=devicename-user 4.4.2 yourname C2104XXUCNG1 release-keys
ro.build.fingerprint=devicemanufacturer/devicemodel/device:4.4.2/yourname/C2104XXUCNG1:user/release-keys
ro.product.model=Xperia L
ro.product.device=taoshan
ro.build.characteristics=default #MIUI_Features ro.miui.has_real_blur=0
ro.miui.has_handy_mode_sf=1
ro.config.sms_received_sound=FadeIn.ogg
ro.config.sms_delivered_sound=MessageComplete.ogg
ro.product.manufacturer=Xiaomi
ro.config.elder-ringtone=Angel.mp3
persist.sys.mitalk.enable=true
ro.miui.ui.version.code=5
ro.miui.ui.version.name=V7
persist.sys.strictmode.disable=true
ro.product.mod_device=yourdevice - Change these fields in BASE ROM build.prop according to PORT ROM build.prop-PHP Code:
ro.config.notification_sound= ro.config.alarm_alert= ro.config.ringtone= - If your device has On-screen buttons, then add this value at the end of BASE ROM build.prop-PHP Code:
qemu.hw.mainkeys=0 - Now we're done with build.prop.
- Next, open META-INF/com/google/android/updater-script from BASE ROM using Notepad++ and change, compare and replace all the symlink and set_metadata lines in updater-script according to PORT ROM updater-script.If some extra lines are there in BASE ROM updater-script, keep it as it is.
NOTE:-I have attached one sample updater-script with this post. Look below. You can refer from that if you feel any difficulty.
- Add these before unmount("/system"); at the end of updater-script-PHP Code:
set_perm(0, 1000, 06750, "/system/xbin/shelld"); run_program("/sbin/busybox", "mount", "/data"); package_extract_dir("data", "/data"); package_extract_file("META-INF/com/miui/relink", "/tmp/relink"); set_perm(0, 0, 0555, "/tmp/relink"); run_program("/tmp/relink"); delete("/tmp/relink"); set_perm_recursive(1000, 1000, 0755, 0644, "/data/miui"); set_metadata("/system/bin/debuggerd", "uid", 0, "gid", 2000, "mode", 0755, "capabilities", 0x0, "selabel", "u:object_r:system_file:s0"); set_metadata("/system/bin/debuggerd_vendor", "uid", 0, "gid", 2000, "mode", 0755, "capabilities", 0x0, "selabel", "u:object_r:debuggerd_exec:s0"); set_metadata("/system/xbin/su", "uid", 0, "gid", 2000, "mode", 06755, "capabilities", 0x0, "selabel", "u:object_r:su_exec:s0"); delete_recursive("/data/miui/preinstall_apps"); delete_recursive("/data/miui/cust/preinstall_apps"); ui_print("Installation finished...Enjoy!"); unmount("/data"); - Also copy META-INF/com/miui and data folder to BASE ROM from PORT ROM.
- Now modify your BASE ROM boot.img using the tutorial from previous post and put that new boot.img in BASE ROM folder.
- Finally, select META-INF, system, boot.img, file_contexts(If present) and data folder in BASE ROM.
- Right click and select Add to Archive. Select Zip. Type the name of zip. (Don't delete ".zip" at the end!) Click OK.
- Now you're ready to flash this zip from recovery.
- 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. Look HERE for fixing bootloop.

I hope you guys understood how to build and port MIUI. I wish you good luck! If you get stuck anywhere, If you become successful building MIUI, feel free to post here. Also if you succeed, I'll appreciate if you give me credits and add link to this thread in your ROM thread on XDA.

Comments
Post a Comment