android-studioandroid-ndkopensl

Android ndk example native-audio error


I try to use the example code for OpenSl ES wich is included in NDK in Android Studio. It didn't work as you will see later on. So I need help to use the code in Android Studio. The Code was written for Eclipse but I used the import function from Android Studio to include it. I tried it Eclipse also and it worked fine but when trying to run the code in Android Studio I get the following Errorcode

/home/flex/AndroidStudioProjects/native-audio1/app/src/main/jni/native-audio-jni.c
Error:(155) undefined reference to `slCreateEngine'
Error:(165) undefined reference to `SL_IID_ENGINE'
Error:(165) undefined reference to `SL_IID_ENGINE'
Error:(170) undefined reference to `SL_IID_ENVIRONMENTALREVERB'
Error:(170) undefined reference to `SL_IID_ENVIRONMENTALREVERB'
Error:(215) undefined reference to `SL_IID_BUFFERQUEUE'
Error:(215) undefined reference to `SL_IID_EFFECTSEND'
Error:(215) undefined reference to `SL_IID_VOLUME'
Error:(215) undefined reference to `SL_IID_BUFFERQUEUE'

...

collect2: error: ld returned 1 exit status
make: *** [/home/flex/AndroidStudioProjects/native-audio1/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/libnative-audio-jni.so] Error 1
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/home/flex/android-ndk-r10e/ndk-build'' finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 2.942 secs
Information:38 errors
Information:0 warnings
Information:See complete output in console

What I have done so far: I changed build.gradle (app) to

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.nativeaudio"
        minSdkVersion 19
        targetSdkVersion 19

        ndk {
            moduleName "native-audio-jni" //I included this one
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

}

Further on I changed local.properties to

sdk.dir=/home/flex/Android/Sdk
ndk.dir=/home/flex/android-ndk-r10e

My Android.mk looks like this

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := native-audio-jni
LOCAL_SRC_FILES := native-audio-jni.c
# for native audio
LOCAL_LDLIBS    += -lOpenSLES
# for logging
LOCAL_LDLIBS    += -llog
# for native asset manager
LOCAL_LDLIBS    += -landroid

include $(BUILD_SHARED_LIBRARY)

my apllication.mk:

APP_ABI := all

Solution

  • Solution was to compile the source code twice.

    So in the terminal I compiled the first time with the command : ndk-build
    And the second time I used the command: ndk-build TARGET_PLATFORM=android-xy
    xy must be replaced with android version

    This is definitly not the correct way but at least it made it work.