Qt 5.2 QWidget Android AdMob
-
Hi!
I have working .apk file. Apk is made in Qt 5.2 QWidget.....But I can't include AdMob... ..this possible????? How? Please HELP!!!!! -
It is possible, but not an easy task. You have to know Qt, c++, java, jni, some knowledge of android apps development to make it work.I don't know Qt would provide us this function or not(crucial to monetize the apps), but Bog Dan(Father of Qt on android) said he would show us how to do that in the future.
"Bog dan blog":http://www.kdab.com/future-qt-android-looks-bright/
-
Hi,
you may also have a look at the "V-Play Admob plugin":http://v-play.net/doc/plugins1-admob/. It is currently based on Qt4, but you can signup for the upcoming Qt5 version here: http://plugins.v-play.netCheers,
Chris -
If you still need help with this, read this
http://xanm.blogspot.ru/2014/03/admob-in-qt.html -
Sorry to revive a year-old topic, but I succeeded in placing an AdMob banner on the bottom of the screen without touching JNI, just using plain Java. Let me show you what I did.
Tools: Qt 5.5.0, QtCreator 3.4.2, Android Studio 1.3 (not required but highly recommended to work with Android project and Java files). Of course you are going to need the Armv5 and Armv7 kits to compile for Android.
1.0 Create a QtQuick Application
2.0 Projects->select your kit, for example "Android for armeabi-v7a (GCC 4.9, Qt 5.5.0).
2.1 Under "Build Android Apk" select details. Make sure you tick "Use Gradle" and "Bundle QtLibraries with APK".
2.2 Click "Create templates", make sure "Copy the Gradle files to Android directory" is selected".
At this point your project is ready. We are going to use Gradle to build our project, but unfortunately the build.gradle file shipped with Qt is quite outdated.
3.0 Let's fix the build.gradle file in your project dir:
3.1 Line 3: replacejcenter()
withmavenCentral()
, this is needed to get the latest Android Plugin
3.2 Line 7: replaceclasspath 'com.android.tools.build:gradle:<oldversion>'
withclasspath 'com.android.tools.build:gradle:1.2.3'
, this is needed to use a newer Android Gradle plugin
3.3 Line 20 (or about there...): in yourdependencies
block add the linecompile 'com.google.android.gms:play-services:7.5.0'
. This will include the Google Play Service jars (including AdMob) into your project
3.4 Launch your project from QtCreator, everything should be fine and you should be able to run your app on your device.
4.0 Ok, now we can fire up Android Studio. In Android Studio click File->Open and go look for your build.gradle file. Let Android Studio crunch a few minutes while it downloads all dependencies and stuff to satisfy Gradle.
The whole point of all this job is to subclass QtActivity to add our AdMod widget (which in Android gergo is called "View").
4.1 Android Studio project pane: right click on the "java" directory (blue icon) and select New->Package. Insert your package name, like "com.mycompany.myproject" (I chose "com.illogica.qt"). When asked where on the filesystem to create the package directories, specify the directory of your project, NOT the Qt installation directory.
4.2 So, now you have created your package like all good Java programmers do. In the Project pane, right click on the package and choose New Class. Insert your class name, for example "MyActivity".
4.3 Here comes the best part. I don't really want to explain all of it, here's the code with some good comments:package com.illogica.qt; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.RelativeLayout; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; import org.qtproject.qt5.android.bindings.QtActivity; public class MyActivity extends QtActivity { private static ViewGroup viewGroup; private AdView mAdView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Init the AdView mAdView = new AdView(this); mAdView.setAdUnitId("YOUR APP UNIT ID HERE"); mAdView.setAdSize(AdSize.BANNER); //Get the root window of the our app View view = getWindow().getDecorView().getRootView(); if (view instanceof ViewGroup) { viewGroup = (ViewGroup) view; //cast window to a ViewGroup, so we can add child views //Add a RelativeLayout overlay, make it fit the whole window final RelativeLayout relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); //Fit the adView to the bottom of our screen-wide RelativeLayout RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); lp.addRule(RelativeLayout.CENTER_HORIZONTAL); mAdView.setLayoutParams(lp); relativeLayout.setLayoutParams(rlp); relativeLayout.addView(mAdView); //Add the adView to the RelativeLayout //When ads are loaded into the adView, show the adView mAdView.setAdListener(new AdListener() { public void onAdLoaded() { viewGroup.addView(relativeLayout); } }); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("ADD THE DEVICE ID") .addTestDevice("OF YOUR TEST DEVICES") .addTestDevice("IN 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(); } }
Using the Relative Layout you can anchor the adView wherever you want on the app window, in my case I chose the bottom.
Caveats: the ad banner is going to eat some pixels, you have to take that into account when writing your Qml UI.
5.0 Finishing touch: add modify the AndroidManifest.xml like this:
5.1 In the activity block change the name of the default class to be launched, in my case is
<activity [...] android:name="com.illogica.qt.MyActivity" [...]
5.2 Add a few permissions write before the manifest end:[...] <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest>
5.3 Add the AdMob specification in the application section:
[...] <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application>
6.0 That should be it, you can now focus on your Qml stuff, which I honestly don't know yet, I'm an Android guy and just wanted to see how to run Qml on Android.
6.1 Remember to run your app from QtCreator, NOT from Android Studio.
6.2 Profit! -
This post is deleted!