在 Linux 系统上安装 Android NDK

在 Linux 系统上安装 Android NDK

  • [1. Android NDK](#1. Android NDK)
  • [2. NDK Downloads](#2. NDK Downloads)
    • [2.1. Latest LTS Version (r26d)](#2.1. Latest LTS Version (r26d))
    • [2.2. Old Unsupported Versions](#2.2. Old Unsupported Versions)
  • [3. 安装 NDK](#3. 安装 NDK)
  • [4. Get started with the NDK (NDK 使用入门)](#4. Get started with the NDK (NDK 使用入门))
  • References

1. Android NDK

https://developer.android.com/ndk

The Android NDK is a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.

Android NDK 是一个工具集,可让您以原生代码 (using languages such as C and C++) 实现应用的各个部分。对于特定类型的应用,这可以帮助您重复使用以这些语言编写的代码库。

The Android Native Development Kit (NDK) provides a cross-compiling tool for compiling code written in C/C++ can be compiled to ARM, or x86 native code (or their 64-bit variants) for Android. The NDK uses the Clang compiler to compile C/C++. GCC was included until NDK r17, but removed in r18 in 2018.

The NDK includes support for CMake and its own ndk-build (based on GNU Make). For CPU profiling, the NDK also includes simpleperf which is similar to the Linux perf tool, but with better support for Android and specifically for mixed Java/C++ stacks.

2. NDK Downloads

https://developer.android.com/ndk/downloads

https://github.com/android/ndk/wiki

Select the NDK package for your development platform. In most cases it will be easier to use the Android SDK manager to install the NDK.

2.1. Latest LTS Version (r26d)

android {
    ndkVersion "26.3.11579264"
}
Platform Package Size (Bytes) SHA1 Checksum
Windows 64-bit android-ndk-r26d-windows.zip 665022840 c7ea35ffe916082876611da1a6d5618d15430c29
Mac android-ndk-r26d-darwin.dmg 1655224062 703100c3d721b04e09f02f3fddc5f1f5ced28b10
Linux 64-bit (x86) android-ndk-r26d-linux.zip 668556491 fcdad75a765a46a9cf6560353f480db251d14765
https://dl.google.com/android/repository/android-ndk-r26d-linux.zip

2.2. Old Unsupported Versions

https://github.com/android/ndk/wiki/Unsupported-Downloads

Note: these obsolete versions of the NDK are no longer supported.

r25c

https://dl.google.com/android/repository/android-ndk-r25c-linux.zip

r24

https://dl.google.com/android/repository/android-ndk-r24-linux.zip

r23c

https://dl.google.com/android/repository/android-ndk-r23c-linux.zip

r22b

https://dl.google.com/android/repository/android-ndk-r22b-linux-x86_64.zip

r21e

3. 安装 NDK

https://dl.google.com/android/repository/android-ndk-r21e-linux-x86_64.zip
  1. 下载并解压 android-ndk-r26d-linux.zip

wget https://dl.google.com/android/repository/android-ndk-r26d-linux.zip --no-check-certificate

(base) yongqiang@yongqiang:~/software$ wget https://dl.google.com/android/repository/android-ndk-r26d-linux.zip
(base) yongqiang@yongqiang:~/software$ chmod a+x android-ndk-r26d-linux.zip
(base) yongqiang@yongqiang:~/software$ unzip android-ndk-r26d-linux.zip
(base) yongqiang@yongqiang:~/software$ rm -rf android-ndk-r26d-linux.zip
(base) yongqiang@yongqiang:~/software$ cd android-ndk-r26d/
(base) yongqiang@yongqiang:~/software/android-ndk-r26d$ pwd
/home/yongqiang/software/android-ndk-r26d
(base) yongqiang@yongqiang:~/software/android-ndk-r26d$
  1. 为当前用户永久配置环境变量

~/.bashrc

(base) yongqiang@yongqiang:~$ vim ~/.bashrc
...
export NDKROOT=/home/yongqiang/software/android-ndk-r26d
export PATH=${NDKROOT}:${PATH}
...
(base) yongqiang@yongqiang:~$ source ~/.bashrc
  1. 为所有用户永久配置环境变量

/etc/profile

(base) yongqiang@yongqiang:~$ vim /etc/profile
...
export NDKROOT=/home/yongqiang/software/android-ndk-r26d
export PATH=${NDKROOT}:${PATH}
...
(base) yongqiang@yongqiang:~$ source /etc/profile
  1. 验证 NDK 安装

ndk-build -v

(base) yongqiang@yongqiang:~$ ndk-build -v
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ echo ${PATH}
/home/yongqiang/software/android-ndk-r26d:/home/yongqiang/vulkan/1.3.239.0/x86_64/bin:/home/yongqiang/miniconda3/bin:/home/yongqiang/miniconda3/condabin:/home/yongqiang/vulkan/1.3.239.0/x86_64/bin:
...

4. Get started with the NDK (NDK 使用入门)

https://developer.android.com/ndk/guides

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input. The NDK may not be appropriate for most novice Android programmers who need to use only Java code and framework APIs to develop their apps.

Native Development Kit (NDK) 是一套工具,使你能够在 Android 应用中使用 C 和 C++ 代码,并提供众多平台库,你可使用这些平台库管理原生 activity 和访问实体设备组件,例如传感器和触控输入。NDK 可能不适合大多数 Android 编程初学者,这些初学者只需使用 Java 代码和框架 API 开发应用。

However, the NDK can be useful for cases in which you need to do one or more of the following:

  • Squeeze extra performance out of a device to achieve low latency or run computationally intensive applications, such as games or physics simulations.
    进一步提升设备性能,以降低延迟或运行游戏或物理模拟等计算密集型应用。
  • Reuse your own or other developers' C or C++ libraries.
    重复使用你自己或其他开发者的 C 或 C++ 库。

Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework.

你可以在 Android Studio 2.2 或更高版本中使用 NDK 将 C 和 C++ 代码编译到原生库中,然后使用 Android Studio 的集成构建系统 Gradle 将原生库打包到 APK 中。Java 代码随后可以通过 Java 原生接口 (JNI) 框架调用原生库中的函数。

Android Studio's default build tool to compile native libraries is CMake. Android Studio also supports ndk-build due to the large number of existing projects that use the build toolkit. However, if you are creating a new native library, you should use CMake.

Android Studio 编译原生库的默认构建工具是 CMake。由于很多现有项目都使用 ndk-build 构建工具包,因此 Android Studio 也支持 ndk-build。不过,如果您要创建新的原生库,则应使用 CMake。

The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android.

这套工具使您能在 Android 应用中使用 C 和 C++ 代码。

CMake: an external build tool that works alongside Gradle to build your native library. You do not need this component if you only plan to use ndk-build.

如果你只计划使用 ndk-build,则不需要此组件。

LLDB: the debugger Android Studio uses to debug native code.

Android Studio 用于调试原生代码的调试程序。

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

相关推荐
未来可期LJ20 天前
【Linux 内存管理】
内存管理·linux 系统
Yongqiang Cheng20 天前
Linux 系统中的 /dev/null 文件
linux 系统·/dev/null