load shared library in android with dlopen

  • I'm trying to load a shared library on android with dlopen.

    here is my code:

    QFile ddd("/data/data/org.qtproject.example.Mobiledinamicload/files/libuntitled1armv7a.so");
        void *libuntitled1 = dlopen("/data/user/0/org.qtproject.example.Mobiledinamicload/files/libuntitled1armv7a.so", RTLD_LAZY);
            if (!libuntitled1) {

    but not works and writes this errors on console:

    D libMobile_dinamic_load_armeabi-v7a.so: true
    D libMobile_dinamic_load_armeabi-v7a.so: szzzzzzzzzzz
    I qtMainLoopThrea: type=1400 audit(0.0:37736): avc: denied { open } for path="/data/data/org.qtproject.example.Mobiledinamicload/files/libuntitled1armv7a.so" dev="mmcblk0p19" ino=245815 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:app_data_file:s0 tclass=file permissive=1

    it works on android studio and i can use the library :

    extern "C" JNIEXPORT jstring JNICALL
            JNIEnv* env,
            jobject /* this */) {
        std::string hello_str ="Hello from C++ shared library";
        void *libuntitled1 = dlopen("/data/data/aaa.bbb.testnativecppqt/files/libuntitled1armv7a.so", RTLD_LAZY);
        if (!libuntitled1) {
            hello_str ="cant load shared library";
            return env->NewStringUTF(hello_str.c_str());
    // call functions from library
        return env->NewStringUTF(hello_str.c_str());

    load with QLibrary not works too:

            auto path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
            QString fileName= path+"/libuntitled1armv7a.so";
            QFile ddd(fileName);
        QLibrary myLib(fileName);

    here is the error of QLibrary:

    D libMobile_dinamic_load_armeabi-v7a.so: true
    D libMobile_dinamic_load_armeabi-v7a.so: true
    I qtMainLoopThrea: type=1400 audit(0.0:38721): avc: denied { open } for path="/data/data/org.qtproject.example.Mobiledinamicload/files/libuntitled1armv7a.so" dev="mmcblk0p19" ino=245778 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:app_data_file:s0 tclass=file permissive=1
    D libMobile_dinamic_load_armeabi-v7a.so: false
    D libMobile_dinamic_load_armeabi-v7a.so: "Cannot load library /data/user/0/org.qtproject.example.Mobiledinamicload/files: (dlopen failed: library \"_data_user_0_org.qtproject.example.Mobiledinamicload_files\" not found)"

    --> my phone is rooted and i can copy any file to any path manually and change files permissions

  • Lifetime Qt Champion

    You are showing 3 different libraries:

  • @jsulm
    All these files are the same.
    I changed the name of the library during various tests and I wrote down the errors elsewhere.
    I forgot to correct them when writing this topic.
    Thanks for letting me know.
    I corrected them now.

  • @mmjvox The method i have followed is like below, it mimics the comments done here

    1.- Adding the custom libXYZ.so as an append on the apk resources directory structure. On the QtCreator this is done on the Additional libraries box placed at the details expander of the Build Android Apk tab.

    This step will deploy the shared object on the android-build/libs/YourArquitecture path. I guess that those contains are copied somehow by the android application installation process on the application directory structure

    2.- Use the absolute path /data/data/YourAplicationName/lib/libXZY.so on the dlopen c-call


Log in to reply