Android ndk solves the problem of mutual calls between the two so

Source: Internet
Author: User

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"
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.