从Android原生活动加载第三方共享库 [英] Loading 3rd party shared libraries from an Android native activity
问题描述
我已经建立了Assimp库作为共享库。我已经把它包含在我的Android NDK项目,它建立正常,但当我打开它,我得到以下错误: 无法加载本地库:我的母语,活动
(也许我要补充一点,我的活动正常工作时,不包括图书馆和我已经检查了apk文件和设备上,该库被添加到库文件夹,并安装在设备上的/数据/数据/我的应用程序内/ lib目录下。)
我已经做了很多的阅读,似乎解决了这个唯一的办法就是他们使用的System.loadLibrary发动我的祖国活动之前加载。我想我会preFER加载它们动态地使用dlopen采取这种办法之前。
我是正确的假设机器人不会自动加载的共享库,我的祖国的活动(即我的共享库)取决于?
我将建立它作为一个静态库,但它是在54MB它无法工作。
这是我的Android.mk: 我试着加入-lassimp到LOCAL_LDLIBS。我不知道这是正确的,但它并没有任何区别。
LOCAL_PATH:= $(叫我-DIR)
包括$(CLEAR_VARS)
LOCAL_MODULE:= assimp
LOCAL_SRC_FILES:= libassimp.so
包括$(preBUILT_SHARED_LIBRARY)
包括$(CLEAR_VARS)
LOCAL_MODULE:=天然活性
LOCAL_SRC_FILES:= main.cpp中
LOCAL_LDLIBS:= -llog -landroid -lEGL -lGLESv2
LOCAL_STATIC_LIBRARIES:= android_native_app_glue
LOCAL_SHARED_LIBRARIES:= assimp
LOCAL_CPPFLAGS + = -march =的ARMv7-A -mfloat-ABI = softfp
LOCAL_CFLAGS:= $(LOCAL_CPPFLAGS)
TARGET_ARCH_ABI:= armeabi-V7A
LOCAL_C_INCLUDES + = $(LOCAL_PATH)
包括$(BUILD_SHARED_LIBRARY)
$(呼叫进口模块,机器人/ native_app_glue)
子类android.app.NativeActivity是解决这一问题的最简单的方法。
包com.you;
公共类MyNativeActivity扩展android.app.NativeActivity {
静态{
的System.loadLibrary(assimp);
}
}
然后改变你的的AndroidManifest.xml
。替换 android.app.NativeActivity
与 MyNativeActivity
键,删除标记有code = 假
。
作为一个侧面说明,Android的做加载一个共享库时,搜索依赖。但是,搜索范围仅限于 /系统/ lib目录
。
I've built the Assimp library as a shared library. I've included it in my Android ndk project and it builds fine but when I load it I get the following error: Unable to load native library: My-Native-Activity.
(Perhaps I should add that my activity works fine when the library is not included and that I've checked the apk and on the device; the library is being added to the libs folder and installed on the device in /data/data/my-app/lib.)
I've done a lot of reading and it seems that the only way to solve this is to load them using System.loadLibrary before launching my native activity. I think I'd prefer load them dynamically using dlopen before taking that approach.
Am I correct in assuming that Android wont automatically load the shared libraries my native activity(i.e. my shared library) depends on?
I would build it as a static library but it was over 54Mb which wont work.
This is my Android.mk: I've tried adding -lassimp to LOCAL_LDLIBS. I'm not sure if that would be correct but it didn't make any difference.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := assimp
LOCAL_SRC_FILES := libassimp.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := native-activity
LOCAL_SRC_FILES := main.cpp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2
LOCAL_STATIC_LIBRARIES := android_native_app_glue
LOCAL_SHARED_LIBRARIES := assimp
LOCAL_CPPFLAGS += -march=armv7-a -mfloat-abi=softfp
LOCAL_CFLAGS := $(LOCAL_CPPFLAGS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_C_INCLUDES += $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
Subclassing android.app.NativeActivity is the simplest way to solve this problem.
package com.you;
public class MyNativeActivity extends android.app.NativeActivity {
static {
System.loadLibrary("assimp");
}
}
Then change your AndroidManifest.xml
. Replace android.app.NativeActivity
with MyNativeActivity
and remove the tag hasCode="false"
.
As a side note, Android does search for dependencies when loading a shared library. But the scope of the search is limited to /system/lib
.
这篇关于从Android原生活动加载第三方共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!