Implement Google Analytics for Android in Qt



  • I'm trying to implement Google Analytics for Android into the native Android part of a Qt project. I'm following these instructions for Google Analytics v4.

    The instructions talk about editing the project-level and the app-level build.gradle files. My first problem is here - there is only an app-level build.gradle that I can edit in Qt. I tried anyway and put all the lines into that build.gradle file (I have to admit I had little idea of what I was doing then).

    I also generated the necessary configuration file (google-services.json), and put it into the root folder of the exported Android files.

    I also set the gradle version in gradle-wrapper.properties to gradle-2.10.

    Now when I compile, I get this error:

    Dex: Error converting bytecode to dex:
    Cause: com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
        UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
    (...)
    

    The app used to work properly so far. I'm using Qt 5.6.
    Have you had any experiences with implementing Google Analytics v4 or this issue?



  • Managed to solve this.
    The problem was that android-support-v4.jar was in the libs folder, and it was also listed as a dependency in the build.gradle, thus creating a conflict.
    Deleting the .jar file solved this error.

    Google Analytics is working now. My build.gradle looks like this:

    buildscript {
        repositories {
            jcenter()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0'
            classpath 'com.google.gms:google-services:2.0.0-beta6'
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    
    apply plugin: 'com.android.application'
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.2.0'
        compile 'com.android.support:support-v4:23.2.0'
    
        compile 'com.google.android.gms:play-services-analytics:8.4.0'
    }
    
    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 androidCompileSdkVersion.toInteger()
    
        buildToolsVersion androidBuildToolsVersion
    
        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 = ['src']
                renderscript.srcDirs = ['src']
                assets.srcDirs = ['assets']
                jniLibs.srcDirs = ['libs']
           }
        }
    
        lintOptions {
            abortOnError false
        }
    }
    
    apply plugin: 'com.google.gms.google-services'
    

Log in to reply
 

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