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"); 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
-
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?
-
@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