如今愈來愈多的app須要進行ndk開發,尤爲音視頻這塊,本人最近準備學習android音視頻開發,發現ndk這塊繞不過去,因此就記錄下學習的點滴,方便之後查閱。html
在 android studio 上進行 ndk 開發,相關的工具確定少不了了,準備很方便,打開 SDK Tools ,勾選LLDB、CMake、NDK 進行安裝便可。java
在新建項目時,勾選 C++ 支持android
一路 next ,最後一步,設置 C++ 標準,這個東西我不太懂,反正使用默認的 Toolchain Default 時,工程使用的是 Cmake 對 native code 進行編譯,網上好多ndk開發環境搭建的教程是根據 Android.mk、Application.mk寫的,在Cmake下跑不起來(一把淚~~)bash
新建的 ndk 開發項目結構上與普通項目的區別主要有如下幾點:app
重點看下 CMakeLists.txt 文件,註釋解釋的挺清楚的ide
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets 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 )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
複製代碼
這裏在放一個 別人翻譯的做爲參考工具
# 有關使用CMake在Android Studio的更多信息,請閱讀文檔:https://d.android.com/studio/projects/add-native-code.html
# 設置CMake的最低版本構建本機所需庫
cmake_minimum_required(VERSION 3.4.1)
# 建立並命名庫,將其設置爲靜態的
# 或共享,並提供其源代碼的相對路徑。
# 你能夠定義多個library庫,並使用CMake來構建。
# Gradle會自動將包共享庫關聯到你的apk程序。
add_library( # 設置庫的名稱
native-lib
# 將庫設置爲共享庫。
SHARED
# 爲源文件提供一個相對路徑。
src/main/cpp/native-lib.cpp )
# 搜索指定預先構建的庫和存儲路徑變量。由於CMake包括系統庫搜索路徑中默認狀況下,只須要指定想添加公共NDK庫的名稱,在CMake驗證庫以前存在完成構建
find_library( # 設置path變量的名稱
log-lib
# 在CMake定位前指定的NDK庫名稱
log )
# 指定庫CMake應該連接到目標庫中,能夠連接多個庫,好比定義庫,構建腳本,預先構建的第三方庫或者系統庫
target_link_libraries( # 指定目標庫
native-lib
# 目標庫到日誌庫的連接 包含在NDK
${log-lib} )
複製代碼
下載構建下項目,就是點擊 adroid studio 菜單 Build—>Rebuild Project,就會自動把 native 源文件編譯爲各個平臺用的 .so 文件。學習
安裝到設備後工程就能夠正常運行了,具體的 java/native 代碼調用方式後面再說。gradle
#include "com_wyt_one_MainActivity.h"
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL Java_com_wyt_one_MainActivity_myNativeMethod( JNIEnv *env, jobject /* this */) {
return env->NewStringUTF("myNativeMethod 執行了");
}
複製代碼
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
MyNative
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/MyNative.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
MyNative
# Links the target library to the log library
# included in the NDK.
${log-lib} )
複製代碼
本文介紹了 ndk 開發所須要的工具,對新建立項目和已有項目兩種 ndk 開發場景下的環境搭建的流程進行了描述。ui