Linux(ubuntu14.04)上编译Android4.4源码的环境搭建及编译

1.下载并安装jdk6
  注意,版本一定要是jdk6,其他版本都会有问题。
  网上流传的方法大都是apt-get install来安装jdk,实际操作证明这样是不行的,下面给出个下载jdk6的地址
  https://jdk6.java.net/download.html  在这里根据自己机器配置情况选择自解压文件(self-extracting)就好了
  上面下载得到jdk-6u38-linux-x64.bin文件,改变属性使其拥有可执行权限。
  ./jdk-6u38-ea-bin-b04-linux-i586-31_oct_2012.bin即可安装

2.Linux配置jdk的环境变量

# set jdk environment  
export JAVA_HOME=/usr/local/java/jdk1.6.0_38
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH  
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH  
3.更新环境变量使其生效
 

 (1) 配置JAVA_HOME和PATH环境变量
$ sudo vi /etc/profile
在该文件的末尾加上如下部分:
JAVA_HOME=/usr/local/java/jdk1.6.0_45
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

(2) 配置ubuntu的JDK和JRE的位置
$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.6.0_45/bin/java" 1
$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.6.0_45/bin/javac" 1
$ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.6.0_45/bin/javaws" 1

(3) 配置Oracle为系统默认JDK/JRE
$ sudo update-alternatives --set java /usr/local/java/jdk1.6.0_45/bin/java
$ sudo update-alternatives --set javac /usr/local/java/jdk1.6.0_45/bin/javac
$ sudo update-alternatives --set javaws /usr/local/java/jdk1.6.0_45/bin/javaws

配置完成后,执行如下命令使其立即生效。
$ . /etc/profile


  java -version
  若能看到自己安装的jdk6的版本号则说明jdk环境变量已经配置成功

6.预先要安装的其他软件
  说明下,为保证编译过程中不老是缺少某些软件而中断,所以我们一定要做好准备工作,不打任何一场无准备的仗。
  网上很多前辈都已经给出来了不少要提前安装好的软件,下面我把它们集中整理出来。
  sudo apt-get install build-essential
  sudo apt-get install make
  sudo apt-get install gcc
  sudo apt-get install g++
  sudo apt-get install libc6-dev
  sudo apt-get install patch
  sudo apt-get install texinfo
  sudo apt-get install libncurses-dev
  sudo apt-get install git-core gnupg
  sudo apt-get install flex
  sudo apt-get install bison
  sudo apt-get install gperf
  sudo apt-get install libsdl-dev
  sudo apt-get install libesd0-dev
  sudo apt-get install libwxgtk2.6-dev
  sudo apt-get install build-essential
  sudo apt-get install zip
  sudo apt-get install curl
  sudo apt-get install ncurses-dev
  sudo apt-get install zlib1g-dev
  sudo apt-get install valgrind
  sudo apt-get install python2.5(该软件我没有安装成功,最后也没出什么问题)
  sudo apt-get install libgtk2.0-0:i386
  sudo apt-get install libpangox-1.0-0:i386
  sudo apt-get install libpangoxft-1.0-0:i386
  sudo apt-get install libidn11:i386
  sudo apt-get install gstreamer0.10-pulseaudio:i386
  sudo apt-get install gstreamer0.10-plugins-base:i386
  sudo apt-get install gstreamer0.10-plugins-good:i386
  sudo apt-get install gstreamer0.10-ffmpeg:i386(该软件我没有安装成功,最后也没出什么问题)
 
  再加个说明:上面的软件中最后几个以i386是因为一个错误(具体什么错误现在实在记不起来了),错误原因就是需要安装ia32-libs的软件包64位Linux(Ubuntu)上编译Android源码时还需要一个32位Linux的包,所以需要这个ia32-libs的支持包,但这个ia32-libs的软件包在网上根本没有找到,也没发现实际可用的解决方案,但从Ubuntu官网上来看,这个ia32-libs其实就是若干软件包的合集,所以下载安装其中的那么几个软件包就可以达到我们的目的。
 
7.正式编译Android源码
  做了那么多的准备工作,现在终于可以做正事了。根据Android官网的说法,编译Android源码执行下面三个命令即可

  ①source build/envsetup.sh

including device/asus/tilapia/vendorsetup.sh
including device/asus/flo/vendorsetup.sh
including device/asus/grouper/vendorsetup.sh
including device/asus/deb/vendorsetup.sh
including device/generic/armv7-a-neon/vendorsetup.sh
including device/generic/mips/vendorsetup.sh
including device/generic/x86/vendorsetup.sh
including device/samsung/manta/vendorsetup.sh
including device/lge/hammerhead/vendorsetup.sh
including device/lge/mako/vendorsetup.sh
including sdk/bash_completion/adb.bash
yetmare@yetmare-QiTianM4500-N000:~/AOSP$ lunch full-eng

============================================


  ②lunch full-eng

    此时要选择一个编译平台

PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.4
TARGET_PRODUCT=full
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a
TARGET_CPU_VARIANT=generic
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.19.0-25-generic-x86_64-with-Ubuntu-14.04-trusty
HOST_BUILD_TYPE=release
BUILD_ID=KRT16M
OUT_DIR=out
============================================


  ③make -j4
    这里的n注意取值,网上一般说法是CPU核数的两倍,还有一说是CPU核数加1。到底哪个更准确我也没有验证。
    但要值得提醒的是,虽然n用一个大的值会使编译速度加快,但如果n取值过大,编译是会报错的。
    如果实在不确定n应该用什么值,那就直接用make命令吧。
   
    从编译开始到编译结束,中间仍有可能有错哦,所有不要掉以轻心,随时看着点,以便及时解决掉。注意:解决掉错误以后,重复上面的3小步,会接着编译。我是在Window7下安装的Ubuntu14.04,分配空间是100G,直接用的make -j4命令,编译2个多小时吧

编译结果如下:

Processing target/product/generic/obj/APPS/Email_intermediates/package.apk
Done!
Install: out/target/product/generic/system/app/Email.odex
Install: out/target/product/generic/system/app/Email.apk
target Symbolic: libwebviewchromium (out/target/product/generic/symbols/system/lib/libwebviewchromium.so)
target Strip: libwebviewchromium (out/target/product/generic/obj/lib/libwebviewchromium.so)
Install: out/target/product/generic/system/lib/libwebviewchromium.so
Install: out/target/product/generic/system/framework/webviewchromium.jar
Install: out/target/product/generic/fake_packages/webview-timestamp
build/tools/generate-notice-files.py  out/target/product/generic/obj/NOTICE.txt  out/target/product/generic/obj/NOTICE.html "Notices for files contained in the filesystem images in this directory:" out/target/product/generic/obj/NOTICE_FILES/src
Combining NOTICE files into HTML
Combining NOTICE files into text
Installed file list: out/target/product/generic/installed-files.txt
Target system fs image: out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img
Running:  mkuserimg.sh out/target/product/generic/system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img ext4 system 576716800 out/target/product/generic/root/file_contexts
+ echo 'in mkuserimg.sh PATH=out/host/linux-x86/bin/:/usr/local/java/jdk1.6.0_38/bin:/home/yetmare/ASOP/out/host/linux-x86/bin:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/mips/mipsel-linux-android-4.7/bin:/home/yetmare/ASOP/development/emulator/qtools:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin:/home/yetmare/ASOP/development/scripts:/home/yetmare/ASOP/prebuilts/devtools/tools:/usr/local/java/jdk1.6.0_38/bin:/usr/local/java/jdk1.6.0_38/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games'
in mkuserimg.sh PATH=out/host/linux-x86/bin/:/usr/local/java/jdk1.6.0_38/bin:/home/yetmare/ASOP/out/host/linux-x86/bin:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/mips/mipsel-linux-android-4.7/bin:/home/yetmare/ASOP/development/emulator/qtools:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin:/home/yetmare/ASOP/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin:/home/yetmare/ASOP/development/scripts:/home/yetmare/ASOP/prebuilts/devtools/tools:/usr/local/java/jdk1.6.0_38/bin:/usr/local/java/jdk1.6.0_38/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
+ ENABLE_SPARSE_IMAGE=
+ '[' out/target/product/generic/system = -s ']'
+ '[' 6 -ne 5 -a 6 -ne 6 ']'
+ SRC_DIR=out/target/product/generic/system
+ '[' '!' -d out/target/product/generic/system ']'
+ OUTPUT_FILE=out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img
+ EXT_VARIANT=ext4
+ MOUNT_POINT=system
+ SIZE=576716800
+ FC=out/target/product/generic/root/file_contexts
+ case $EXT_VARIANT in
+ '[' -z system ']'
+ '[' -z 576716800 ']'
+ '[' -n out/target/product/generic/root/file_contexts ']'
+ FCOPT='-S out/target/product/generic/root/file_contexts'
+ MAKE_EXT4FS_CMD='make_ext4fs  -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system'
+ echo make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system
make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system
+ make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system
Creating filesystem with parameters:
    Size: 576716800
    Block size: 4096
    Blocks per group: 32768
    Inodes per group: 7040
    Inode size: 256
    Journal blocks: 2200
    Label: 
    Blocks: 140800
    Block groups: 5
    Reserved block group size: 39
Created filesystem with 1263/35200 inodes and 81851/140800 blocks
+ '[' 0 -ne 0 ']'
Install system fs image: out/target/product/generic/system.img
out/target/product/generic/system.img+ maxsize=588791808 blocksize=2112 total=576716800 reserve=5947392
    

至此,Linux(ubuntu14.04)上编译Android源码的环境搭建及编译全过程已经结束了。

8.在模拟器中运行

在模拟器中运行的步骤就比较简单了,只需在终端中执行如下命令即可:

emulator
运行成功后的界面如下:

Linux(ubuntu14.04)上编译Android4.4源码的环境搭建及编译_第1张图片

最后在赠送一个Ubuntu上安装7z软件包的命令及使用方法

安装7z:apt-get install p7zip-full
使用7z:7z x file file是要解压的文件名
转自:jileniao.net/linux-android-building.html

你可能感兴趣的:(linux)