如何在 android studio 中使用原生 OpenSL ES



我需要使用 Android Studio 在 NDK 中开发一个音频应用程序。我已添加

the ndk path to local.properties -
    ndk.dir=/opt/android-ndk-r10
    sdk.dir=/opt/adt-bundle-linux-x86_64-20140702/sdk
In build.gradle I added an entry for OpenSLES -
apply plugin: 'com.android.application'
android {
    compileSdkVersion 20
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.example.hellojni"
        minSdkVersion 8
        targetSdkVersion 21
        ndk {
            moduleName "HelloJNI"
            ldLibs "OpenSLES"       // Link with these libraries!
            stl "stlport_shared"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}
dependencies {
    compile 'com.android.support:support-v4:20.0.0'
    compile 'com.android.support:appcompat-v7:20.0.0'
}

接下来我尝试为 opensl 添加 #includes -

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

但 IDE 无法识别标头,指出它找不到包含文件。我还尝试将本机音频项目导入 android 工作室,但也没有编译或运行。我知道官方仍然不支持将NDK与Android Studio一起使用。但是我看过一些视频,展示了如何将ndk与android工作室集成。有没有办法做到这一点。提前致谢

OpenSL 库可用于具有 API 9+ 的 Android 平台,因此您可能需要更改所需的最小 SDK。

不确定 NDK 如何选择编译哪个平台,但您可能还需要使用自定义 Application.mk 文件进行编译,如下所示:

APP_ABI := armeabi
APP_PLATFORM := android-9
TARGET_PLATFORM := android-9

此外,应始终使用最高可用 SDK 进行定位和编译。

下面是构建文件的外观示例:

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.example.hellojni"
        minSdkVersion 9
        targetSdkVersion 22
        ndk {
            moduleName "HelloJNI"
            ldLibs "OpenSLES"       // Link with these libraries!
            stl "stlport_shared"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
}

注意:使用上述配置,可以为我找到库(使用内置的 ndk 配置)。

最新更新