Modules A and B are mainly implemented in C and need to be compiled into two. so, B. so, but. so calls B. so function, B. so also called. so functions, and for some reason, A and B must be compiled separately. The problem is that no matter which module is compiled first, the compilation fails because they are mutually dependent.
This dependency must be canceled during compilation. In the following program, use dlopen to open so and dlsym to obtain the function pointer to avoid this dependency.
A. c
View plain
# Include <stdio. h>
# Include <stdlib. h>
# Include <stdarg. h>
# Include <dlfcn. h>
# Include <jni. h>
Char * GetStringA (void)
{
Return "I am in a. so ";
}
Jstring Java_com_ckl_SoCallSo_SoCallSoActivity_fucntionInA (JNIEnv * env, jobject thiz)
{
Return (* env)-> NewStringUTF (env, GetStringA ());
}
Jstring Java_com_ckl_SoCallSo_SoCallSoActivity_AcallB (JNIEnv * env, jobject thiz)
{
Jstring ret;
// So path:/data/package name of my program/lib/My so file name
Void * filehandle = dlopen ("/data/com. ckl. SoCallSo/lib/libb. so", RTLD_LAZY );
If (filehandle)
{
Char * (* funcPtrB) (void) = NULL;
FuncPtrB = dlsym (filehandle, "GetStringB ");
If (funcPtrB)
{
Ret = (* env)-> NewStringUTF (env, funcPtrB ());
}
Else
{
Ret = (* env)-> NewStringUTF (env, "dlsym GetStringB failed! ");
}
Dlclose (filehandle );
}
Else
{
Ret = (* env)-> NewStringUTF (env, "dlopen failed! ");
}
Return ret;
}
B .C
View plain
# Include <stdio. h>
# Include <stdlib. h>
# Include <stdarg. h>
# Include <dlfcn. h>
# Include <jni. h>
Char * GetStringB (void)
{
Return "I am in B. so ";
}
Jstring Java_com_ckl_SoCallSo_SoCallSoActivity_fucntionInB (JNIEnv * env, jobject thiz)
{
Return (* env)-> NewStringUTF (env, GetStringB ());
}
Jstring Java_com_ckl_SoCallSo_SoCallSoActivity_BcallA (JNIEnv * env, jobject thiz)
{
Jstring ret;
// So path:/data/package name of my program/lib/My so file name
Void * filehandle = dlopen ("/data/com. ckl. SoCallSo/lib/liba. so", RTLD_LAZY );
If (filehandle)
{
Char * (* funcPtrA) (void) = NULL;
FuncPtrA = dlsym (filehandle, "GetStringA ");
If (funcPtrA)
{
Ret = (* env)-> NewStringUTF (env, funcPtrA ());
}
Else
{
Ret = (* env)-> NewStringUTF (env, "dlsym GetStringA failed! ");
}
Dlclose (filehandle );
}
Else
{
Ret = (* env)-> NewStringUTF (env, "dlopen failed! ");
}
Return ret;
}
Android. mk
View plain
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE: =
LOCAL_SRC_FILES: = a. c
Include $ (BUILD_SHARED_LIBRARY)
Include $ (CLEAR_VARS)
LOCAL_MODULE: = B
LOCAL_SRC_FILES: = B. c
Include $ (BUILD_SHARED_LIBRARY)
A. c. B. c generate liba. so, libb. so, liba. so call libb. the GetStringB () function in so, libb. so call liba. the GetStringA () function in so.
View plain
In addition, the path of the so file is/data/package name of my program/lib/My so file name.
Project source code SoCallSo.7z
The running effect is as follows:
Author: victoryckl Column"