柚子快報(bào)邀請(qǐng)碼778899分享:Android CMake
柚子快報(bào)邀請(qǐng)碼778899分享:Android CMake
首先了解幾個(gè)名詞
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
跨平臺(tái)外部編譯工具,跟Gradle一起編譯本地庫(kù)文件(libarary)。如果只是使用ndk-build編譯,無(wú)需安裝。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/)
通過(guò)上述配置,將按規(guī)則? liblibrary-name.so? 生成??libnative-lib.so
java代碼里的引用
static {
System.loadLibrary("native-lib");
}
LLDB
Android Studio 通過(guò)它來(lái)調(diào)試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代碼通過(guò)JNI調(diào)研Navtive Library的函數(shù)
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 開(kāi)發(fā):CMake 使用_android cmake_zxy_de_android的博客-CSDN博客
Android-CMake語(yǔ)法 - 簡(jiǎn)書(shū)
Android NDK開(kāi)發(fā)掃盲及最新CMake的編譯使用 - 知乎
柚子快報(bào)邀請(qǐng)碼778899分享:Android CMake
參考閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。