更新 Oboe 的建構設定

將 Oboe 程式庫納入專案的方法有兩種。

將 Oboe 與 Gradle 和 CMake 整合

這些操作說明僅適用於搭配 CMake 原生依附元件並使用 Android Gradle 外掛程式 4.1.0 以上版本的專案。

如果專案使用的是 Android Gradle 外掛程式 4.0 或 ndk-build 而非 CMake,流程會稍微不同。請參閱使用原生依附元件一文。

更新 build.gradle

如要在使用 Android Gradle 外掛程式 4.1.0 以上版本時將 Oboe 新增至應用程式,請在應用程式的 build.gradle 檔案中新增以下程式碼。

  1. oboe 依附元件新增至 dependencies 部分。如有需要,請將 1.5.0 替換為 Oboe 的最新穩定版本

    dependencies {
        implementation 'com.google.oboe:oboe:1.5.0'
    }
    
  2. 在「buildFeatures」部分中啟用「prefab」選項。

    android {
        buildFeatures {
            prefab true
        }
    }
    
  3. 將應用程式設為使用共用 STL:

    android {
        defaultConfig {
            externalNativeBuild {
                cmake {
                    arguments "-DANDROID_STL=c++_shared"
                }
            }
        }
    }
    

更新 CMakeLists.txt

新增 Oboe 時,必須在應用程式的 CMakeLists.txt 檔案中加入兩個新增項目。

  1. 新增下列 find_package 指令:

    find_package (oboe REQUIRED CONFIG)
    
  2. oboe::oboe 新增到傳送至主要執行檔相關 target_link_libraries 指令的程式庫清單。

與 Android Game SDK 整合

  1. 下載程式庫,並在來源控管系統中加以確認。

  2. 對專案的建構設定進行下列變更。

靜態資料庫

整合 Android Game SDK 時,您將以靜態方式連結至以特定 ABI、API 級別、NDK 和 STL 組合編譯的 Oboe 程式庫版本:

  1. gamesdk/include 新增至編譯器,其中包含路徑。
  2. 在連結器程式庫路徑中新增以下形式的路徑:

    gamesdk/libs/architecture_APIapiLevel_NDKndkVersion_stlVersion_Release
    

    例如:gamesdk/libs/arm64-v8a_API24_NDK18_cpp_static_Release

  3. -loboe_static 加入連接器指令。

不需整合 liboboe.so 共用資料庫,表示靜態連結將提供較少的程式碼量。

共用資料庫

如果設定無法支援靜態資料庫所需的 ABI、API 級別、NDK 和 STL 組合,可以改為連結至共用資料庫。

  1. 請按照前一個工作階段 (關於靜態資料庫) 的步驟 1 和 2,更新編譯器包含路徑,並使用適當的標頭檔案。

  2. 在連結器程式庫路徑中新增以下形式的路徑:

    gamesdk/libs/architectureAPIapiLevelNDKndkVersion_stlVersion_Release/lib/oboe

  3. Add -loboe -lOpenSLES to your linker command.

Using CMake (static library only)

If you're using CMake, see the gamesdk/samples/bouncyball/app/CMakeLists.txt file in the downloaded SDK for an example CMake configuration. It includes a utility file called gamesdk/samples/gamesdk.cmake, which performs final checks, adds the proper compiler include paths, and generates a target that you can use to link the library.

To use the gamesdk.cmake utility:

  1. Include this file in your CMakeLists.txt:

    // Use a relative or absolute path. For example, /home/yourusername/gamesdk
    // or C:\Android\gamesdk.
    include("path/to/gamesdk/samples/gamesdk.cmake")
    

  2. 如要呼叫 add_gamesdk_target 函式,請使用包含 gamesdk 的資料夾:

    // Use a relative or absolute path.
    add_gamesdk_target(PACKAGE_DIR path/to/gamesdk)
    
  3. 在原生資料庫的 target_link_libraries 中,將 oboe 新增為依附元件:

    // The casing of OpenSLES is important.
    target_link_libraries(native-lib oboe OpenSLES ...) 
    

如需 CMake 的進階用法,請參閱 gamesdk.cmake 來源檔案。

後續步驟:使用 Oboe

如要使用 Oboe 播放或錄製音訊,請建立並啟用一或多個音訊串流,並使用回呼在音訊輸入/輸出裝置和應用程式之間交換音訊。參閱使用 Oboe 一文。