📜 ⬆️ ⬇️

AdMob, Qt 5.2 and Android or what happens when there is no answer on the Internet

Hello colleagues.

Qt 5 is new enough to not have something very necessary there. It also turned out that there is no plug-in for AdMob and other monetization services.
After you have developed or ported your application to Qt for android, the question of monetization can often arise. Briefly mentioned solving this problem in my previous post. Here is described in more detail how to embed AdMob in Qt application for Android.


Search for a ready solution


')
V-play AdMob plugin

V-play is a paid framework with a plugin for embedding various services in Qt applications, including adware. For some, it will be quite an acceptable solution to the issue of monetization.

qadmob

Many links in the network lead precisely to this plugin, but it is already outdated.

What helped solve the problem


Helped: developer documentation for Android, Qt and Necessities source code, as well as a few become, Qt's “father” for Android, Bogdan Vatra.

Instruction


Google play service

First you need to add the Google Play Service (at the moment it is there a AdMob java api) for joint assembly with your Qt project.

Step 1

Add the project.properties file to the folder where the android Qt project files are stored. We register a link to the Google Play Service library in it:
android.library.reference.1 =. / relative / path / to / google-play-services_lib
It is important to specify the relative path to the library directory, with the absolute path being errors during the build. And of course this path should be relative to the build directory of the project, and not the source code directory.

Step 2

This step may not be necessary, but if errors occur during assembly, try it.
In the library directory (/ path_to_android_sdk / extras / google / google_play_services / libproject / google-play-services_lib /), run the following commands:
android update lib-project --path . ant clean ant release 


Step 3

Next, edit the AndroidManifest.xml

add rights to download ads:
  <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Add the version of Google Play Service to the application section:
  <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> 

Add activity to the application section:
  <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:name="com.google.android.gms.ads.AdActivity"/> 


Add admob banner

After the Google Play Service is connected to the project, you should implement the main Activity, which should be inherited from QtActivity, you should add AdView in it. Here’s how it might look like in code:
Lot of code
  package org.qtproject.example.admobqt; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.AdListener; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; public class AdMobQtActivity extends org.qtproject.qt5.android.bindings.QtActivity { private static ViewGroup viewGroup; private AdView mAdView; private boolean adAdded = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAdView = new AdView(this); mAdView.setAdUnitId("YOUR_ADMOB_ID_HERE"); mAdView.setAdSize(AdSize.BANNER); View view = getWindow().getDecorView().getRootView(); if (view instanceof ViewGroup) { viewGroup = (ViewGroup) view; ViewGroup.LayoutParams ad_layout_params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 150); mAdView.setLayoutParams(ad_layout_params); mAdView.setAdListener( new AdListener() { public void onAdLoaded(){ if( adAdded) return; adAdded = true; viewGroup.addView( mAdView); } }); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE") .build(); mAdView.loadAd( adRequest); } } @Override public void onPause() { mAdView.pause(); super.onPause(); } @Override public void onResume() { super.onResume(); mAdView.resume(); } @Override public void onDestroy() { mAdView.destroy(); super.onDestroy(); } } 



Here we get the root View from our Activity.
  View view = getWindow().getDecorView().getRootView(); 


In the Qt project, this will be QtLayout, which is inherited from the ViewGroup, which allows us to include AdView in it.
Next you need to add AdView to the ViewGroup at the moment when the advertisement is loaded. If this is done in the onCreate function, the advertising blog will not be displayed until the screen orientation changes, or until the application is minimized and expanded. This seems to be a problem in the Qt java classes of QtLayout.java and / or QtSurface.java.
  mAdView.setAdListener( new AdListener() { public void onAdLoaded(){ if( adAdded) return; adAdded = true; viewGroup.addView( mAdView); } }); 


Here's what happened in the end:
github.com/AlexMarlo/AdMob-Qt5.2-Example

Useful links:
qt-project.org/doc/qt-5/qtandroidextras-notification-example.html
gitorious.org/qadmob
blog.qt.digia.com/blog/2013/12/12/implementing-in-app-purchase-on-android
developer.android.com/tools/projects/index.html
developer.android.com/tools/projects/projects-cmdline.html

PS:
The next step is to create a full-fledged cross-platform plugin for integrating AdMob and other monetization services in Qt. I'm not sure that I can do something like this, but I hope that this article will help someone to cope with this task.

Source: https://habr.com/ru/post/220947/


All Articles