Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Working with multiple java activities on Android



  • Hello,

    I'm trying to integrate QtAdMob into a project of mine. QtAdMob comes with a custom java activity to retrieve ads from the Google AdMob backend. This custom activity is set as default activity in the AndroidManifest.xml and then retrieved, for example, with the following code:

        QPlatformNativeInterface* interface = QGuiApplication::platformNativeInterface();
        jobject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");
        if (activity)
        {
            m_Activity = new QAndroidJniObject(activity);
        }
    
        m_Activity->callMethod<void>("InitializeAdBanner");
    

    My issue is as follows: I already have a custom java activity for other things as my default activity. From what limited documentation there is around this,nativeResourceForIntegration("QActivity") seems to return QtAndroid::androidActivity()which is documented as "Returns a handle to this application's main Activity". Which in turn, naturally, returns my own custom activity. Where I obviously don't find QtAdMobs methods.

    Now, I believe I can add additional activities in my AndroidManifest.xml and I did so as follows:

    <!-- QtAdMob integration requirements-->
            <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" android:screenOrientation="unspecified" android:label="-- %%INSERT_APP_NAME%% --">
                <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
            </activity>
            <activity android:name="org.dreamdev.QtAdMob.QtAdMobActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" android:screenOrientation="unspecified" android:label="-- %%INSERT_APP_NAME%% --">
                <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
                <!-- this is the admob application id and needs to be set to the correct value -->
                <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx"/>
                <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
            </activity>
    

    However, I don't seem to be able to access this activity afterwards - or at least I'm doing something wrong. I ensured the activity is part of the package and copied accordingly in my .pro file through adding it in my DISTFILES and I can see the .java file in the build directly. However I fail at accessing it. I tried, for instance, accessing it through QAndroidJniObject as follows:

        QAndroidJniObject activity("org/dreamdev/QtAdMob/QtAdMobActivity");
        QAndroidJniEnvironment env;
        jclass adMobClass = env->GetObjectClass(activity.object<jobject>());
    

    or simply as follows:

            QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/dreamdev/QtAdMob/QtAdMobActivity", "activity", "()Landroid/app/Activity");
        
    

    but whenever I try to access activity.callMethod<void>("InitializeAdBanner"); I crash with JNI DETECTED ERROR IN APPLICATION: GetMethodID received NULL jclass. Apparently I'm doing something wrong.

    I'd appreciate if someone could point me into the right direction about this.



  • I admit, I'm somewhat surprised that there doesn't seem to be an answer to this. Is my approach wrong? Is it something weird to do? I worked around this by stuffing all the additional methods into my initial activity, but that, over a long course of action, doesn't seem to be a good idea. I'd much rather keep different functionality in separate files if at all possible.

    Am I overlooking something obvious? Am I just being dumb about things here?



  • Is this like the wrong place to ask these kinds of questions or is just barely anyone using Qt for mobile development (anymore)?


  • Lifetime Qt Champion

    Hi,

    Or you have a use case that nobody has encountered yet.

    In any case, you can also ask on the android-development mailing list.



  • Obviously, programmers who use Qt try to avoid Java and other platform specifics or other-language things as much as possible.

    When they need to use Java once every couple of years, they do it by copying a short function from somewhere and then try to forget what they needed to do to make it work.


Log in to reply