Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. load shared library in android with dlopen
Forum Updated to NodeBB v4.3 + New Features

load shared library in android with dlopen

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmjvox
    wrote on 3 Nov 2020, 16:20 last edited by mmjvox 11 Apr 2020, 20:40
    #1

    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");
        qDebug()<<ddd.exists();
    
        void *libuntitled1 = dlopen("/data/user/0/org.qtproject.example.Mobiledinamicload/files/libuntitled1armv7a.so", RTLD_LAZY);
            if (!libuntitled1) {
                qDebug()<<"szzzzzzzzzzz";
            }
    

    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
    Java_aaa_bbb_testnativecppqt_MainActivity_stringFromJNI(
            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);
            qDebug()<<ddd.exists();
        QLibrary myLib(fileName);
        qDebug()<<myLib.isLibrary(fileName);
        qDebug()<<myLib.load();
        qDebug()<<myLib.errorString();
    

    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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Nov 2020, 07:54 last edited by
      #2

      You are showing 3 different libraries:

      • a.so
      • libuntitled1.so
      • libdynamic1enginearmeabi-v7a.so
        You say libuntitled1.so works in Android Studio. Did you try to load that lib with QLibrary? Did you compare the access rights for all these 3 libraries?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 4 Nov 2020, 20:38
      0
      • J jsulm
        4 Nov 2020, 07:54

        You are showing 3 different libraries:

        • a.so
        • libuntitled1.so
        • libdynamic1enginearmeabi-v7a.so
          You say libuntitled1.so works in Android Studio. Did you try to load that lib with QLibrary? Did you compare the access rights for all these 3 libraries?
        M Offline
        M Offline
        mmjvox
        wrote on 4 Nov 2020, 20:38 last edited by
        #3

        @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.

        J 1 Reply Last reply 24 Apr 2021, 19:13
        0
        • M mmjvox
          4 Nov 2020, 20:38

          @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.

          J Offline
          J Offline
          juanba
          wrote on 24 Apr 2021, 19:13 last edited by
          #4

          @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

          IHTH

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved