Back to Home

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

admob · qt · android development · qt5.2

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

  • Tutorial
Hello colleagues.

Qt 5 is new enough so that there is not something very necessary. It also turned out that there is no plugin for AdMob and other monetization services.
After you have developed or ported your Qt application to android, monetization can often be a question. Briefly mentioned the solution to this problem in my previous post. It describes in more detail how to integrate AdMob into a Qt application for Android.


Finding a solution



V-play admob plugin

V-play is a paid framework with a plugin for embedding various services in Qt applications, including advertising ones. For someone, this will be a perfectly acceptable solution to the issue of monetization.

qadmob

Many links on the network lead to this plugin, but it is already out of date.

What helped solve the problem


Helped: documentation for the developer for Android, the source code of Qt and Necessities, as well as a few become, the "father" of Qt for Android, Bogdan Vatra.

Instruction manual


Google play service

First you need to add the Google Play Service (at the moment this is where the AdMob java api is located) for co-assembly with your Qt project.

Step 1

Add the project.properties file to the folder where the android Qt files of the project are stored. We write 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 an absolute path there will be errors during assembly. And naturally, this path should be relative to the build directory of the project, and not the directory with the source code.

Step 2

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


Step 3

Next, edit AndroidManifest.xml

add rights to download ads:

add the version of Google Play Service to the application section:

add activity to the application section:


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 need to add AdView in it. Here's how it might look in code:
A 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 a Qt project, this will be QtLayout, which is inherited from 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 you do this in the onCreate function, the advertising blog will not be displayed until the screen orientation changes, or until the application is minimized and deployed. This seems to be a problem in the Qt java classes QtLayout.java and / or QtSurface.java.
       mAdView.setAdListener( new AdListener() {  
         public void onAdLoaded(){  
           if( adAdded)  
             return;  
           adAdded = true;  
           viewGroup.addView( mAdView);  
         }  
       }); 


Here is the result:
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 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 cope with this task.

Read Next