如何在android apk中添加第三方C库?

时间:2014-04-01 10:44:33

标签: android

我有一个预建第三方C库libliveplay.so,我想将它添加到apk。我把它放在

之下
libs/armeabi
libs/armeabi-v7a

我的android.mk文件就像:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := RtspPlayer
LOCAL_CERTIFICATE := platform
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_DEX_PREOPT := false

LOCAL_SHARED_LIBRARIES := libliveplay

include $(BUILD_PACKAGE)
include $(call all-makefiles-under,$(LOCAL_PATH))

但它不能用这种方法添加到apk中,有人可以帮忙吗?

当apk构建时,我将其安装在我的设备上,并发生运行时错误:

E/AndroidRuntime( 4660): FATAL EXCEPTION: main
E/AndroidRuntime( 4660): java.lang.ExceptionInInitializerError
E/AndroidRuntime( 4660):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 4660):    at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime( 4660):    at android.app.Instrumentation.newActivity(Instrumentation.java:1064)
E/AndroidRuntime( 4660):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
E/AndroidRuntime( 4660):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime( 4660):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 4660):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime( 4660):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4660):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 4660):    at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime( 4660):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4660):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 4660):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime( 4660):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime( 4660):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 4660): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load liveplay from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.mrlans.play-2.apk,libraryPath=/data/app-lib/com.mrlans.play-2]: findLibrary returned null
E/AndroidRuntime( 4660):    at java.lang.Runtime.loadLibrary(Runtime.java:365)
E/AndroidRuntime( 4660):    at java.lang.System.loadLibrary(System.java:535)
E/AndroidRuntime( 4660):    at com.mrlans.play.MediaActivity.<clinit>(MediaActivity.java:152)
E/AndroidRuntime( 4660):    ... 15 more

错误表明所以没有包含在apk中,当安装apk时它没有安装到系统中。请帮忙。

1 个答案:

答案 0 :(得分:0)

您需要将libliveplay包含为PREBUILT库。将libliveplay.so放在/ jni目录中,使结构如下:

Android.mk                 --> the file above
armeabi/libliveplay.so     --> the armeabi prebuilt shared library
armeabi-v7a/libliveplay.so --> the armeabi-v7a prebuilt shared library

然后在Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := liveplay
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libliveplay.so
include $(PREBUILT_SHARED_LIBRARY)


include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := RtspPlayer
LOCAL_CERTIFICATE := platform
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_DEX_PREOPT := false

LOCAL_SHARED_LIBRARIES := liveplay

include $(BUILD_PACKAGE)
include $(call all-makefiles-under,$(LOCAL_PATH))