Build MIUI from Source-

new in Development, Don’t Worry this Guide for You, here is a Step by Step Guide to build or Compile MIUI 8,7 ROM for any android Phone using source code and Patchrom Method.

Requirements:-

  1. Ubuntu 14.04 64-bit. (Installed as virtual machine like in VMware will also work)
  2. 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.. 
  3. Apktool. You can use Advanced Apktool by BDFreak. It’s easier to use.
  4. 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.

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-
    PHP Code:
    sudo apt-get update

    Then after run this-

    Quote:

    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 ~/bin
    PHP Code:
    PATH=~/bin:$PATH
    PHP Code:
    cd ~/bin
    PHP Code:
    curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    PHP Code:
    chmod a+x ~/bin/repo
    PHP Code:
    mkdir ~/patchrom
    PHP Code:
    cd ~/patchrom
    PHP 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_HERE
    Code:
    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.xml
    PHP 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 taoshan
    PHP Code:
    cd taoshan
  • Now create a makefile for your device. If you want, you can make changes to this my makefile and use.
    PHP Code:
    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.

    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

    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.
    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 ~/patchrom
    PHP Code:
    source build/envsetup.sh
  • Again move to your device folder and start MIUI framework patching.
    PHP Code:
    cd /home/stryder/taoshan
    PHP 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.
    PHP Code:
    make fullota

    or you can also use

    PHP 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.

Modifying boot.img

Now, let’s modify the boot.img from base ROM zip to it make compatible with our MIUI ROM.

  • 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 stryder
    PHP 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
    oneshotservice 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 in
    PHP Code:
    /home/stryder/

    Move original ramdisk.cpio file to-

    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!

Thanks to @STRYDER~007

Demo file- ramdisk_files.zip