首先了解几个名词
NDK
The Android Native Development Kit
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.
CMake
跨平台外部编译工具,跟Gradle一起编译本地库文件(libarary)。如果只是使用ndk-build编译,无需安装。Ndk-build和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.
在gradle中指定cmake版本
android {
...
externalNativeBuild {
cmake {
...
version "cmake-version"
}
}
}
CMakeLists.txt
A CMake build script
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add_library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library( # Specifies the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
# Specifies a path to native header files.
include_directories(src/main/cpp/include/)
通过上述配置,将按规则 liblibrary-name.so 生成 libnative-lib.so
java代码里的引用
static {
System.loadLibrary("native-lib");
}
LLDB
Android Studio 通过它来调试native code。LLDB跟Android Studio一起安装
The debugger Android Studio uses to debug native code. By default, LLDB will be installed alongside Android Studio.
SDK
AGP Android Gradle plugin
gradle
externalNativeBuild {
cmake {
// Sets optional flags for the C++ compiler.
cppFlags "-frtti -fexceptions -std=gnu++0x -DHAVE_PTHREADS -fpermissive -O3 -Wall -Werror -###"
// Passes optional arguments to CMake.
arguments "-DPRODUCT_CONFIG=effects"
arguments "-DANDROID_STL=c++_shared"
// Sets a flag to enable format macro constants for the C compiler.
cFlags "-D__STDC_FORMAT_MACROS"
}
}
ndk {
abiFilters 'arm64-v8a','armeabi-v7a'
}
https://developer.android.com/studio/projects/gradle-external-native-builds
Ninja
JNI
Java Native Interface
Java或者Kotlin代码通过JNI调研Navtive Library的函数
Your Java or Kotlin code can then call functions in your native library through the Java Native Interface (JNI).
参考
https://developer.android.com/studio/projects/install-ndk#groovy
Android CMake_仰望XX的博客-CSDN博客
Android 开发:CMake 使用_android cmake_zxy_de_android的博客-CSDN博客
Android-CMake语法 - 简书文章来源:https://www.toymoban.com/news/detail-495896.html
Android NDK开发扫盲及最新CMake的编译使用 - 知乎文章来源地址https://www.toymoban.com/news/detail-495896.html
到了这里,关于Android CMake的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!