Including shared objects that library uses in Andr

2019-05-31 15:27发布

问题:

My Android project is using an Android library project that has native code and I'm getting an UnsatisifiedLinkError from a native function that library uses.

I've included in the Android.mk file of my project these shared objects the library uses and they are correctly copied over to the /libs directory of the Eclipse project when I build and are present in the apks /lib directory when I run. The method not found should be in the libVuforiaTracking.so.

Here's my Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := QCAR-prebuilt
LOCAL_SRC_FILES = ../../VuforiaTracking/libs/$(TARGET_ARCH_ABI)/libQCAR.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := VuforiaTracking-prebuilt
LOCAL_SRC_FILES = ../../VuforiaTracking/libs/$(TARGET_ARCH_ABI)/libVuforiaTracking.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := MyProject

# The list of shared libraries *modules* this module depends on at runtime.
# This is necessary at link time and to embed the corresponding information
# in the generated file. We reference the prebuilt library defined earlier 
# in this makefile.
LOCAL_SHARED_LIBRARIES := QCAR-prebuilt VuforiaTracking-prebuilt

#LOCAL_SRC_FILES := MyProject.cpp

# By default, ARM target binaries will be generated in 'thumb' mode, where
# each instruction are 16-bit wide. You can define this variable to 'arm'
# if you want to force the generation of the module's object files in
# 'arm' (32-bit instructions) mode
LOCAL_ARM_MODE := arm

include $(BUILD_SHARED_LIBRARY)

And here's the log message:

12-21 09:53:48.220: E/AndroidRuntime(13086): FATAL EXCEPTION: main
12-21 09:53:48.220: E/AndroidRuntime(13086): java.lang.UnsatisfiedLinkError: Native method not found: com.qualcomm.QCARSamples.ImageTargets.VuforiaTrackingActivity.getOpenGlEsVersionNative:()I
12-21 09:53:48.220: E/AndroidRuntime(13086):    at com.qualcomm.QCARSamples.ImageTargets.VuforiaTrackingActivity.getOpenGlEsVersionNative(Native Method)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at com.qualcomm.QCARSamples.ImageTargets.VuforiaTrackingActivity.getInitializationFlags(VuforiaTrackingActivity.java:293)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at com.qualcomm.QCARSamples.ImageTargets.VuforiaTrackingActivity.onCreate(VuforiaTrackingActivity.java:282)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.app.Activity.performCreate(Activity.java:5191)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.os.Looper.loop(Looper.java:137)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at android.app.ActivityThread.main(ActivityThread.java:4898)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at java.lang.reflect.Method.invokeNative(Native Method)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at java.lang.reflect.Method.invoke(Method.java:511)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-21 09:53:48.220: E/AndroidRuntime(13086):    at dalvik.system.NativeStart.main(Native Method)

Is that the correct way to include shared objects of a library dependency?

Edit:

I noticed the package name says com.qualcomm.QCARSamples.ImageTargets.VuforiaTrackingActivity and I should probably note that I'm subclassing this activity of the library in my project, so the package name of the actual activity would be different. Would this cause any problems when the JNI goes to look for a method named com.qualcomm.QCARSamples.ImageTargets.VuforiaTrackingActivity.getOpenGlEsVersionNative() and my subclass is named com.my.package.MyActivity?

回答1:

The issue was that I changed the name of the class that used the native method to VuforiaTrackingActivity when the native method was called VuforiaTracking. So the shared objects were in fact loaded correctly, and it didn't matter that I was extending the base class the native method names were based off of. I changed the method com.qualcomm.QCARSamples.ImageTargets.VuforiaTracking.openGlEsVersionNative to com.qualcomm.QCARSamples.ImageTargets.VuforiaTrackingActivity.openGlEsVersionNative and everything works accordingly.