http://ikinglai.blog.51cto.com/6220785/1324985
At the time of Android development, a static library of third parties written in C or C + + is often used. If there is a source, you can directly with your own code to compile into a dynamic library so, but if there is no source, you must be in their own dynamic library so inside the others generated a good static library into the compilation. I encountered a lot of problems in compiling, I think it is necessary to summarize.
Let me explain in a simple practical example how to import a static library in a dynamic library.
The source code in the static library has two files: static.h, STATIC.C, has an Add method
Static.h
| 12 |
#include <stdio.h>intadd(int x, inty); |
Static.c
| 12345 |
#include "static.h"intadd(int x, int y){ returnx + y;} |
Compile it into a static library, android.mk as follows:
| 12345 |
LOCAL_PATH := $(call my-dir)include$(CLEAR_VARS)LOCAL_MODULE := static_addLOCAL_SRC_FILES := static.cinclude$(BUILD_STATIC_LIBRARY) |
Note When compiling a static library, you must have a application.mk file:
| 1 |
APP_MODULES:=static_add |
The value of app_modules should remain the same as the value of Local_module in Android.mk.
Then call Ndk-build to compile the build libstatic_add.a static library.
Hejinlai_imac:jni hejinlai$ Ndk-build
PREBUILT:LIBSTATIC_ADD.A <= jni/
After you build the static library, then write the source code in the dynamic library: share.h share.c
Share.h
| 12 |
#include<stdio.h>int test_add(int x, inty); |
Share.c
| 1234567 |
#include"share.h"#include "static.h"int test_add(int x, int y){ // 调用static里面的方法 returnadd(x, y);} |
Write android.mk to import the static library:
| 12345678910 |
LOCAL_PATH := $(call my-dir)include$(CLEAR_VARS)LOCAL_MODULE := static_addLOCAL_SRC_FILES := libstatic_add.ainclude $(PREBUILT_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := share_addLOCAL_STATIC_LIBRARIES := static_addLOCAL_SRC_FILES := share.cinclude$(BUILD_SHARED_LIBRARY) |
Note that the LIBSTATIC_ADD.A generated above must be placed in the same directory as ANDROID.MK, otherwise the corresponding path needs to be filled in and then compiled:
Hejinlai_imac:jni hejinlai$ Ndk-build
Compile Thumb:share_add <= share.c
PREBUILT:LIBSTATIC_ADD.A <= jni/
SharedLibrary:libshare_add.so
Install:libshare_add.so = libs/armeabi/libshare_add.so
Prompted so to compile successfully.
Note that my side share.c and STATIC.C are placed in the same directory, if placed in a different directory, you need to specify
Local_c_includes link to the appropriate path.
Android Imports third-party static libraries. A is compiled into a dynamic library. So