首页 > 代码库 > 使用docker创建aosp编译环境

使用docker创建aosp编译环境

如果只需要编译一个版本的aosp,那么如果在主机上架建环境即可,可写的程序如果要在各个aosp版本中编译,那在主机上架建多个aosp编译环境可以会比较麻烦,github上找到一个openstf用docker架建aosp环境的代码,地址:https://github.com/openstf/docker-aosp

创建Dockerfile

aosp主要涉及三种环境jdk6,jdk7,jdk8,根据aosp的版本不同可以选择不同的Dockerfile

技术分享
FROM ubuntu:14.04MAINTAINER asmrobot asmrobot@hotmail.comADD sources.list /sources.listRUN mv /sources.list /etc/apt/sources.list# UpdateRUN export DEBIAN_FRONTEND=noninteractive &&     dpkg --add-architecture i386 &&     apt-get update && \     apt-get -y install software-properties-common# Install AOSP dependenciesRUN export DEBIAN_FRONTEND=noninteractive &&     apt-get -y install build-essential &&     apt-get -y install git gnupg ccache lzop flex bison gperf       build-essential zip curl zlib1g-dev zlib1g-dev:i386 libc6-dev       lib32bz2-1.0 lib32ncurses5-dev x11proto-core-dev libx11-dev:i386       libreadline6-dev:i386 lib32z1-dev libgl1-mesa-glx:i386       libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown       libxml2-utils xsltproc libreadline6-dev lib32readline-gplv2-dev       libncurses5-dev bzip2 libbz2-dev libbz2-1.0 libghc-bzlib-dev       lib32bz2-dev squashfs-tools pngcrush schedtool dpkg-dev &&     ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1       /usr/lib/i386-linux-gnu/libGL.so# Install JDKRUN export DEBIAN_FRONTEND=noninteractive &&     add-apt-repository ppa:webupd8team/java &&     apt-get update &&     echo debconf shared/accepted-oracle-license-v1-1 select true |       sudo debconf-set-selections &&     echo debconf shared/accepted-oracle-license-v1-1 seen true |       sudo debconf-set-selections &&     apt-get -y install oracle-java6-installer oracle-java6-set-default# Install repo toolADD repo /usr/local/bin/repoRUN chmod +x /usr/local/bin/repo# Set up workspaceRUN git config --global user.email "asmrobot@hotmail.com" &&     git config --global user.name "asmrobot" &&     git config --global color.ui auto# Volumes for AOSP sourceVOLUME ["/aosp"]VOLUME ["/mirror"]# Build commands must be run in the AOSP source treeWORKDIR /aosp# Volume for external app sourceVOLUME ["/app"]# Volume for build artifactsVOLUME ["/artifacts"]# Set up entrypoint while working around docker/hub-feedback#811ADD aosp.sh /usr/local/bin/aosp.shRUN chmod +x /usr/local/bin/aosp.shRUN ln -s /usr/local/bin/aosp.sh /aosp.sh# Show help by defaultCMD ["/aosp.sh", "--help"]
jdk6
技术分享
FROM ubuntu:14.04MAINTAINER asmrobot asmrobot@hotmail.comADD sources.list /sources.listRUN mv /sources.list /etc/apt/sources.list# UpdateRUN export DEBIAN_FRONTEND=noninteractive &&     dpkg --add-architecture i386 &&     apt-get update# Install AOSP dependenciesRUN export DEBIAN_FRONTEND=noninteractive &&     apt-get -y install build-essential &&     apt-get -y install git gnupg ccache lzop flex bison gperf       build-essential zip curl zlib1g-dev zlib1g-dev:i386 libc6-dev       lib32bz2-1.0 lib32ncurses5-dev x11proto-core-dev libx11-dev:i386       libreadline6-dev:i386 lib32z1-dev libgl1-mesa-glx:i386       libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown       libxml2-utils xsltproc libreadline6-dev lib32readline-gplv2-dev       libncurses5-dev bzip2 libbz2-dev libbz2-1.0 libghc-bzlib-dev       lib32bz2-dev squashfs-tools pngcrush schedtool dpkg-dev &&     ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1       /usr/lib/i386-linux-gnu/libGL.so# Install JDKRUN export DEBIAN_FRONTEND=noninteractive &&     apt-get -y install openjdk-7-jdk# Install repo toolADD repo /usr/local/bin/repoRUN chmod +x /usr/local/bin/repo# Set up workspaceRUN git config --global user.email "asmrobot@hotmail.com" &&     git config --global user.name "asmrobot" &&     git config --global color.ui auto# Volumes for AOSP sourceVOLUME ["/aosp"]VOLUME ["/mirror"]# Build commands must be run in the AOSP source treeWORKDIR /aosp# Volume for external app sourceVOLUME ["/app"]# Volume for build artifactsVOLUME ["/artifacts"]# Set up entrypoint while working around docker/hub-feedback#811ADD aosp.sh /usr/local/bin/aosp.shRUN chmod +x /usr/local/bin/aosp.shRUN ln -s /usr/local/bin/aosp.sh /aosp.sh# Show help by defaultCMD ["/aosp.sh", "--help"]
jdk7
技术分享
FROM ubuntu:16.04MAINTAINER asmrobot asmrobot@hotmail.comADD sources.list /sources.listRUN mv /sources.list /etc/apt/sources.list# Install AOSP dependenciesRUN export DEBIAN_FRONTEND=noninteractive &&     apt-get update &&     apt-get -y install git-core gnupg flex bison gperf build-essential       zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386       lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache       libgl1-mesa-dev libxml2-utils xsltproc unzip python# Install JDKRUN export DEBIAN_FRONTEND=noninteractive &&     apt-get -y install openjdk-8-jdk# Install repo toolADD repo /usr/local/bin/repoRUN chmod +x /usr/local/bin/repo# Set up workspaceRUN git config --global user.email "asmrobot@hotmail.com" &&     git config --global user.name "asmrobot" &&     git config --global color.ui auto# Volumes for AOSP sourceVOLUME ["/aosp"]VOLUME ["/mirror"]# Build commands must be run in the AOSP source treeWORKDIR /aosp# Volume for external app sourceVOLUME ["/app"]# Volume for build artifactsVOLUME ["/artifacts"]# Set up entrypoint while working around docker/hub-feedback#811ADD aosp.sh /usr/local/bin/aosp.shRUN chmod +x /usr/local/bin/aosp.shRUN ln -s /usr/local/bin/aosp.sh /aosp.sh# Show help by defaultCMD ["/aosp.sh", "--help"]
jdk8

三个版本中大部分代码都是一样的,只是每种环境安装的相应软件不同。在openstf之上改变的有

1.ubuntu的source.list,jdk6,7为163的源地址,jdk8为aliyun的。

2.repo中aosp的源码下载地址改为清华的repo:https://mirrors.tuna.tsinghua.edu.cn/git/git-repo

3.移除了原版中创建镜像的代码,减少了点参数。。。。参数太少看着眩晕,如果想创建私有aosp源可以在网上找资源

  镜像默认命令文件为/aosp.sh

  支持三种操作:    

checkout一个分支:checkout-branch <mirrorurl> <branch> [sync-options]
必须的volumes:
/aosp 你想要存放aosp源码的目录
Checks out 一个指定的分支到 /aosp.
编译全部全部:build-all <target> [make-options]
必须的volumes:
/aosp 存放aosp源码的目录
用你指定的target编译所有的/aosp 目录中的源码树。
编译指定模块:build <target> <appname> [make-options]
必须的volumes:
/app app存放代码的目录 (i.e.包含 Android.mk 的目录)
/aosp 存放aosp源码的目录
/artifacts app代码生成后的目录
用你指定的target编译/app源码,程序会先把/app复制到 /aosp/external/MY_<appname>
. <appname> 必须不有冲突

编译aosp前的准备

1.构建docker容器

  //切换到dockerfile目录

  cd /var/aosp-docker/jdk6

  //以dockerfile创建aosp/jdk6镜像

  sudo docker build -t aosp/jdk6 .

2.checkout出你需要的aosp源码

  sudo docker run --rm -ti -v /var/aosp-4.4.2:/aosp aosp/jdk6 /aosp.sh checkout-branch https://aosp.tuna.tsinghua.edu.cn/platform/manifest android-4.4.2_r2

  这里的源使用的清华的镜像,如果你有私用镜像改上下面镜像地址即可。  

分支和sdk level对应表
branch sdk level
android-4.4_r1 19
android-4.4.2_r2
android-4.4.4_r2.0.1
android-4.4w_r1 20
android-5.0.0_r1 21
android-5.0.2_r3
android-5.1.0_r1 22
android-5.1.1_r38
android-6.0.0_r1 23
android-6.0.1_r74
android-7.0.0_r1 24
android-7.1.0_r7
 
编译你的原生程序
  1.在基于aosp编译任何程序之前你必须要先整体编译一下aosp
    sudo docker run -ti --rm -v -v /var/aosp-4.4.2:/aosp aosp/jdk6 /aosp.sh build-all aosp_arm-eng
    以aosp_arm-eng目标类型编译aosp
    常见目标类型大概有以下几种:
aosp_arm-eng
aosp_x86-eng
aosp_mips-eng
vbox_x86-eng
aosp_hammerhead-userdebug
aosp_mako-userdebug
mini_mips-userdebug
mini_x86-userdebug
mini_armv7a_neon-userdebug
aosp_grouper-userdebug
aosp_tilapia-userdebug
aosp_flo-userdebug
aosp_deb-userdebug
aosp_manta-userdebug
2.编译你的程序
  以一个hello world为示例
  
技术分享
LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS) APP_STL := stlport_staticLOCAL_SRC_FILES:=     test.cpp     LOCAL_CFLAGS += -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION)    LOCAL_CFLAGS += -std=c++11          LOCAL_MODULE:= testLOCAL_MODULE_TAGS := optionalinclude $(BUILD_EXECUTABLE)
Android.mk
技术分享
#include <stdio.h>int main(int argc, char **argv){    printf("hello world!~");    return 0;}
test.cpp

docker run -ti --rm -v  /var/aosp-4.4.2:/aosp -v /home/xxxx/test:/app -v /home/xxxx/test-bin:/artifacts aosp/jdk6 /aosp.sh build aosp_arm-eng test

编译成功后出现:

技术分享

 

编译好的程序就在挂载/artifacts的目录中,我的是:/home/xxxx/test-bin

把test 用adb push 到手机上运行,如图:

  技术分享

 

我整理后的Dockerfile github:https://github.com/asmrobot/aosp-docker

使用docker创建aosp编译环境