Howto Build Android 4.3 for Nexus 4

October 9, 2013

This post will walk through building and deploying Android 4.3 (Jelly Bean MR2) for a Nexus 4 device, from upstream sources.

Overview

A useful source tree, in practical terms, requires these components:

  1. Proprietary binaries for hardware support
  2. Proprietary Google Apps applications (e.g. Play store)
  3. The AOSP system image, recovery image, modem firmware, etc.
  4. A kernel

These four components are not bundled together, and need to be assembled into the Android build system.

Obtain the AOSP source

First, obtain the AOSP tree:

repo init \
    -u https://android.googlesource.com/platform/manifest \
    -b android-4.3_r1.1
repo sync -c -d -j10

Get the proprietary hardware support files

Now, get the proprietary hardware support files, and install them to the source tree:

cd $ANDROID_BUILD_TOP

wget https://dl.google.com/dl/android/aosp/broadcom-mako-jwr66y-137ef66d.tgz
wget https://dl.google.com/dl/android/aosp/lge-mako-jwr66y-a85ca75e.tgz
wget https://dl.google.com/dl/android/aosp/qcom-mako-jwr66y-a5becaf1.tgz

for item in *.tgz; do
    tar xzvf $item
done

for item in *.sh; do
    bash $item
done

# Manually accept all agreements to expand files into ./vendor tree.

rm *.sh *.tgz

Note: probably a good idea to stash these in a/some git repository/ies.

Setup the kernel

Now, get the kernel. Note that the version in the android-msm-mako-3.4-jb-mr2 branch actually has a display driver bug, and “tears” during scroll, out of box. The solution is to use the same revision as is in the stock kernel image.

git clone https://android.googlesource.com/kernel/msm kernel
cd kernel
git checkout f43c3d9dd98e351a98bbcccff5933061493f30a4

Alright — but it turns out that the AndroidKernel.mk, the main Makefile for the Android kernel, has some issues. The kernel config has modules disabled, but the makefile tries to build modules. You can comment out the offending lines with this sed command:

sed -i '/\t\+\$(.*module/s/^/#/' kernel/AndroidKernel.mk

Get the proprietary Google Apps

There are a couple ways to go about this, but an easy way is to download the proprietary files in a bundle from RootzWiki:

wget http://goo.im/gapps/gapps-jb-20130813-signed.zip

That package is intended to be use as an update.zip through a recovery session, once the phone is all loaded with an image. An alternative is to create Makefiles so that the files get included in the build. The key files are device-partial.mk, and Android.mk.

You can also cheat and just checkout that project, into your tree:

cd $ANDROID_BUILD_TOP
mkdir -p vendor/google
git clone \
    https://github.com/jamesonwilliams/vendor_google_mako \
    vendor/google/mako

A few modifications, to include the the added files

We now have all of the basic components we need to build, but still have to make a few tweaks to the build system. Firstly, we want to use the files we downloaded to ./vendor, so have to remove the RODUCT_RESTRICT_VENDOR_FILES directive:

sed -i '/PRODUCT_RESTRICT_VENDOR_FILES/s/^/#/' device/lge/mako/full_mako.mk

Similarly, we want to use the kernel source we added to the tree — not the prebuilt kernel image that comes with AOSP. The changes to support this can be viewed in this commit.

Lastly, we need to add a couple of lines so that the Google Apps files are included in the build. Changes to support this can be found in this commit.

Build the Thing

Alright. Assuming all of the above was followed carefully, we should be ready to build:

cd $ANDROID_BUILD_TOP
. build/envsetup.sh
lunch full_mako-user
make clean
make updatepackage

The update package ends up at $OUT/$TARGET_PRODUCT-img-$TARGET_BUILD_VARIANT.$USER.zip.

Deploy the Thing

Plug in your Nexus 4, ensure that you are able to do USB Debugging, and run:

adb reboot-bootloader
fastboot oem unlock
fastboot -w update $OUT/$TARGET_PRODUCT-img-$TARGET_BUILD_VARIANT.$USER.zip

At that point, the phone will reboot, and you’ll be running Android 4.3 on your Nexus 4.

25 responses to “Howto Build Android 4.3 for Nexus 4”

  1. […] Update: this is no longer the preferred approach. Building for Android Jelly Bean MR2 (4.3) is covered here: Howto Build Android 4.3 for Nexus 4 […]

  2. Chandra Prakash Gopalaiah says:

    In Step Get the proprietary Google Apps

    Below is a small change needs to be done for Google Apps to be included into your build.

    Just add below line at the end of file vendor/lge/mako/device-vendor.mk

    $(call inherit-product-if-exists, vendor/google/$(LOCAL_STEM))

  3. jamevu says:

    when build rom with this way.. when booting phone a red frame occur .. i do not know why ?

  4. Jameson says:

    @Jamevu: it’s because I have lunch full_mako-eng above. If you want to build a regular image, do full_mako-user. Actually, I’ll update the post to avoid confusion in the future.

  5. Chandra Prakash Gopalaiah says:

    @Jamevu – checkout this post in google group which might provide you more info on disabling red frame border on eng builds

    https://groups.google.com/forum/#!topic/android-x86/xhUxzF4kTjU

  6. Amit says:

    following your suggestions from past for 4.2. and 4.3 i am able to build now KitKat AOSP
    initial screenshots @ https://plus.google.com/u/0/109701627813546607713/posts

    Thanks,
    Amit

  7. Jameson says:

    @Amit — way to go! Haven’t gotten to it yet myself. :-).

  8. Yechiel says:

    Thank you very much for this how2!!!

  9. git says:

    Hi, a very nice guide. I thank you a lot. I am trying to build android-4.4_r1.1 aka Kitkat for the Nexus 4, since the binaries and the factory image are released now. Do you know if there is still a display driver bug and which branch/revision I should use for the kernel?
    Thanks in advance.

  10. Mike says:

    Hi, thank you for your information and my device is upgrading to 4.3.
    Will you share information how to build Android 4.4 (Kitkat) for Nexus 4?
    Thanks!

  11. Jameson says:

    @Mike I’m focusing my energies on Nexus 5 for now, but the procedure for mako kitkat is almost the same as here: http://nosemaj.org/howto-build-android-kitkat-nexus-5 .

  12. OGP says:

    I have a problem at the very end:
    “make updatepackage” fails with “make: *** No rule to make target ‘updatepackage’. Stop”.

    Any advice?

  13. biswa says:

    Hi which deconfig it takes. I want to enable perf with the build hence want to add some kernel config. Can you please help?

  14. Jameson says:

    @Biswa: For Nexus 4, the kernel defconfig is called mako_defconfig. See here for more details: http://source.android.com/source/building-kernels.html

  15. Biswa says:

    Hi Jameson,

    Thanks a lot for the help. Just wanted to ask an add on question if you know the answer. Will the perf tool be built by default when we build the kernel. Or we need to build that separately?

    Thanks,
    Biswa

  16. Jameson says:

    @Biswa: I’m not sure, I’ve never done much with perf.

  17. OGP says:

    Hi Jameson

    Do you plan to make instructions for Nexus 4 (not 5) and 4.4.2?

    Thanks

  18. Jameson says:

    @OGP: I have no plans to, but the changes are almost identical. Just make sure to pull the binaries out of the occam system image! 🙂 Probably wouldn’t hurt to get the Gapps that way too.

  19. biswa says:

    HI Jameson,
    I am getting this error?
    arm@benchmark-Test1:~/workspace2/biswa/android_src$ git checkout f43c3d9dd98e351a98bbcccff5933061493f30a4
    fatal: Not a git repository (or any of the parent directories): .git

    Please let me know if I am doing right?

    Thanks,
    Biswa

  20. biswa says:

    There is a new problem:

    arm@benchmark-Test1:~/workspace2/biswa/android_src$ git init
    Initialized empty Git repository in /home/arm/workspace2/biswa/android_src/.git/
    arm@benchmark-Test1:~/workspace2/biswa/android_src$ git checkout f43c3d9dd98e351a98bbcccff5933061493f30a4
    fatal: reference is not a tree: f43c3d9dd98e351a98bbcccff5933061493f30a4

  21. Jameson says:

    @Biswa: Ah yes, it looks like you need to cd ./kernel before checking out that kernel revision. Also, make sure you get rid of the .git project you created at the root of your tree (e.g. rm -r android_src/.git )

    Added the step to the instructions.

  22. AndroidUser says:

    Google play, & other google apps are not installed with the above procedure on nexus4

  23. AndroidUser says:

    thank you james for your reply now google apps installed but the device is very slow in booting & wifi is not working ..

  24. […] Valgrind is this magnificent monster when it comes to… well, most any thing you might want to do to your application or library. To get the most out of valgrind, you want debug symbols for your code. Easy, just compile AOSP from scratch! There are plenty of good posts on the subject; I went with this guide. […]

Leave a Reply