How to include C++ static libraries into the android apk of a QtApp, when building with Gradle



  • I am using a QtApp on android. I have a few C++ static libraries linking to my app each having a .pro file in them. I need to use gradle to build my app. The app builds fine using my build.gradle & creates an apk. But when I launch the app on device, it crashes !! Mostly, the reason is that my static libs are not getting added to the apk.

    Following is my build.gradle file

    apply plugin: 'com.android.application'
    apply plugin: 'maven'

    buildscript {
    repositories {
    maven {
    url "http://repo1.maven.org/maven2"
    }
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:2.1.3'
    }
    }

    android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    

    }
    The section jniLibs.srcDirs = ['libs'] nicely picks up the Qt related .so libs but it is missing my static libs as I see in the build log.

    So my question is : How can add my static libs to build.gradle so that gradle includes my static libs into the built apk?


  • Moderators

    @Nelson_Piquet
    a static lib is already included into your binary at link time, otherwise you would already have an error there.
    So you should debug your crash using the debugger.



  • @raven-worx Good point. I will be glad if I don't have to do anything special for the static libs. I am looking through the logcat of my android device to analyze.

    Btw, following is the apk creation step in my build-script with --gradle, Is there anything wrong you see ?

    androiddeployqt --sign mykeystore myKeyalias --storepass myPassword --gradle clean build --output android --verbose --input android-libMyQtApp.so-deployment-settings.json

    Can I add something more to correct my build ? Is there anything wrong you see ?



  • @raven-worx My app is running all fine when built with ant. it started to crash only after building with gradle


  • Moderators

    @Nelson_Piquet
    check the catlog. Ther should be more info what exactly caused the crash and where.



  • @raven-worx Following is the crash log I am getting:

    handle_notify_event, send msg [submit:trigger=0,bugtype=2,modulename=com.mycompany.myapp,level=1,testtype=NORMAL,path=/data/log/unzip/GRA-L09_GRA-L09C432B321a_0000000000_20161014165530_crash,mode=1;]
    10-14 16:55:30.825 3199 3436 I logserver: send_to_client, send to (9) res = 188
    10-14 16:55:30.844 3443 3462 W System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.huawei.lcagent.client.LogCollectManager.getUserType()' on a null object reference
    10-14 16:55:30.844 3443 3462 W System.err: at com.android.server.util.ReportTools.getUserType(ReportTools.java:86)
    10-14 16:55:30.844 3443 3462 W System.err: at com.android.server.util.ReportTools.isBetaUser(ReportTools.java:73)
    10-14 16:55:30.844 3443 3462 W System.err: at com.android.server.util.ReportTools.report(ReportTools.java:58)
    10-14 16:55:30.844 3443 3462 W System.err: at com.android.server.util.HwUserBehaviourRecord.appExitRecord(HwUserBehaviourRecord.java:65)
    10-14 16:55:30.844 3443 3462 W System.err: at com.android.server.am.ActivityManagerService$UiHandler.handleMessage(ActivityManagerService.java:1521)
    10-14 16:55:30.844 3443 3462 W System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
    10-14 16:55:30.844 3443 3462 W System.err: at android.os.Looper.loop(Looper.java:150)
    10-14 16:55:30.844 3443 3462 W System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
    10-14 16:55:30.844 3443 3462 W System.err: at com.android.server.ServiceThread.run(ServiceThread.java:46)
    10-14 16:55:30.844 3443 3462 E ReportTools: This is not beta user build



  • Checked the catlog in another android device with better logging. Following is the cause of the crash

    Caused by: java.lang.ClassNotFoundException: Didn't find class "org.qtproject.qt5.android.bindings.QtApplication" on path: DexPathList[[zip file "/data/app/com.mycompany.myapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.myapp-2/lib/arm, /data/app/com.mycompany.myapp-2/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]

    Not sure what I am doing wrong here


  • Moderators

    @Nelson_Piquet
    did you create the Android tempaltes (as described here) so that the android classes are also deployed?



  • I have the Androidmanifest file & the res folder inside MyAppfolder/android with ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android in MyApp.pro set. However, I have to move the manifest file 6 res folder inside MyAppfolder/android/src/main for gradle to build without issues.



  • by the way, I am doing this build command line using the following install step

    androiddeployqt --sign mykeystore.keystore myAlias --storepass myPassword --gradle clean build --output android --verbose --input android-libMyApp.so-deployment-settings.json


  • Moderators

    @Nelson_Piquet
    so you are not using QtCreator?



  • @raven-worx In my team all use QtCreator including me as well. But, we need to make a build-script for jenkins & Continious Integration.

    The build-script builds successfully when built with ant that comes by default with androiddeployqt We need the android part to build using gradle. Hence, I have appended this --gradle to androiddeployqt command & trying to build this with gradle.

    Again, the reason of trying to use gradle here is for versioning. If you google how to auto-increment version number of android app, then everybody suggests to do it with gradle. I dont know why ? Everybody suggest to move on to gradle in the android community specially. May be there are other reasons as well. At the moment I am just trying to build our QtApp using gradle


  • Moderators

    @Nelson_Piquet
    IIRC Qtcreator shows the build command in the console during the build



  • @raven-worx Yeah. I got that from the compile log of qtCreator. Thats really good about Qt. The command is correctly set now otherwise the build would not succeed. I think I am missing some dependency/plugin or something in build.gradle which is causing the following error & crashing the app on launch

    "Didn't find class "org.qtproject.qt5.android.bindings.QtApplication" on path: DexPathList[[zip file "/data/app/com.mycompany.myapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.myapp-2/lib/arm, /data/app/com.mycompany.myapp-2/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]"


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.