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. Qt + Android + Opencv linking libs
Forum Update on Monday, May 27th 2025

Qt + Android + Opencv linking libs

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 885 Views
  • 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.
  • D Offline
    D Offline
    Deymos
    wrote on 14 Dec 2021, 07:10 last edited by
    #1

    Hello, I’m trying to run opencv on qt with android, initially I took static libraries from the kit, but there were problems with them in the absence of AmediaExtractor and so on. so I compiled dynamic libraries from sources, disabling fat_java, they were successfully assembled in qt, but when the application starts, it crashes with an exception:'

    Java.lang.UnsatisfiedLinkError: dlopen failed: library “libopencv_core.so” not found: needed by /data/app/~~R8s5DfUqJCgALlqrx8DPig==/com.Demo.DemoApp-MYIKl0rddoPtWYMlO2my9g==/lib/arm/libandroidApp_armeabi-v7a.so in namespace classloader-namespace

    as I understand it, the problem is that I did not add libraries to the android folder and did not link them in gradle, but I do not really understand how to do this, I will be grateful for the help, because all the guides show that you need to connect libraries from android studio, but I work in qt and do not have this opportunity.
    I thought qt would cope with the task of linking android and qt on its own, but for some reason it did not work.

    Includies in PRO file
    OPENCV_ANDROID = $${PWD}/OpenCVFiles/opencv_build/
    INCLUDEPATH += $$OPENCV_ANDROID/
    android {

    LIBS +=
    -L$$OPENCV_ANDROID/lib/armeabi-v7a/
    -L$$OPENCV_ANDROID/3rdparty/lib/armeabi-v7a/
    -L$$OPENCV_ANDROID/jni/armeabi-v7a/
    -ltegra_hal
    -lcpufeatures
    -llibjpeg-turbo
    -llibtiff
    -llibwebp
    -llibopenjp2
    -llibpng
    -lIlmImf
    -llibprotobuf
    -lquirc
    -littnotify
    -lade
    -lopencv_core
    -lopencv_flann
    -lopencv_imgproc
    -lopencv_ml
    -lopencv_photo
    -lopencv_dnn
    -lopencv_features2d
    -lopencv_imgcodecs
    -lopencv_videoio
    -lopencv_calib3d
    -lopencv_highgui
    -lopencv_objdetect
    -lopencv_stitching
    -lopencv_video
    -lopencv_gapi
    -lopencv_java4

    ANDROID_PACKAGE_SOURCE_DIR=$${PWD}/android

    }

    gradle file
    buildscript {
    repositories {
    google()
    jcenter()
    }

    dependencies {
    classpath 'com.android.tools.build:gradle:3.6.0'
    }

    }

    repositories {
    google()
    jcenter()
    }

    apply plugin: ‘com.android.application’

    dependencies {
    implementation fileTree(dir: ‘libs’, include: [’.jar’, '.aar’])
    implementation ‘com.google.android.gms:play-services-ads:19.+’
    implementation ‘com.google.android.gms:play-services-auth:18.+’
    implementation ‘com.google.http-client:google-http-client-gson:1.26.0’
    implementation(‘com.google.api-client:google-api-client-android:1.26.0’) {
    exclude group: ‘org.apache.httpcomponents’
    }
    implementation (‘com.google.apis:google-api-services-drive:v3-rev173-1.25.0’) {
    exclude group: ‘org.apache.httpcomponents’
    }
    implementation ‘com.google.android.ump:user-messaging-platform:1.0.0’
    implementation ‘androidx.annotation:annotation:1.1.0’
    implementation ‘com.google.guava:guava:28.2-android’
    }

    android {
    /*******************************************************

    • The following variables:
      • androidBuildToolsVersion,
      • androidCompileSdkVersion
      • qt5AndroidDir - holds the path to qt android files
    • needed to build any Qt application
    • on Android.
    • are defined in gradle.properties file. This file is
    • updated by QtCreator and androiddeployqt tools.
    • Changing them manually might break the compilation!
      *******************************************************/
      compileSdkVersion 28
      //buildToolsVersion “x.y.z” // not needed since com.android.tools.build:gradle:3.0.0

    defaultConfig {
    minSdkVersion 26
    targetSdkVersion 28

    externalNativeBuild {
        cmake {
            arguments "-DANDROID_STL=c++_shared"
            targets "opencv_jni_shared"
        }
    }
    

    }

    buildToolsVersion '28.0.3'

    sourceSets {
    main {
    manifest.srcFile 'AndroidManifest.xml'
    java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java']
    aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl']
    res.srcDirs = [qt5AndroidDir + '/res', 'res']
    resources.srcDirs = ['resources']
    renderscript.srcDirs = ['src']
    assets.srcDirs = ['assets']
    jniLibs.srcDirs = ['libs']
    }
    }

    lintOptions {
    abortOnError false
    }
    // Do not compress Qt binary resources file
    aaptOptions {
    noCompress 'rcc'
    }
    defaultConfig {
    resConfig "en"
    minSdkVersion = qtMinSdkVersion
    targetSdkVersion = qtTargetSdkVersion
    }

    packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/ASL2.0'
    }
    }

    https://pastebin.com/B5PbS3Nx
    That’s how I build opencv with cmake, maybe the problem is this? I’m turn off FAT_JAVA_LIB and made libs shared.

    D 1 Reply Last reply 14 Dec 2021, 08:51
    0
    • R Offline
      R Offline
      Ramon_S
      wrote on 16 Dec 2021, 08:29 last edited by
      #3

      I don't think you need to copy them to libs folder but rather into src/main/jniLibs/ABI. See https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs for more details.

      1 Reply Last reply
      0
      • D Deymos
        14 Dec 2021, 07:10

        Hello, I’m trying to run opencv on qt with android, initially I took static libraries from the kit, but there were problems with them in the absence of AmediaExtractor and so on. so I compiled dynamic libraries from sources, disabling fat_java, they were successfully assembled in qt, but when the application starts, it crashes with an exception:'

        Java.lang.UnsatisfiedLinkError: dlopen failed: library “libopencv_core.so” not found: needed by /data/app/~~R8s5DfUqJCgALlqrx8DPig==/com.Demo.DemoApp-MYIKl0rddoPtWYMlO2my9g==/lib/arm/libandroidApp_armeabi-v7a.so in namespace classloader-namespace

        as I understand it, the problem is that I did not add libraries to the android folder and did not link them in gradle, but I do not really understand how to do this, I will be grateful for the help, because all the guides show that you need to connect libraries from android studio, but I work in qt and do not have this opportunity.
        I thought qt would cope with the task of linking android and qt on its own, but for some reason it did not work.

        Includies in PRO file
        OPENCV_ANDROID = $${PWD}/OpenCVFiles/opencv_build/
        INCLUDEPATH += $$OPENCV_ANDROID/
        android {

        LIBS +=
        -L$$OPENCV_ANDROID/lib/armeabi-v7a/
        -L$$OPENCV_ANDROID/3rdparty/lib/armeabi-v7a/
        -L$$OPENCV_ANDROID/jni/armeabi-v7a/
        -ltegra_hal
        -lcpufeatures
        -llibjpeg-turbo
        -llibtiff
        -llibwebp
        -llibopenjp2
        -llibpng
        -lIlmImf
        -llibprotobuf
        -lquirc
        -littnotify
        -lade
        -lopencv_core
        -lopencv_flann
        -lopencv_imgproc
        -lopencv_ml
        -lopencv_photo
        -lopencv_dnn
        -lopencv_features2d
        -lopencv_imgcodecs
        -lopencv_videoio
        -lopencv_calib3d
        -lopencv_highgui
        -lopencv_objdetect
        -lopencv_stitching
        -lopencv_video
        -lopencv_gapi
        -lopencv_java4

        ANDROID_PACKAGE_SOURCE_DIR=$${PWD}/android

        }

        gradle file
        buildscript {
        repositories {
        google()
        jcenter()
        }

        dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0'
        }

        }

        repositories {
        google()
        jcenter()
        }

        apply plugin: ‘com.android.application’

        dependencies {
        implementation fileTree(dir: ‘libs’, include: [’.jar’, '.aar’])
        implementation ‘com.google.android.gms:play-services-ads:19.+’
        implementation ‘com.google.android.gms:play-services-auth:18.+’
        implementation ‘com.google.http-client:google-http-client-gson:1.26.0’
        implementation(‘com.google.api-client:google-api-client-android:1.26.0’) {
        exclude group: ‘org.apache.httpcomponents’
        }
        implementation (‘com.google.apis:google-api-services-drive:v3-rev173-1.25.0’) {
        exclude group: ‘org.apache.httpcomponents’
        }
        implementation ‘com.google.android.ump:user-messaging-platform:1.0.0’
        implementation ‘androidx.annotation:annotation:1.1.0’
        implementation ‘com.google.guava:guava:28.2-android’
        }

        android {
        /*******************************************************

        • The following variables:
          • androidBuildToolsVersion,
          • androidCompileSdkVersion
          • qt5AndroidDir - holds the path to qt android files
        • needed to build any Qt application
        • on Android.
        • are defined in gradle.properties file. This file is
        • updated by QtCreator and androiddeployqt tools.
        • Changing them manually might break the compilation!
          *******************************************************/
          compileSdkVersion 28
          //buildToolsVersion “x.y.z” // not needed since com.android.tools.build:gradle:3.0.0

        defaultConfig {
        minSdkVersion 26
        targetSdkVersion 28

        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared"
                targets "opencv_jni_shared"
            }
        }
        

        }

        buildToolsVersion '28.0.3'

        sourceSets {
        main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java']
        aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl']
        res.srcDirs = [qt5AndroidDir + '/res', 'res']
        resources.srcDirs = ['resources']
        renderscript.srcDirs = ['src']
        assets.srcDirs = ['assets']
        jniLibs.srcDirs = ['libs']
        }
        }

        lintOptions {
        abortOnError false
        }
        // Do not compress Qt binary resources file
        aaptOptions {
        noCompress 'rcc'
        }
        defaultConfig {
        resConfig "en"
        minSdkVersion = qtMinSdkVersion
        targetSdkVersion = qtTargetSdkVersion
        }

        packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        }
        }

        https://pastebin.com/B5PbS3Nx
        That’s how I build opencv with cmake, maybe the problem is this? I’m turn off FAT_JAVA_LIB and made libs shared.

        D Offline
        D Offline
        Deymos
        wrote on 14 Dec 2021, 08:51 last edited by
        #2

        I understood what was the matter, going inside the apk file, I found that there are no opencv libraries, there are only standard qt libraries, does anyone have any ideas on how to connect the library to android from qt? The fact is that opencv libraries are copied to the qtbuild / android-build / libs folder, from there they are not added to the apk, and the qt libraries are in the qtbuild / android-build / libs / armeabi-v7a folder, and from there they go to the apk

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Ramon_S
          wrote on 16 Dec 2021, 08:29 last edited by
          #3

          I don't think you need to copy them to libs folder but rather into src/main/jniLibs/ABI. See https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs for more details.

          1 Reply Last reply
          0

          3/3

          16 Dec 2021, 08:29

          • Login

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