我有一个项目使用Android NDK访问我设备的加速计传感器。该应用程序有效,除非我尝试使用ASensor_getReportingMode()
或ASensor_isWakeUpSensor()
,因为它找不到符号。它也可能与其他功能一起发生。奇怪的是,相同的头文件的功能,如ASensor_getName()
或ASensor_getVendor()
,可以正常工作。有谁知道这里可能缺少什么?
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 ++源代码段
#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
}
完全例外:
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)
答案 0 :(得分:1)
我终于明白了。这是因为我用来编译的目标SDK android-24
与我设备上的android-19
不匹配。通过使用正确的目标SDK,我可以在编译时看到我试图使用的功能不受支持