Android NDK Development ---- Android 4.4

简介: NDK Development: ==== This document describes how one can modify the NDK and generate new experimental release packages for it.
NDK Development:
====

This document describes how one can modify the NDK and generate
new experimental release packages for it.

I. Getting the sources:
---

The sources live under the "ndk" and "development/ndk" directories in
the Android source tree:

  - "ndk" contains the main build scripts and documentation
  - "development/ndk" contains platform-specific headers and samples

If you have downloaded the full Android source tree through the "repo"
tool, you can start directly there. Otherwise, you can just get these
two repositories with the following:

        mkdir workdir
        cd workdir
        git clone https://android.googlesource.com/platform/ndk.git ndk
        git clone https://android.googlesource.com/platform/development.git development
        export NDK=`pwd`/ndk


II. Building the platforms tree:
---

You need to do that once if you want to use the content of $NDK to build
samples, tests or anything else:

        $NDK/build/tools/gen-platforms.sh

What the script does is populate the $NDK/platforms and $NDK/samples
directories from the content of development/ndk.

What is under development/ndk is segregated by API level. This makes it
easier to add a new platform to the tree, but is not well-suited to building
stuff. The gen-platforms.sh script will gather all files appropriately
and place the result inside $NDK/platforms and $NDK/samples.

Note: These directories are listed by $NDK/.gitignore, so they won't appear
      on your git status. You can remove them if you want by running:

        $NDK/build/tools/dev-cleanup.sh

which also removes all intermediate files and directories from $NDK.


III. Prebuilt binaries:
---

The NDK requires several prebuilt binary executables to work properly, these
include the following:

  - toolchain binaries for the cross-compiler and associated tools
  - gdbserver binaries required for native debugging

These are not provided in the NDK's git repositories. However, there are
several ways to get them:

### 1. From a previous NDK release package:

By far the easiest thing to do is to copy the binaries from a previous
NDK installation. You can do that with a command like the following one:

          cp -r $PREVIOUS_NDK/toolchains/* $NDK/toolchains/

NOTE: The binaries are listed in $NDK/.gitignore and will not appear
      in your git status.


### 2. Download and rebuild directly from the internet:

IMPORTANT: This is *very* long.

The NDK comes with several scripts that can be used to rebuild the
binaries from scratch, after downloading their sources from
android.googlesource.com.

There are several ways to do that, the most naive one, which will
always work but will be *very* long (expect a few hours on a typical
dual-core machine) is to do the following:

          $NDK/build/tools/rebuild-all-prebuilt.sh

This will perform all the steps required to rebuild the binaries,
which include:

  - downloading the sources from android.googlesource.com
  - patching them with appropriate changes, if needed
  - rebuilding everything from scratch
  - copying the generated binaries to the proper location under $NDK

You will need about 30G of free space in your /tmp directory to be
able to do that, and *plenty* of free time.

IMPORTANT: If you plan to generate NDK release packages, even
experimental ones, we strongly suggest you to use the individual
steps described in 3/ below.

IMPORTANT:
Since NDK r5, Windows binaries can be built on Linux by using the
--mingw option, which requires that you have the "mingw32" package
installed on your system. For example:

        $NDK/build/tools/rebuild-all-prebuilt.sh --mingw

We do not officially support building these binaries directly on
Windows (either through Cygwin or MSys) anymore, due to the vast
number of problems these environments create when trying to do so.



### 3. Download, rebuild, package, install in separate steps:

If you plan to generate your own NDK release packages, it is better
to rebuild your binaries using separate steps, as in:

  - Download the sources from the Internet, patch them, then
    package the result in a simple tarball.

  - For every target system (linux-x86, darwin-x86 and windows),
    rebuild the binaries from the same source tarball.

  - Package and collect all prebuilt binaries into a single
    directory that will be used when packaging NDK releases.

Here are more details on how to do that:

#### 3.a/ Download + patching + packaging sources:

Use the following command to download, patch and package the
sources:

        $NDK/build/tools/download-toolchain-sources.sh --package

This will create a large tarball containing all sources ready to be
used by the following step. The generated file path will be dumped at
the script when it completes its operation and should be something
like:

        /tmp/android-ndk-toolchain-<date>.tar.bz2

Note that if you don't use the --package option, you will need to
provide the name of a directory where the patched sources will be
copied instead, as in:

        $NDK/build/tools/download-toolchain-sources.sh <target-src-dir>


#### 3.b/ Build the binaries:

Use the following command to rebuild the binaries from the source
tarball that was created in the previous section with the --package
option:

        $NDK/build/tools/rebuild-all-prebuilt.sh --toolchain-pkg=<file>

Where <file> points to the package generated by the
download-toolchain-sources.sh script.

In the case where you downloaded the sources to a directory instead,
use the --toolchain-src-dir option instead, as with:

        $NDK/build/tools/rebuild-all-prebuilt.sh --toolchain-src-dir=<path>

This will rebuild all the prebuilt binaries for your host platforms
and place them in a directory named:

        /tmp/ndk-prebuilt/prebuilt-<date>/

These binary packages include the following:

  - host-specific toolchain binaries. e.g.
    `arm-linux-androideabi-4.6-linux-x86.tar.bz2`.

  - toolchain specific device binaries, e.g.
    `arm-gdbserver.tar.bz2`.

IMPORTANT:
To generate Windows binaries on Windows, install the "mingw32"
package on your system, then use the --mingw option, as in:

        $NDK/build/tools/rebuild-all-prebuilt.sh --mingw --toolchain-pkg=<file>

Note that device-specific binaries (e.g. gdbserver) cannot be
rebuilt with this option.

#### 3.c/ Copy the binaries to your NDK tree:

Simply go to your NDK tree, and unpack the binary tarballs in place,
for example:

        cd $NDK
        tar xjf <path>/*.tar.bz2

Where <path> is a directory containing all the tarballs (e.g. it
could be simply /tmp/ndk-prebuilt/prebuilt-<date>)

This will put the corresponding files at the correct location.

#### 3.c/

It is a good idea to save the generated toolchain binaries into
an archive. To do that, use the --package option, as in:

        $NDK/build/tools/rebuild-all-prebuilt.sh --package

This will generate a package file containing all the prebuilts, that
can be unpacked directly into your $NDK directory. The package name is
printed at the end, e.g."android-ndk-prebuild-<date>-<system>.tar.bz2".

Where <date> is the current date, and <system> is your system name.
Then, to unpack:

        cd $NDK
        tar xjf /tmp/android-ndk-prebuilt-<date>-<system>.tar.bz2


The generated package can easily be shared with other people.


IV. Generate new package releases:
---

You can generate new experimental NDK release packages once you're satisfied
with your changes, in order to share them with other people. There are two
ways to do that:

### 1. Using the 'make-release.sh' script:

The simplest, and also the slowest way, to generate a new NDK release
is to invoke this script, with:

         $NDK/build/tools/make-release.sh

NOTE: THIS WILL BE VERY VERY LONG. The script will do all the steps
  described in section III *from* scratch, and this can take several
  hours on a dual-core machine.

You should only use it in case of desperation, or if you don't want
to deal with all the details exposed in section III or below.


### 2. Using a previous NDK release package:

This is the second simplest way to generate a new package, and it will
be extremely quick because it will pick the prebuilt binaries directly
from the previous package.

Do the following:

        cd $NDK
        build/tools/package-release.sh --prebuilt-ndk=<file>

Where <file> points to a previous NDK package (i.e. archive file).

NOTE: This method can only be used to generate a single release package
    for the current host system.

### 3. Using prebuilt tarballs:

If you have generated prebuilt binary tarballs with the steps described
in section III.3 above, you can use these to generate release packages
as well.

Assuming that you have collected prebuilt tarballs for all three supported
host systems (i.e. linux-x86, darwin-x86 and windows) under a directory,
do the following:

        cd $NDK
        build/tools/package-release.sh --prebuilt-dir=<path>

The generated NDK package release will have a name that looks like:

        /tmp/ndk-release/android-ndk-<release>-<system>.zip

Where <release> is by default the current date in ISO format
(e.g. 20100915), and <system> corresponds to the host system where the
NDK release is supposed to run.

The script 'package-release.sh' provides a few additional options:

        --release=<name>       Change the name of the release

        --systems=<list>       Change the list of host systems to package for

        --platforms=<list>     List of API levels to package in the NDK

        --out-dir=<path>       Specify a different output directory for the
                              final packages (instead of /tmp/ndk-release)

Use --help to list them all.
目录
相关文章
|
数据采集 编解码 Ubuntu
Android流媒体开发之路二:NDK C++开发Android端RTMP直播推流程序
Android流媒体开发之路二:NDK C++开发Android端RTMP直播推流程序
278 0
|
1月前
|
编译器 Android开发
配置环境变量,使CMakeLists.txt可直接使用Android NDK工具链编译项目
配置环境变量,使CMakeLists.txt可直接使用Android NDK工具链编译项目
|
2月前
|
Java Android开发 C++
🚀Android NDK开发实战!Java与C++混合编程,打造极致性能体验!📊
在Android应用开发中,追求卓越性能是不变的主题。本文介绍如何利用Android NDK(Native Development Kit)结合Java与C++进行混合编程,提升应用性能。从环境搭建到JNI接口设计,再到实战示例,全面展示NDK的优势与应用技巧,助你打造高性能应用。通过具体案例,如计算斐波那契数列,详细讲解Java与C++的协作流程,帮助开发者掌握NDK开发精髓,实现高效计算与硬件交互。
137 1
|
3月前
|
开发工具 Android开发
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
168 4
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
|
3月前
|
开发工具 Android开发
解决Android Studio编译提示NDK is missing a “platforms“ directory
解决Android Studio编译提示NDK is missing a “platforms“ directory
199 1
|
4月前
|
Java Android开发 C++
🚀Android NDK开发实战!Java与C++混合编程,打造极致性能体验!📊
【7月更文挑战第28天】在 Android 开发中, NDK 让 Java 与 C++ 混合编程成为可能, 从而提升应用性能。**为何选 NDK?** C++ 在执行效率与内存管理上优于 Java, 特别适合高性能需求场景。**环境搭建** 需 Android Studio 和 NDK, 工具如 CMake。**JNI** 构建 Java-C++ 交互, 通过声明 `native` 方法并在 C++ 中实现。**实战** 示例: 使用 C++ 计算斐波那契数列以提高效率。**总结** 混合编程增强性能, 但增加复杂性, 使用前需谨慎评估。
141 4
|
6月前
|
Android开发
Android launcher development resources
Android launcher development resources
29 1
|
5月前
|
XML Java Android开发
Android Studio2.2 中支持NDK开发HelloJNI例子
Android Studio2.2 中支持NDK开发HelloJNI例子
33 0
|
6月前
|
Java 开发工具 Android开发
鸿蒙HarmonyOS 与 Android 的NDK有什么区别?
鸿蒙(HarmonyOS)和Android的NDK(Native Development Kit)是两个不同的概念,它们在设计理念、架构、开发方式和目标平台等方面存在着一些显著的不同。
325 0
|
6月前
|
传感器 Java 开发工具
[NDK/JNI系列03] Android Studio集成NDK开发环境
[NDK/JNI系列03] Android Studio集成NDK开发环境
65 0
下一篇
无影云桌面