使用docker创建aosp编译环境

Posted 小砖整理

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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.04
MAINTAINER asmrobot asmrobot@hotmail.com

ADD sources.list /sources.list
RUN mv /sources.list /etc/apt/sources.list

# Update
RUN export DEBIAN_FRONTEND=noninteractive && \\
    dpkg --add-architecture i386 && \\
    apt-get update && \\ 
    apt-get -y install software-properties-common

# Install AOSP dependencies
RUN 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 JDK
RUN 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 tool
ADD repo /usr/local/bin/repo
RUN chmod +x /usr/local/bin/repo

# Set up workspace
RUN git config --global user.email "asmrobot@hotmail.com" && \\
    git config --global user.name "asmrobot" && \\
    git config --global color.ui auto

# Volumes for AOSP source
VOLUME ["/aosp"]
VOLUME ["/mirror"]

# Build commands must be run in the AOSP source tree
WORKDIR /aosp

# Volume for external app source
VOLUME ["/app"]

# Volume for build artifacts
VOLUME ["/artifacts"]

# Set up entrypoint while working around docker/hub-feedback#811
ADD aosp.sh /usr/local/bin/aosp.sh
RUN chmod +x /usr/local/bin/aosp.sh
RUN ln -s /usr/local/bin/aosp.sh /aosp.sh

# Show help by default
CMD ["/aosp.sh", "--help"]
jdk6
FROM ubuntu:14.04
MAINTAINER asmrobot asmrobot@hotmail.com

ADD sources.list /sources.list
RUN mv /sources.list /etc/apt/sources.list

# Update
RUN export DEBIAN_FRONTEND=noninteractive && \\
    dpkg --add-architecture i386 && \\
    apt-get update

# Install AOSP dependencies
RUN 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 JDK
RUN export DEBIAN_FRONTEND=noninteractive && \\
    apt-get -y install openjdk-7-jdk

# Install repo tool
ADD repo /usr/local/bin/repo
RUN chmod +x /usr/local/bin/repo

# Set up workspace
RUN git config --global user.email "asmrobot@hotmail.com" && \\
    git config --global user.name "asmrobot" && \\
    git config --global color.ui auto

# Volumes for AOSP source
VOLUME ["/aosp"]
VOLUME ["/mirror"]

# Build commands must be run in the AOSP source tree
WORKDIR /aosp

# Volume for external app source
VOLUME ["/app"]

# Volume for build artifacts
VOLUME ["/artifacts"]

# Set up entrypoint while working around docker/hub-feedback#811
ADD aosp.sh /usr/local/bin/aosp.sh
RUN chmod +x /usr/local/bin/aosp.sh
RUN ln -s /usr/local/bin/aosp.sh /aosp.sh

# Show help by default
CMD ["/aosp.sh", "--help"]
jdk7
FROM ubuntu:16.04
MAINTAINER asmrobot asmrobot@hotmail.com

ADD sources.list /sources.list
RUN mv /sources.list /etc/apt/sources.list

# Install AOSP dependencies
RUN 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 JDK
RUN export DEBIAN_FRONTEND=noninteractive && \\
    apt-get -y install openjdk-8-jdk

# Install repo tool
ADD repo /usr/local/bin/repo
RUN chmod +x /usr/local/bin/repo

# Set up workspace
RUN git config --global user.email "asmrobot@hotmail.com" && \\
    git config --global user.name "asmrobot" && \\
    git config --global color.ui auto

# Volumes for AOSP source
VOLUME ["/aosp"]
VOLUME ["/mirror"]

# Build commands must be run in the AOSP source tree
WORKDIR /aosp

# Volume for external app source
VOLUME ["/app"]

# Volume for build artifacts
VOLUME ["/artifacts"]

# Set up entrypoint while working around docker/hub-feedback#811
ADD aosp.sh /usr/local/bin/aosp.sh
RUN chmod +x /usr/local/bin/aosp.sh
RUN ln -s /usr/local/bin/aosp.sh /aosp.sh

# Show help by default
CMD ["/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_static

LOCAL_SRC_FILES:= \\
    test.cpp 


    



LOCAL_CFLAGS += -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION)
    
LOCAL_CFLAGS += -std=c++11          


LOCAL_MODULE:= test
LOCAL_MODULE_TAGS := optional
include $(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编译环境的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu 20.04编译AOSP 并启动模拟器

编译android源代码(aosp)

Repo下载AOSP源码:基于ubuntu22.04 环境配置

AOSP编译

AOSP 8.1.0 编译一些心得

AOSP 8.1.0 编译一些心得