在 Linux 系统上安装 Android NDK

打印 上一主题 下一主题

主题 511|帖子 511|积分 1533

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)

  1. android {
  2.     ndkVersion "26.3.11579264"
  3. }
复制代码
PlatformPackageSize (Bytes)SHA1 ChecksumWindows 64-bitandroid-ndk-r26d-windows.zip665022840c7ea35ffe916082876611da1a6d5618d15430c29Macandroid-ndk-r26d-darwin.dmg1655224062703100c3d721b04e09f02f3fddc5f1f5ced28b10Linux 64-bit (x86)android-ndk-r26d-linux.zip668556491fcdad75a765a46a9cf6560353f480db251d14765
  1. 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
  1. https://dl.google.com/android/repository/android-ndk-r25c-linux.zip
复制代码
r24
  1. https://dl.google.com/android/repository/android-ndk-r24-linux.zip
复制代码
r23c
  1. https://dl.google.com/android/repository/android-ndk-r23c-linux.zip
复制代码
r22b
  1. https://dl.google.com/android/repository/android-ndk-r22b-linux-x86_64.zip
复制代码
r21e
3. 安装 NDK

  1. https://dl.google.com/android/repository/android-ndk-r21e-linux-x86_64.zip
复制代码

  • 下载并解压 android-ndk-r26d-linux.zip
wget https://dl.google.com/android/repository/android-ndk-r26d-linux.zip
--no-check-certificate
  1. (base) yongqiang@yongqiang:~/software$ wget https://dl.google.com/android/repository/android-ndk-r26d-linux.zip
  2. (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$
复制代码

  • 为当前用户永世配置环境变量
~/.bashrc
  1. (base) yongqiang@yongqiang:~$ vim ~/.bashrc
  2. ...
  3. export NDKROOT=/home/yongqiang/software/android-ndk-r26d
  4. export PATH=${NDKROOT}:${PATH}
  5. ...
  6. (base) yongqiang@yongqiang:~$ source ~/.bashrc
复制代码

  • 为全部用户永世配置环境变量
/etc/profile
  1. (base) yongqiang@yongqiang:~$ vim /etc/profile
  2. ...
  3. export NDKROOT=/home/yongqiang/software/android-ndk-r26d
  4. export PATH=${NDKROOT}:${PATH}
  5. ...
  6. (base) yongqiang@yongqiang:~$ source /etc/profile
复制代码
/etc/profile 是用户登录后自动实行的第一个 shell 脚本,用来设置全局的环境变量,对全部用户有效。实行完 /etc/profile 脚本后,才回去实行用户自己的环境脚本,如 ~/.bashrc 等。

  • 验证 NDK 安装
ndk-build -v
  1. (base) yongqiang@yongqiang:~$ ndk-build -v
  2. GNU Make 4.3
  3. Built for x86_64-pc-linux-gnu
  4. Copyright (C) 1988-2020 Free Software Foundation, Inc.
  5. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  6. This is free software: you are free to change and redistribute it.
  7. There is NO WARRANTY, to the extent permitted by law.
  8. (base) yongqiang@yongqiang:~$
  9. (base) yongqiang@yongqiang:~$ echo ${PATH}
  10. /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:
  11. ...
复制代码
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/

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立聪堂德州十三局店

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表