音視頻開發【1】--AS3.x NDK開發環境搭建

如今愈來愈多的app須要進行ndk開發,尤爲音視頻這塊,本人最近準備學習android音視頻開發,發現ndk這塊繞不過去,因此就記錄下學習的點滴,方便之後查閱。html

工具準備

在 android studio 上進行 ndk 開發,相關的工具確定少不了了,準備很方便,打開 SDK Tools ,勾選LLDB、CMake、NDK 進行安裝便可。java

ndk相關工具準備

新建ndk開發項目

在新建項目時,勾選 C++ 支持android

C++ support

一路 next ,最後一步,設置 C++ 標準,這個東西我不太懂,反正使用默認的 Toolchain Default 時,工程使用的是 Cmake 對 native code 進行編譯,網上好多ndk開發環境搭建的教程是根據 Android.mk、Application.mk寫的,在Cmake下跑不起來(一把淚~~)bash

C++標準

新建的 ndk 開發項目結構上與普通項目的區別主要有如下幾點:app

  • 新增CMakeLists.txt:CMake、ndk-build 的構建腳本
  • 新增 cpp 目錄:native 源文件、頭文件等
  • build.gradle(app)文件增長了對 externalNativeBuild 的配置

Android視圖下的工程目錄結構

Project視圖下的工程目錄結構

build.gradle(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 文件。學習

so 文件位置

安裝到設備後工程就能夠正常運行了,具體的 java/native 代碼調用方式後面再說。gradle

在已有普通項目上進行ndk開發

  1. 首先在 main 目錄下新建 cpp 文件夾,存放 native 源文件。

Alt text

  1. 在 MainActivity 中添加 native 代碼,myNativeMethod() 方法如今是紅色的,由於如今尚未對應的native層方法實現,下面繼續。

Alt text

  1. 命令行進入 src/main/java 目錄下,執行 javah 命令,生成 native 頭文件,就是 .h 文件,而後移動該 .h 文件到剛纔新建的 cpp 目錄下。

Alt text

Alt text

  1. 如今有了 native .h 頭文件,頭文件中只有對方法的聲明,沒有方法的實現,還須要對頭文件進行實現。在 cpp 目錄下新建 MyNative.cpp 文件,輸入代碼。

Alt text

#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 執行了");
}
複製代碼
  1. 通過上面幾步,ndk開發須要的源文件和 activity 裏的調用代碼已經完成了,調用代碼如今仍是紅色的,由於如今尚未對 native 源文件進行編譯,編譯須要 CMakeLists.txt 文件,上文提到過,因此在 app 目錄下新建 CMakeLists.txt 文件,把上文給出的 CMakeLists.txt 代碼粘貼進去,稍加修改(把 native-lib 都改成 MyNative)

Alt text

# 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} )
複製代碼
  1. 編輯 build.gradle(app) 文件,配置 CMake。

Alt text

  1. rebuild 工程,便可生成 .so 文件,activity 裏的 native 方法調用也不紅了

Alt text

Alt text

  1. 在界面上添加一個 Button ,點擊時調用 native 方法,將按鈕顯示的文本更新爲 native 方法返回的字符串。

Alt text

Alt text

總結

本文介紹了 ndk 開發所須要的工具,對新建立項目和已有項目兩種 ndk 開發場景下的環境搭建的流程進行了描述。ui

相關文章
相關標籤/搜索