简体   繁体   中英

How to use C++ dlopen macro to load a library from C++ code instead from JAVA

I'm trying to use some already written C++ code for server communication in my Android application (Basically creating a client), sadly, the logic of the code depends on this piece of code:

#ifndef LoadLib
#define LoadLib(a) (hmod)dlopen(a, RTLD_NOW)
#endif

The code gets called from:

m_hmod = LoadLib(m_namespace::LIBRARY);

where LIBRARY = "nameofmylib.so"

Problem is that when I call LoadLib, my m_var remains NULL, which means it can't find the library ( That's my guess so far ) and so my logic goes into error handling rather than connecting to the socket.

So far, I've been surfing StackOverflow and trying solutions people suggest with changing the Gradle file and adjusting CMakeList.txt but none work. I've been debugging it for few hours as well, and I even tried looking for the .so location inside the Android Studio Device File Explorer, so that I could put in the absolute path. But that also yielded no results.

The complete flow of loading goes like this:

#ifndef LoadLib
#define LoadLib(a) (hmod)dlopen(a, RTLD_NOW)
#endif


typedef void * hmod;
hmod m_hmod;

m_hmod = LoadLib(m_namespace::LIBRARY);

if (m_hmod) {
    ... Some code that needs to execute ...
} else {
    ... What actually happens ...
}

The code I am working with isn't something I wrote, but I have to stick to it, thus I need to use the logic that's inside it rather than rewriting it and making my own.

Most likely, m_namespace::LIBRARY is not being packed into the APK. To check this, you don't need to examine the device files. Android Studio provides a menu command to analyze your APK.

If the library is missing, you should ask yourself if the name follows Android rules: it must begin with lib and it must end with .so . No number suffixes, no extra characters, and God forbid, no spaces.

If the library name is OK, but it is used prebuilt by your CMake script, check that it is available for the ABI of your device. Also, make sure that its path is listed in jniLibs.src list in build.gradle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM