在需要引入第三方的so文件的情况下,怎样使用NDK编译本地代码?
1.新建jni文件夹,将你要编译源码的文件以及需要引入的so文件复制到jni文件目录下
2.编写android.mk文件,首先将你的将你的本地代码文件的android.mk部分的代码写好,然后在这部分后面添加需要引入的so文件的代码
参考格式如下:
1.全部写在一个文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libyour_module_name
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := your_module_src.cpp
include $(BUILD_SHARED_LIBRARY)
#下面为第三方的库
include $(CLEAR_VARS)
LOCAL_MODULE := liblocSDK3
LOCAL_SRC_FILES := liblocSDK3.so
include $(PREBUILT_SHARED_LIBRARY)
2.单独引入
我们也可以为每一个第三方库,写一个.mk文件,然后在主的.mk文件中进行引入
Android_ffmpeg.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := libffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg_jni
LOCAL_SRC_FILES := ffmpeg_jni.c
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/ $(LOCAL_PATH)/include/libavutil/ $(LOCAL_PATH)/include/libavcodec/ $(LOCAL_PATH)/include/libavformat/ $(LOCAL_PATH)/include/libavcodec/ $(LOCAL_PATH)/include/libswscale/
LOCAL_LDLIBS += -L$(LOCAL_PATH)/prebuilt/ -lffmpeg -llog
include $(BUILD_SHARED_LIBRARY)
include $(LOCAL_PATH)/prebuilt/Android_ffmpeg.mk
以上部分就是百度定位sdk的so文件引入时的android.mk部分的编写,如果你还需要其他的so文件呢,你只需要在这部分的后面再加上这样的代码,然后将名字替换成那个so文件的名即可。
编写好了之后,我们编译工程,最后我们会在armeabi等文件夹中,看到两个so文件,libyour_module_name.so以及liblocSDK3.so文件,如果你是直接将需要引入的so文件导入到armeabi文件夹下,那么编译后你将只会看见libyour_module_name.so文件。
3.单独引用(推荐)
首先可以为每一个第三方库,写一个.mk文件,然后在主的.mk文件中进行调用
Android.mk (第三方库)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := libffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)
Android.mk (自己写的jni wrapper)
#added by rx 150425
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := player
SDL_PATH := ../SDL2
FFMPEG_PATH := ../ffmpeg
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(SDL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(FFMPEG_PATH)/include
# Add your application source files here...
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c
#LOCAL_SRC_FILES += SDLtest.c
#LOCAL_SRC_FILES += SDLTestAudio.c
LOCAL_SRC_FILES += sdl2Lesson.c
#添加本地共享库
LOCAL_SHARED_LIBRARIES := SDL2
LOCAL_SHARED_LIBRARIES += ffmpeg
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog
include $(BUILD_SHARED_LIBRARY)
Android.mk (最外层的mk文件调用当前所有子目录的mk文件)
include $(call all-subdir-makefiles)
目录结构示意图.png
网友评论
Android.mk 已配置好,#include "libavformat/avformat.h" 这个写法还是编译不了