The ARM-64 architecture (`arm64-v8a`) requires at least level 21 of Android API.
The default entry-point of the Docker image, [`paddle/scripts/docker/build_android.sh`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh) generates the [Android cross-compiling standalone toolchain](https://developer.android.com/ndk/guides/standalone_toolchain.html) based on the argument: `ANDROID_ABI` or `ANDROID_API`. For information about other configuration arguments, please continue reading.
The above command generates and outputs the inference library in `$PWD/install_android` and puts third-party libraries in `$PWD/install_android/third_party`.
## Cross-Compiling on Linux
The Linux-base approach to cross-compile is to run steps in `Dockerfile.android` manually on a Linux x64 computer.
Android NDK includes everything we need to build the [*standalone toolchain*](https://developer.android.com/ndk/guides/standalone_toolchain.html), which in then used to build PaddlePaddle for Android. (We plan to remove the intermediate stage of building the standalone toolchain in the near future.)
- To build the standalone toolchain for `armeabi-v7a` and Android API level 21:
The generated standalone toolchain will be in `your/path/to/arm64_standalone_toolchain`.
**Please be aware that the minimum level of Android API required by PaddlePaddle is 21.**
### Cross-Compiling Arguments
CMake supports [choosing the toolchain](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling). PaddlePaddle provides [`android.cmake`](https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/android.cmake), which configures the Android cross-compiling toolchain for CMake. `android.cmake` is not required for CMake >= 3.7, which support Android cross-compiling. PaddlePaddle detects the CMake version, for those newer than 3.7, it uses [the official version](https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling).
Some other CMake arguments you need to know:
- `CMAKE_SYSTEM_NAME` must be `Android`. This tells PaddlePaddle's CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like `WITH_GPU=OFF`, `WITH_AVX=OFF`, `WITH_PYTHON=OFF`, and `WITH_RDMA=OFF`.
- `WITH_C_API` must be `ON`, to build the C-based inference library for Android.
- `WITH_SWIG_PY` must be `OFF` because the Android platform doesn't support SWIG-based API.
Some Android-specific arguments:
- `ANDROID_STANDALONE_TOOLCHAIN`: the absolute path of the Android standalone toolchain, or the path relative to the CMake build directory. PaddlePaddle's CMake extensions would derive the cross-compiler, sysroot and Android API level from this argument.
- `ANDROID_TOOLCHAIN`: could be `gcc` or `clang`. The default value is `clang`.
- For CMake >= 3.7, it should anyway be `clang`. For older versions, it could be `gcc`.
- Android's official `clang` requires `glibc` >= 2.15.
- `ANDROID_ABI`: could be `armeabi-v7a` or `arm64-v8a`. The default value is `armeabi-v7a`.
- `ANDROID_NATIVE_API_LEVEL`: could be derived from the value of `ANDROID_STANDALONE_TOOLCHAIN`.
- `ANROID_ARM_MODE`:
- could be `ON` or `OFF`, and defaults to `ON`, when `ANDROID_ABI=armeabi-v7a`;
- no need to specify when `ANDROID_ABI=arm64-v8a`.
- `ANDROID_ARM_NEON`: indicates if to use NEON instructions.
- could be `ON` or `OFF`, and defaults to `ON`, when `ANDROID_ABI=armeabi-v7a`;
- no need to specify when `ANDROID_ABI=arm64-v8a`.
Other useful arguments:
- `USE_EIGEN_FOR_BLAS`: indicates if using Eigen. Could be `ON` or `OFF`, defaults to `OFF`.
- `HOST_C/CXX_COMPILER`: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable `CC`, or `cc`.
<liclass="toctree-l2"><aclass="reference internal"href="../../getstarted/build_and_install/index_en.html">Install and Build</a><ul>
<liclass="toctree-l3"><aclass="reference internal"href="../../getstarted/build_and_install/docker_install_en.html">PaddlePaddle in Docker Containers</a></li>
<liclass="toctree-l3"><aclass="reference internal"href="../../getstarted/build_and_install/build_from_source_en.html">Installing from Sources</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../usage/k8s/k8s_en.html">Paddle On Kubernetes</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../usage/k8s/k8s_aws_en.html">Distributed PaddlePaddle Training on AWS with Kubernetes</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../dev/build_en.html">Build PaddlePaddle from Source Code and Run Unit Test</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../dev/new_layer_en.html">Write New Layers</a></li>
<spanid="build-paddlepaddle-for-android"></span><h1>Build PaddlePaddle for Android<aclass="headerlink"href="#build-paddlepaddle-for-android"title="Permalink to this headline">¶</a></h1>
<p>There are two approaches to build PaddlePaddle for Android: using Docker and on Linux without Docker.</p>
<spanid="cross-compiling-using-docker"></span><h2>Cross-Compiling Using Docker<aclass="headerlink"href="#cross-compiling-using-docker"title="Permalink to this headline">¶</a></h2>
<p>Docker-based cross-compiling is the recommended approach because Docker runs on all major operating systems, including Linux, Mac OS X, and Windows.</p>
<divclass="section"id="build-the-docker-image">
<spanid="build-the-docker-image"></span><h3>Build the Docker Image<aclass="headerlink"href="#build-the-docker-image"title="Permalink to this headline">¶</a></h3>
<p>The following steps pack all the tools that we need to build PaddlePaddle into a Docker image.</p>
<spanid="build-the-inference-library"></span><h3>Build the Inference Library<aclass="headerlink"href="#build-the-inference-library"title="Permalink to this headline">¶</a></h3>
<p>We can run the Docker image we just created to build the inference library of PaddlePaddle for Android using the command below:</p>
<p>The ARM-64 architecture (<codeclass="docutils literal"><spanclass="pre">arm64-v8a</span></code>) requires at least level 21 of Android API.</p>
<p>The default entry-point of the Docker image, <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh"><codeclass="docutils literal"><spanclass="pre">paddle/scripts/docker/build_android.sh</span></code></a> generates the <aclass="reference external"href="https://developer.android.com/ndk/guides/standalone_toolchain.html">Android cross-compiling standalone toolchain</a> based on the argument: <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI</span></code> or <codeclass="docutils literal"><spanclass="pre">ANDROID_API</span></code>. For information about other configuration arguments, please continue reading.</p>
<p>The above command generates and outputs the inference library in <codeclass="docutils literal"><spanclass="pre">$PWD/install_android</span></code> and puts third-party libraries in <codeclass="docutils literal"><spanclass="pre">$PWD/install_android/third_party</span></code>.</p>
</div>
</div>
<divclass="section"id="cross-compiling-on-linux">
<spanid="cross-compiling-on-linux"></span><h2>Cross-Compiling on Linux<aclass="headerlink"href="#cross-compiling-on-linux"title="Permalink to this headline">¶</a></h2>
<p>The Linux-base approach to cross-compile is to run steps in <codeclass="docutils literal"><spanclass="pre">Dockerfile.android</span></code> manually on a Linux x64 computer.</p>
<divclass="section"id="setup-the-environment">
<spanid="setup-the-environment"></span><h3>Setup the Environment<aclass="headerlink"href="#setup-the-environment"title="Permalink to this headline">¶</a></h3>
<p>To build for Android’s, we need <aclass="reference external"href="https://developer.android.com/ndk/downloads/index.html">Android NDK</a>:</p>
<p>Android NDK includes everything we need to build the <aclass="reference external"href="https://developer.android.com/ndk/guides/standalone_toolchain.html"><em>standalone toolchain</em></a>, which in then used to build PaddlePaddle for Android. (We plan to remove the intermediate stage of building the standalone toolchain in the near future.)</p>
<ul>
<li><pclass="first">To build the standalone toolchain for <codeclass="docutils literal"><spanclass="pre">armeabi-v7a</span></code> and Android API level 21:</p>
<p>The generated standalone toolchain will be in <codeclass="docutils literal"><spanclass="pre">your/path/to/arm_standalone_toolchain</span></code>.</p>
</li>
<li><pclass="first">To build the standalone toolchain for <codeclass="docutils literal"><spanclass="pre">arm64-v8a</span></code> and Android API level 21:</p>
<p>The generated standalone toolchain will be in <codeclass="docutils literal"><spanclass="pre">your/path/to/arm64_standalone_toolchain</span></code>.</p>
</li>
</ul>
<p><strong>Please be aware that the minimum level of Android API required by PaddlePaddle is 21.</strong></p>
<spanid="cross-compiling-arguments"></span><h3>Cross-Compiling Arguments<aclass="headerlink"href="#cross-compiling-arguments"title="Permalink to this headline">¶</a></h3>
<p>CMake supports <aclass="reference external"href="https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling">choosing the toolchain</a>. PaddlePaddle provides <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/android.cmake"><codeclass="docutils literal"><spanclass="pre">android.cmake</span></code></a>, which configures the Android cross-compiling toolchain for CMake. <codeclass="docutils literal"><spanclass="pre">android.cmake</span></code> is not required for CMake >= 3.7, which support Android cross-compiling. PaddlePaddle detects the CMake version, for those newer than 3.7, it uses <aclass="reference external"href="https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling">the official version</a>.</p>
<p>Some other CMake arguments you need to know:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">CMAKE_SYSTEM_NAME</span></code> must be <codeclass="docutils literal"><spanclass="pre">Android</span></code>. This tells PaddlePaddle’s CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like <codeclass="docutils literal"><spanclass="pre">WITH_GPU=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_AVX=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_PYTHON=OFF</span></code>, and <codeclass="docutils literal"><spanclass="pre">WITH_RDMA=OFF</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">WITH_C_API</span></code> must be <codeclass="docutils literal"><spanclass="pre">ON</span></code>, to build the C-based inference library for Android.</li>
<li><codeclass="docutils literal"><spanclass="pre">WITH_SWIG_PY</span></code> must be <codeclass="docutils literal"><spanclass="pre">OFF</span></code> because the Android platform doesn’t support SWIG-based API.</li>
</ul>
<p>Some Android-specific arguments:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_STANDALONE_TOOLCHAIN</span></code>: the absolute path of the Android standalone toolchain, or the path relative to the CMake build directory. PaddlePaddle’s CMake extensions would derive the cross-compiler, sysroot and Android API level from this argument.</li>
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_TOOLCHAIN</span></code>: could be <codeclass="docutils literal"><spanclass="pre">gcc</span></code> or <codeclass="docutils literal"><spanclass="pre">clang</span></code>. The default value is <codeclass="docutils literal"><spanclass="pre">clang</span></code>.<ul>
<li>For CMake >= 3.7, it should anyway be <codeclass="docutils literal"><spanclass="pre">clang</span></code>. For older versions, it could be <codeclass="docutils literal"><spanclass="pre">gcc</span></code>.</li>
<li>Android’s official <codeclass="docutils literal"><spanclass="pre">clang</span></code> requires <codeclass="docutils literal"><spanclass="pre">glibc</span></code>>= 2.15.</li>
</ul>
</li>
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_ABI</span></code>: could be <codeclass="docutils literal"><spanclass="pre">armeabi-v7a</span></code> or <codeclass="docutils literal"><spanclass="pre">arm64-v8a</span></code>. The default value is <codeclass="docutils literal"><spanclass="pre">armeabi-v7a</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_NATIVE_API_LEVEL</span></code>: could be derived from the value of <codeclass="docutils literal"><spanclass="pre">ANDROID_STANDALONE_TOOLCHAIN</span></code>.</li>
<li>could be <codeclass="docutils literal"><spanclass="pre">ON</span></code> or <codeclass="docutils literal"><spanclass="pre">OFF</span></code>, and defaults to <codeclass="docutils literal"><spanclass="pre">ON</span></code>, when <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI=armeabi-v7a</span></code>;</li>
<li>no need to specify when <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI=arm64-v8a</span></code>.</li>
</ul>
</li>
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_ARM_NEON</span></code>: indicates if to use NEON instructions.<ul>
<li>could be <codeclass="docutils literal"><spanclass="pre">ON</span></code> or <codeclass="docutils literal"><spanclass="pre">OFF</span></code>, and defaults to <codeclass="docutils literal"><spanclass="pre">ON</span></code>, when <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI=armeabi-v7a</span></code>;</li>
<li>no need to specify when <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI=arm64-v8a</span></code>.</li>
</ul>
</li>
</ul>
<p>Other useful arguments:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">USE_EIGEN_FOR_BLAS</span></code>: indicates if using Eigen. Could be <codeclass="docutils literal"><spanclass="pre">ON</span></code> or <codeclass="docutils literal"><spanclass="pre">OFF</span></code>, defaults to <codeclass="docutils literal"><spanclass="pre">OFF</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">HOST_C/CXX_COMPILER</span></code>: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable <codeclass="docutils literal"><spanclass="pre">CC</span></code>, or <codeclass="docutils literal"><spanclass="pre">cc</span></code>.</li>
</ul>
<p>Some frequent configurations for your reference:</p>
<li><codeclass="docutils literal"><spanclass="pre">USE_EIGEN_BLAS=ON</span></code> for <codeclass="docutils literal"><spanclass="pre">armeabi-v7a</span></code>, or <codeclass="docutils literal"><spanclass="pre">USE_EIGEN_FOR_BLAS=OFF</span></code> for <codeclass="docutils literal"><spanclass="pre">arm64-v8a</span></code>.</li>
</ul>
</div>
<divclass="section"id="build-and-install">
<spanid="build-and-install"></span><h3>Build and Install<aclass="headerlink"href="#build-and-install"title="Permalink to this headline">¶</a></h3>
<p>After running <codeclass="docutils literal"><spanclass="pre">cmake</span></code>, we can run <codeclass="docutils literal"><spanclass="pre">make;</span><spanclass="pre">make</span><spanclass="pre">install</span></code> to build and install.</p>
<p>Before building, you might want to remove the <codeclass="docutils literal"><spanclass="pre">third_party</span></code> and <codeclass="docutils literal"><spanclass="pre">build</span></code> directories including pre-built libraries for other architectures.</p>
<p>After building,in the directory <codeclass="docutils literal"><spanclass="pre">CMAKE_INSTALL_PREFIX</span></code>, you will find three sub-directories:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">include</span></code>: the header file of the inference library,</li>
<li><codeclass="docutils literal"><spanclass="pre">lib</span></code>: the inference library built for various Android ABIs,</li>
<li><codeclass="docutils literal"><spanclass="pre">third_party</span></code>: dependent third-party libraries built for Android.</li>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.
The ARM-64 architecture (`arm64-v8a`) requires at least level 21 of Android API.
The default entry-point of the Docker image, [`paddle/scripts/docker/build_android.sh`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh) generates the [Android cross-compiling standalone toolchain](https://developer.android.com/ndk/guides/standalone_toolchain.html) based on the argument: `ANDROID_ABI` or `ANDROID_API`. For information about other configuration arguments, please continue reading.
The above command generates and outputs the inference library in `$PWD/install_android` and puts third-party libraries in `$PWD/install_android/third_party`.
## Cross-Compiling on Linux
The Linux-base approach to cross-compile is to run steps in `Dockerfile.android` manually on a Linux x64 computer.
Android NDK includes everything we need to build the [*standalone toolchain*](https://developer.android.com/ndk/guides/standalone_toolchain.html), which in then used to build PaddlePaddle for Android. (We plan to remove the intermediate stage of building the standalone toolchain in the near future.)
- To build the standalone toolchain for `armeabi-v7a` and Android API level 21:
The generated standalone toolchain will be in `your/path/to/arm64_standalone_toolchain`.
**Please be aware that the minimum level of Android API required by PaddlePaddle is 21.**
### Cross-Compiling Arguments
CMake supports [choosing the toolchain](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling). PaddlePaddle provides [`android.cmake`](https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/android.cmake), which configures the Android cross-compiling toolchain for CMake. `android.cmake` is not required for CMake >= 3.7, which support Android cross-compiling. PaddlePaddle detects the CMake version, for those newer than 3.7, it uses [the official version](https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling).
Some other CMake arguments you need to know:
- `CMAKE_SYSTEM_NAME` must be `Android`. This tells PaddlePaddle's CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like `WITH_GPU=OFF`, `WITH_AVX=OFF`, `WITH_PYTHON=OFF`, and `WITH_RDMA=OFF`.
- `WITH_C_API` must be `ON`, to build the C-based inference library for Android.
- `WITH_SWIG_PY` must be `OFF` because the Android platform doesn't support SWIG-based API.
Some Android-specific arguments:
- `ANDROID_STANDALONE_TOOLCHAIN`: the absolute path of the Android standalone toolchain, or the path relative to the CMake build directory. PaddlePaddle's CMake extensions would derive the cross-compiler, sysroot and Android API level from this argument.
- `ANDROID_TOOLCHAIN`: could be `gcc` or `clang`. The default value is `clang`.
- For CMake >= 3.7, it should anyway be `clang`. For older versions, it could be `gcc`.
- Android's official `clang` requires `glibc` >= 2.15.
- `ANDROID_ABI`: could be `armeabi-v7a` or `arm64-v8a`. The default value is `armeabi-v7a`.
- `ANDROID_NATIVE_API_LEVEL`: could be derived from the value of `ANDROID_STANDALONE_TOOLCHAIN`.
- `ANROID_ARM_MODE`:
- could be `ON` or `OFF`, and defaults to `ON`, when `ANDROID_ABI=armeabi-v7a`;
- no need to specify when `ANDROID_ABI=arm64-v8a`.
- `ANDROID_ARM_NEON`: indicates if to use NEON instructions.
- could be `ON` or `OFF`, and defaults to `ON`, when `ANDROID_ABI=armeabi-v7a`;
- no need to specify when `ANDROID_ABI=arm64-v8a`.
Other useful arguments:
- `USE_EIGEN_FOR_BLAS`: indicates if using Eigen. Could be `ON` or `OFF`, defaults to `OFF`.
- `HOST_C/CXX_COMPILER`: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable `CC`, or `cc`.
执行上述`docker run`命令时,容器默认执行[paddle/scripts/docker/build_android.sh](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh)脚本。该脚本中记录了交叉编译Android版PaddlePaddle库常用的CMake配置,并且会根据`ANDROID_ABI`和`ANDROID_API`自动构建独立工具链、进行编译和安装。由于arm64架构要求Android API不小于21。因此当`ANDROID_ABI=arm64-v8a`,`ANDROID_API<21`时,Docker容器中将默认使用`Android API 21`的编译工具链。用户可以参考下文**配置交叉编译参数**章节,根据个人的需求修改定制Docker容器所执行的脚本。编译安装结束之后,PaddlePaddle的C-API库将被安装到`$PWD/install_android`目录,所依赖的第三方库同时也被安装到`$PWD/install_android/third_party`目录。
执行上述`docker run`命令时,容器默认执行[paddle/scripts/docker/build_android.sh](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh)脚本。该脚本中记录了交叉编译Android版PaddlePaddle库常用的CMake配置,并且会根据`ANDROID_ABI`和`ANDROID_API`自动构建独立工具链、进行编译和安装。由于arm64架构要求Android API不小于21。因此当`ANDROID_ABI=arm64-v8a`,`ANDROID_API<21`时,Docker容器中将默认使用`Android API 21`的编译工具链。用户可以参考下文**配置交叉编译参数**章节,根据个人的需求修改定制Docker容器所执行的脚本。编译安装结束之后,PaddlePaddle的C-API库将被安装到`$PWD/install_android`目录,所依赖的第三方库同时也被安装到`$PWD/install_android/third_party`目录。
<spanid="build-paddlepaddle-for-android"></span><h1>Build PaddlePaddle for Android<aclass="headerlink"href="#build-paddlepaddle-for-android"title="永久链接至标题">¶</a></h1>
<p>There are two approaches to build PaddlePaddle for Android: using Docker and on Linux without Docker.</p>
<spanid="cross-compiling-using-docker"></span><h2>Cross-Compiling Using Docker<aclass="headerlink"href="#cross-compiling-using-docker"title="永久链接至标题">¶</a></h2>
<p>Docker-based cross-compiling is the recommended approach because Docker runs on all major operating systems, including Linux, Mac OS X, and Windows.</p>
<divclass="section"id="build-the-docker-image">
<spanid="build-the-docker-image"></span><h3>Build the Docker Image<aclass="headerlink"href="#build-the-docker-image"title="永久链接至标题">¶</a></h3>
<p>The following steps pack all the tools that we need to build PaddlePaddle into a Docker image.</p>
<spanid="build-the-inference-library"></span><h3>Build the Inference Library<aclass="headerlink"href="#build-the-inference-library"title="永久链接至标题">¶</a></h3>
<p>We can run the Docker image we just created to build the inference library of PaddlePaddle for Android using the command below:</p>
<p>The ARM-64 architecture (<codeclass="docutils literal"><spanclass="pre">arm64-v8a</span></code>) requires at least level 21 of Android API.</p>
<p>The default entry-point of the Docker image, <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/scripts/docker/build_android.sh"><codeclass="docutils literal"><spanclass="pre">paddle/scripts/docker/build_android.sh</span></code></a> generates the <aclass="reference external"href="https://developer.android.com/ndk/guides/standalone_toolchain.html">Android cross-compiling standalone toolchain</a> based on the argument: <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI</span></code> or <codeclass="docutils literal"><spanclass="pre">ANDROID_API</span></code>. For information about other configuration arguments, please continue reading.</p>
<p>The above command generates and outputs the inference library in <codeclass="docutils literal"><spanclass="pre">$PWD/install_android</span></code> and puts third-party libraries in <codeclass="docutils literal"><spanclass="pre">$PWD/install_android/third_party</span></code>.</p>
</div>
</div>
<divclass="section"id="cross-compiling-on-linux">
<spanid="cross-compiling-on-linux"></span><h2>Cross-Compiling on Linux<aclass="headerlink"href="#cross-compiling-on-linux"title="永久链接至标题">¶</a></h2>
<p>The Linux-base approach to cross-compile is to run steps in <codeclass="docutils literal"><spanclass="pre">Dockerfile.android</span></code> manually on a Linux x64 computer.</p>
<divclass="section"id="setup-the-environment">
<spanid="setup-the-environment"></span><h3>Setup the Environment<aclass="headerlink"href="#setup-the-environment"title="永久链接至标题">¶</a></h3>
<p>To build for Android’s, we need <aclass="reference external"href="https://developer.android.com/ndk/downloads/index.html">Android NDK</a>:</p>
<p>Android NDK includes everything we need to build the <aclass="reference external"href="https://developer.android.com/ndk/guides/standalone_toolchain.html"><em>standalone toolchain</em></a>, which in then used to build PaddlePaddle for Android. (We plan to remove the intermediate stage of building the standalone toolchain in the near future.)</p>
<ul>
<li><pclass="first">To build the standalone toolchain for <codeclass="docutils literal"><spanclass="pre">armeabi-v7a</span></code> and Android API level 21:</p>
<p>The generated standalone toolchain will be in <codeclass="docutils literal"><spanclass="pre">your/path/to/arm_standalone_toolchain</span></code>.</p>
</li>
<li><pclass="first">To build the standalone toolchain for <codeclass="docutils literal"><spanclass="pre">arm64-v8a</span></code> and Android API level 21:</p>
<p>The generated standalone toolchain will be in <codeclass="docutils literal"><spanclass="pre">your/path/to/arm64_standalone_toolchain</span></code>.</p>
</li>
</ul>
<p><strong>Please be aware that the minimum level of Android API required by PaddlePaddle is 21.</strong></p>
<p>CMake supports <aclass="reference external"href="https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling">choosing the toolchain</a>. PaddlePaddle provides <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/android.cmake"><codeclass="docutils literal"><spanclass="pre">android.cmake</span></code></a>, which configures the Android cross-compiling toolchain for CMake. <codeclass="docutils literal"><spanclass="pre">android.cmake</span></code> is not required for CMake >= 3.7, which support Android cross-compiling. PaddlePaddle detects the CMake version, for those newer than 3.7, it uses <aclass="reference external"href="https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling">the official version</a>.</p>
<p>Some other CMake arguments you need to know:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">CMAKE_SYSTEM_NAME</span></code> must be <codeclass="docutils literal"><spanclass="pre">Android</span></code>. This tells PaddlePaddle’s CMake system to cross-compile third-party dependencies. This also changes some other CMake arguments like <codeclass="docutils literal"><spanclass="pre">WITH_GPU=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_AVX=OFF</span></code>, <codeclass="docutils literal"><spanclass="pre">WITH_PYTHON=OFF</span></code>, and <codeclass="docutils literal"><spanclass="pre">WITH_RDMA=OFF</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">WITH_C_API</span></code> must be <codeclass="docutils literal"><spanclass="pre">ON</span></code>, to build the C-based inference library for Android.</li>
<li><codeclass="docutils literal"><spanclass="pre">WITH_SWIG_PY</span></code> must be <codeclass="docutils literal"><spanclass="pre">OFF</span></code> because the Android platform doesn’t support SWIG-based API.</li>
</ul>
<p>Some Android-specific arguments:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_STANDALONE_TOOLCHAIN</span></code>: the absolute path of the Android standalone toolchain, or the path relative to the CMake build directory. PaddlePaddle’s CMake extensions would derive the cross-compiler, sysroot and Android API level from this argument.</li>
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_TOOLCHAIN</span></code>: could be <codeclass="docutils literal"><spanclass="pre">gcc</span></code> or <codeclass="docutils literal"><spanclass="pre">clang</span></code>. The default value is <codeclass="docutils literal"><spanclass="pre">clang</span></code>.<ul>
<li>For CMake >= 3.7, it should anyway be <codeclass="docutils literal"><spanclass="pre">clang</span></code>. For older versions, it could be <codeclass="docutils literal"><spanclass="pre">gcc</span></code>.</li>
<li>Android’s official <codeclass="docutils literal"><spanclass="pre">clang</span></code> requires <codeclass="docutils literal"><spanclass="pre">glibc</span></code>>= 2.15.</li>
</ul>
</li>
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_ABI</span></code>: could be <codeclass="docutils literal"><spanclass="pre">armeabi-v7a</span></code> or <codeclass="docutils literal"><spanclass="pre">arm64-v8a</span></code>. The default value is <codeclass="docutils literal"><spanclass="pre">armeabi-v7a</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_NATIVE_API_LEVEL</span></code>: could be derived from the value of <codeclass="docutils literal"><spanclass="pre">ANDROID_STANDALONE_TOOLCHAIN</span></code>.</li>
<li>could be <codeclass="docutils literal"><spanclass="pre">ON</span></code> or <codeclass="docutils literal"><spanclass="pre">OFF</span></code>, and defaults to <codeclass="docutils literal"><spanclass="pre">ON</span></code>, when <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI=armeabi-v7a</span></code>;</li>
<li>no need to specify when <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI=arm64-v8a</span></code>.</li>
</ul>
</li>
<li><codeclass="docutils literal"><spanclass="pre">ANDROID_ARM_NEON</span></code>: indicates if to use NEON instructions.<ul>
<li>could be <codeclass="docutils literal"><spanclass="pre">ON</span></code> or <codeclass="docutils literal"><spanclass="pre">OFF</span></code>, and defaults to <codeclass="docutils literal"><spanclass="pre">ON</span></code>, when <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI=armeabi-v7a</span></code>;</li>
<li>no need to specify when <codeclass="docutils literal"><spanclass="pre">ANDROID_ABI=arm64-v8a</span></code>.</li>
</ul>
</li>
</ul>
<p>Other useful arguments:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">USE_EIGEN_FOR_BLAS</span></code>: indicates if using Eigen. Could be <codeclass="docutils literal"><spanclass="pre">ON</span></code> or <codeclass="docutils literal"><spanclass="pre">OFF</span></code>, defaults to <codeclass="docutils literal"><spanclass="pre">OFF</span></code>.</li>
<li><codeclass="docutils literal"><spanclass="pre">HOST_C/CXX_COMPILER</span></code>: specifies the host compiler, which is used to build the host-specific protoc and target-specific OpenBLAS. It defaults to the value of the environment variable <codeclass="docutils literal"><spanclass="pre">CC</span></code>, or <codeclass="docutils literal"><spanclass="pre">cc</span></code>.</li>
</ul>
<p>Some frequent configurations for your reference:</p>
<li><codeclass="docutils literal"><spanclass="pre">USE_EIGEN_BLAS=ON</span></code> for <codeclass="docutils literal"><spanclass="pre">armeabi-v7a</span></code>, or <codeclass="docutils literal"><spanclass="pre">USE_EIGEN_FOR_BLAS=OFF</span></code> for <codeclass="docutils literal"><spanclass="pre">arm64-v8a</span></code>.</li>
</ul>
</div>
<divclass="section"id="build-and-install">
<spanid="build-and-install"></span><h3>Build and Install<aclass="headerlink"href="#build-and-install"title="永久链接至标题">¶</a></h3>
<p>After running <codeclass="docutils literal"><spanclass="pre">cmake</span></code>, we can run <codeclass="docutils literal"><spanclass="pre">make;</span><spanclass="pre">make</span><spanclass="pre">install</span></code> to build and install.</p>
<p>Before building, you might want to remove the <codeclass="docutils literal"><spanclass="pre">third_party</span></code> and <codeclass="docutils literal"><spanclass="pre">build</span></code> directories including pre-built libraries for other architectures.</p>
<p>After building,in the directory <codeclass="docutils literal"><spanclass="pre">CMAKE_INSTALL_PREFIX</span></code>, you will find three sub-directories:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">include</span></code>: the header file of the inference library,</li>
<li><codeclass="docutils literal"><spanclass="pre">lib</span></code>: the inference library built for various Android ABIs,</li>
<li><codeclass="docutils literal"><spanclass="pre">third_party</span></code>: dependent third-party libraries built for Android.</li>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.