apidae
apidae

Reputation: 531

Android NDK dlopen failed: cannot locate symbol "ASensor_getReportingMode" referenced by "lib*.so"

I have a project that uses Android NDK to access the accelerometer sensor of my device. The application works, except if I try to use the ASensor_getReportingMode() or ASensor_isWakeUpSensor() since it doesn't find the symbols. It probably happens with other functions as well. Strangely there are functions of the same header file such as ASensor_getName() or ASensor_getVendor() that work without problem. Does anyone know what could be missing here?

build.gradle (Module)

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion 24
        buildToolsVersion "24.0.0"

        defaultConfig {
            applicationId "com.apidae.sensorndk"
            minSdkVersion.apiLevel 19
            targetSdkVersion.apiLevel 24
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file('proguard-rules.pro'))
            }
        }
        ndk {
            moduleName "ndkfeatures"
            ldLibs.addAll([ 'android', 'log'])
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
}

Java Class

package com.apidae.sensorndk;

public class NdkFeatures {
    static {
        System.loadLibrary("ndkfeatures");
    }
    public native boolean init();
} 

C++ Source segment

#include <android/sensor.h>
...
    Log::info("Sensor Info:\n  Name:\t%s\n  Vendor:\t%s\n  Mode:\t%d",
        ASensor_getName(mAccelerometer),
        ASensor_getVendor(mAccelerometer),
        ASensor_getReportingMode(mAccelerometer)); // <- Taking this line out runs
...

build.gradle (App)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.7.2'
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Full Exception:

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.apidae.sensorndk, PID: 6855
        java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "ASensor_getReportingMode" referenced by "libndkfeatures.so"...
            at java.lang.Runtime.loadLibrary(Runtime.java:371)
            at java.lang.System.loadLibrary(System.java:553)
            at com.apidae.sensorndk.NdkFeatures.<clinit>(NdkFeatures.java:6)
            at com.apidae.sensorndk.MainActivity.onCreate(MainActivity.java:13)
            at android.app.Activity.performCreate(Activity.java:5264)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 2

Views: 13199

Answers (1)

apidae
apidae

Reputation: 531

I figured this out finally. This happens because the target SDK android-24 that I was using to compile didn't match the android-19 on my device . By using the correct target SDK I can see at compile time that the functions I'm trying to use aren't supported

Source: https://stackoverflow.com/a/27338365/6578619

Upvotes: 2

Related Questions