Ubuntu下Android源码以及内核下载与编译

1018787246 8年前
   <p>本教程是基于Ubuntu下Android6.0.1源码以及内核的下载和编译,记录一下,以后也就不用自己去找资料,一遍一遍的尝试了。可以KX上网的,英语好的,直接去 <a href="/misc/goto?guid=4959730047578884112" rel="nofollow,noindex">AndroidSource</a> .</p>    <ul>     <li>系统环境:Ubuntu14.04LTS</li>     <li>Android版本:6.0.1</li>     <li>重要网址<br> <a href="/misc/goto?guid=4959730047676472363" rel="nofollow,noindex">清华大学镜像</a><br> <a href="/misc/goto?guid=4959730047578884112" rel="nofollow,noindex">AndroidSource</a></li>    </ul>    <h2>下载前的准备</h2>    <ul>     <li> <p>安装OpenJdk</p> <pre>  sudo add-apt-repository ppa:openjdk-r/ppa  sudo apt-get update  sudo apt-get install openjdk-8-jdk  sudo update-alternatives --config java  java -version  </pre> </li>     <li> <p>安装git</p> <pre>  sudo apt-get install git-core  </pre> </li>     <li> <p>安装额外的组建</p> <pre>  sudo apt-get install 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  </pre> </li>    </ul>    <table>     <thead>      <tr>       <th>组建</th>       <th>功能</th>       <th>介绍网址</th>      </tr>     </thead>     <tbody>      <tr>       <td>gnupg</td>       <td>加密工具</td>       <td><a href="/misc/goto?guid=4958879096392807186" rel="nofollow,noindex">GPG入门教程</a></td>      </tr>      <tr>       <td>flex</td>       <td>The Fast Lexical Analyzer</td>       <td><a href="/misc/goto?guid=4959730047804087584" rel="nofollow,noindex">快速的语法分析工具</a></td>      </tr>      <tr>       <td>bison</td>       <td>用于自动生成语法分析器程序</td>       <td><a href="/misc/goto?guid=4959730047886061930" rel="nofollow,noindex">自动生成语法分析器程序</a></td>      </tr>      <tr>       <td>gperf</td>       <td>完美的散列函数生成器</td>       <td><a href="/misc/goto?guid=4959730047975588619" rel="nofollow,noindex">使用 gperf 实现高效的 C/C++ 命令行处理</a></td>      </tr>      <tr>       <td>build-essential</td>       <td>编译内核中make menuconfig进图形编译</td>       <td><a href="/misc/goto?guid=4959730048052824453" rel="nofollow,noindex">build-essential</a></td>      </tr>      <tr>       <td>zip</td>       <td>Linux 下zip包的压缩与解压</td>       <td><a href="/misc/goto?guid=4959730048143877670" rel="nofollow,noindex">Linux 下zip包的压缩与解压</a></td>      </tr>      <tr>       <td>curl</td>       <td>网络请求和提取工具</td>       <td><a href="/misc/goto?guid=4958188997903938385" rel="nofollow,noindex">curl网站开发指南</a></td>      </tr>      <tr>       <td>zlib1g-dev</td>       <td>用于发现gzip和PKZIP的工具</td>       <td><a href="/misc/goto?guid=4959730048265490926" rel="nofollow,noindex">Binary package “zlib1g-dev” in ubuntu trusty</a></td>      </tr>      <tr>       <td>gcc-multilib</td>       <td>允许在64位机器中运行32位应用</td>       <td><a href="/misc/goto?guid=4959730048352675480" rel="nofollow,noindex">multilib</a></td>      </tr>      <tr>       <td>g++-multilib</td>       <td>同上(g++编译工具)</td>       <td><a href="/misc/goto?guid=4959730048445479510" rel="nofollow,noindex">多平台支持</a></td>      </tr>      <tr>       <td>libc6-dev-i386</td>       <td>Embedded GNU C Library: 32-bit development libraries for AMD64</td>       <td><a href="/misc/goto?guid=4959730048519846838" rel="nofollow,noindex">libc6-dev-i386</a></td>      </tr>      <tr>       <td>lib32ncurses5-dev</td>       <td>待完善</td>       <td>待完善</td>      </tr>      <tr>       <td>x11proto-core-dev</td>       <td>待完善</td>       <td>待完善</td>      </tr>      <tr>       <td>libx11-dev</td>       <td>待完善</td>       <td>待完善</td>      </tr>      <tr>       <td>lib32z-dev</td>       <td>待完善</td>       <td>待完善</td>      </tr>      <tr>       <td>ccache</td>       <td>待完善</td>       <td>待完善</td>      </tr>      <tr>       <td>libgl1-mesa-dev</td>       <td>待完善</td>       <td>待完善</td>      </tr>      <tr>       <td>libxml2-utils</td>       <td>待完善</td>       <td>待完善</td>      </tr>      <tr>       <td>xsltproc</td>       <td>待完善</td>       <td>待完善</td>      </tr>      <tr>       <td>unzip</td>       <td>待完善</td>       <td>待完善</td>      </tr>     </tbody>    </table>    <h2>下载</h2>    <p>因为网络以及墙的原因,我们使用清华大学镜像,步骤如下:</p>    <ul>     <li> <p>下载repo工具</p> <pre>  mkdir ~/bin  PATH=~/bin:$PATH  curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo  chmod a+x ~/bin/repo  </pre> </li>     <li> <p>下载源码</p> <pre>  wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包  tar xf aosp-latest.tar  cd AOSP   # 解压得到的 AOSP 工程目录  # 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录  repo sync # 正常同步一遍即可得到完整目录  # 或 repo sync -l 仅checkout代码  </pre> </li>    </ul>    <p>ps:总共 <em>25G</em> ,慢慢下吧!</p>    <h2>编译</h2>    <p>进入AOSP根目录</p>    <ul>     <li> <p>初始化编译环境</p> <pre>  . build/envsetup.sh  </pre> </li>     <li> <p>选择编译目标,选择1,所有选项的意思,后面更新</p> <pre>  lunch  </pre> </li>     <li> <p>开始编译,这里使用了4个并发数:</p> <pre>  make -j4  </pre> </li>     <li> <p>使用打包工具mmm,完成命令后会在根目录下生成android.irp,用android studio打开一个现有项目,打开android.irp即可</p> <pre>  mmm development/tools/idegen/  </pre> </li>    </ul>    <h2>运行当前版本的模拟器</h2>    <ul>     <li>将emulator源码目录加入PATH中</li>    </ul>    <pre>  export PATH=&PATH:~/bin/AOSP/out/host/linux-x86/bin  </pre>    <ul>     <li>设置源码编译输出目录</li>    </ul>    <pre>  export ANDROID_PRODUCT_OUT=~/bin/AOSP/out/target/product/generic  </pre>    <ul>     <li>运行emulator</li>    </ul>    <pre>  emulator  </pre>    <h2>下载Android内核源代码</h2>    <ul>     <li> <p>进入kernel目录,下载内核</p> <pre>  git clone https://aosp.tuna.tsinghua.edu.cn/android/kernel/goldfish.git  </pre> </li>     <li> <p>进入goldfish目录,选择分支</p> </li>    </ul>    <pre>  cd goldfish  git branch -a  git checkout remotes/origin/android-goldfish-2.6.29  </pre>    <h2>编译Android内核源代码</h2>    <ul>     <li> <p>将交叉编译工具目录添加到PATH环境变量中</p> <pre>  export PATH=$PATH:~/bin/AOSP/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin  </pre> </li>     <li> <p>打开goldfish下Makefile修改配置文件,找到ARCH,CROSS_COMPILE,将其修改如下</p> <pre>  ARCH      ?=arm  CROSS_COMPILE  ?=/home/user/bin/AOSP/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/  arm-linux-androidkernel-  </pre> </li>    </ul>    <p>note:一定要设为绝对路径。</p>    <ul>     <li>生成配置文件以及编译 <pre>  make goldfish_defconfig  make  </pre> </li>    </ul>    <h2>运行当前内核版本的模拟器</h2>    <ul>     <li> <p>运行当前模拟器</p> <pre>  emulator -kernel ./kernel/goldfish/arch/arm/boot/zImage  </pre> </li>     <li> <p>查看内核版本</p> <pre>  adb shell  cd proc  cat version  </pre> </li>    </ul>    <h2>BUG</h2>    <ul>     <li>JDK内存溢出</li>    </ul>    <pre>  FAILED: /bin/bash out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/dex-dir/classes.dex.rsp  Out of memory error (version 1.2-rc4 'Carnac' (298900 f95d7bdecfceb327f9d201a1348397ed8a843843 by [email protected])).  GC overhead limit exceeded.  Try increasing heap size with java option '-Xmx<size>'.  Warning: This may have produced partial or corrupted output.  ninja: build stopped: subcommand failed.  make: *** [ninja_wrapper] 错误 1  </pre>    <p>A:</p>    <pre>  export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"  ./prebuilts/sdk/tools/jack-admin kill-server  ./prebuilts/sdk/tools/jack-admin start-server  </pre>    <p>ps:作为一个天朝的程序员</p>    <p> </p>    <p>来自:https://blog.jiangtao.tech/2016/07/24/Ubuntu%E4%B8%8BAndroid%E6%BA%90%E7%A0%81%E4%BB%A5%E5%8F%8A%E5%86%85%E6%A0%B8%E4%B8%8B%E8%BD%BD%E4%B8%8E%E7%BC%96%E8%AF%91/</p>    <p> </p>