https://developer.android.com/ndk/guides/index.html(NDK开发一)

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. 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.

本机开发工具包(NDK)是一组工具,允许您使用Android的C和C++代码,并提供平台库,您可以使用这些库来管理本地活动和访问物理设备组件,例如传感器和触摸输入。对于使用Java代码和系统API开发的新手来说,你用不到NDK,但是对于以下的情况,你需要使用NDK

  • 从设备中挤出额外的性能,以实现低延迟或运行计算密集型应用程序,例如游戏或物理模拟。
  • 重用你的或者其他人的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++代码编译成一个本地库,并将其打包到您的APK中,这是IDE的集成构建系统。然后,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来编译,如果你要构建一个新的native库,你应该使用Cmake。

要使用或者调试NDK,你需要下载如下组件

  • The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android.
  • 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.
  • LLDB: the debugger Android Studio uses to debug native code.(用于调试native code)

这里写图片描述

以下内容来自这里

You can add C and C++ code to your Android project by placing the code into a cpp directory in your project module. When you build your project, this code is compiled into a native library that Gradle can package with your APK. Your Java or Kotlin code can then call functions in your native library through the Java Native Interface (JNI).

你可以把你的C或者C++代码放在cpp目录下,你打包的时候他就会通过gradle打包进你的apk,你的Java或者kotlin代码可以通过JNI调用native代码(关于JNI下一篇写)

If you want to import an existing ndk-build library into your Android Studio project, learn how to link Gradle to your native library project. However, if you are creating a new native library, you should use CMake.

如果你想导入已经存在的ndk-build库,我之后的博客会写(关于ndk-build),如果你想新建一个native库,你应该使用CMake.

Specify the CMake version you want Gradle to use in your module’s build.gradle file:

android {
    ...
    externalNativeBuild {
        cmake {
            ...
            version "3.7.1"
        }
    }
}

Either add the path to the CMake installation to your PATH environment variable or include it in your project’s local.properties file, as shown below. If Gradle is unable to find the version of CMake you specified in your build.gradle file, you get a build error.

# If you set this property, Gradle no longer uses PATH to find CMake.
cmake.dir="path-to-cmake"

将CMake的安装地址在你的环境变量中指定或者在local.properties中指定,指定代码如上。

本博客未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值