We connect AdMob advertising in Marmalade (extension for Android OS)

When developing cross-platform games and applications, most developers release the Android version of their game, paid or free (with ads), and often the full paid + lite version is free, again with ads.

When developing a game using the Marmalade (formerly Airplay SDK), there was a need to introduce advertising (AdMob is used as an example in this article), and since there was no sensible information, examples, or tutorials anywhere, I had to dig it myself. As a result, everything turned out to be not so difficult.

And so what we need:
  1. Marmalade SDK 5.0 and higher (the article used version 5.0)
  2. Android NDK r5 (with the tools installed in the requirements and the NDK_ROOT system variable)
  3. GoogleAdMobAdsSdk-4.1.0.jar

Create an extension


We go to the directory where Marmalade is installed, a folder with extensions (for example, c: \ Marmalade \ 5.0 \ extensions), and create our own folder with the name of the new AdmobAds extension . It is advisable that the extension be in a directory with other extensions.
Now you need to create a text document AdmobAds.s4e , which should contain the following:
#include 
functions:
s3eResult InitAds (const char * pub_id) S3E_RESULT_ERROR run_on_os_thread
s3eResult ShowAds () S3E_RESULT_ERROR run_on_os_thread
s3eResult HideAds () S3E_RESULT_ERROR run_on_os_thread

This means that the extension will have 3 methods:
  • InitAds () which will accept as input the publisher_id parameter
  • ShowAds () - show ads
  • HideAds () - hide ads


Now you need to generate the extension code for Android, for this, right-click on AdmobAds.s4e and select Build Android Extension . After that, the template source code for java and cpp, and other auxiliary files will be generated in the source folder.

image

After the code has been generated, open ... \ AdmobAds \ source \ android \ AdmobAds.java which in turn contains the method described in AdmobAds.s4e :
/ *
java implementation of the AdmobAds extension.
Add android-specific functionality here.
These functions are called via JNI from native code.
* /
/ *
 * NOTE: This file was originally written by the extension builder, but will not
 * be overwritten (unless --force is specified) and is intended to be modified.
 * /
import com.ideaworks3d.marmalade.LoaderAPI;
class AdmobAds
{
    public int InitAds (final String pub_id)
    {
        return 0;
    }
    public int ShowAds ()
    {
        return 0;
    }
    public int HideAds ()
    {
        return 0;
    }
}


Attention: the method is not executed in the UI thread, and to display the same Toast, you must add its launch through runOnUiThread:

import com.ideaworks3d.marmalade.LoaderActivity;    
import android.widget.Toast;
    ...    
    public int InitAds (final String pub_id)
    {
    	LoaderActivity.m_Activity.runOnUiThread (new Runnable () {
			@Override
			public void run () {
				Toast.makeText (LoaderActivity.m_Activity, pub_id, Toast.LENGTH_LONG) .show ();
			}
		});
        return 0;
    }

LoaderActivity.m_Activity - the main activity of the application, it can be inherited if necessary.

Now let's check how it works, double-click to launch
  1. AdmobAds_android.mkb
  2. AdmobAds_android_java.mkb

If everything was done correctly, then build failed should not occur.

In order to connect the extension to the project, you need to perform a couple of steps:
  • For example, copy HelloWorld example
  • Add sub-project (extension) to s3eHelloWorld.mkb

subprojects
{
	Admobads
}

  • Save and open (dependencies will be generated and extension to the project linked)
  • Add the following changes to s3eHelloWorld.cpp:

#include "AdmobAds.h"
int main ()
{
	if (AdmobAdsAvailable ()) {
		InitAds ("a14bd815 ...");
	}

The AdmobAdsAvailable () method is automatically generated when the extension is created, and returns true if the extension is available for this platform (i.e., in our case it will be true for Android and for iOS, win, etc. will return false).

After compiling the GCC ARM (Release), we start the Deployment tool, select the file deploy_config.py from the build directory, select it from the list of Android platforms, click Deply All. After that, we start the application export for the selected platforms. In case of a successful build, download the apk file to your phone (!) And check.

We saw Toast - everything is fine, now it remains to connect Admob advertising

Adding Ads to AdmobAds Extension


Open and edit the previously described source code for the AdmobAds.java extension and add the advertisement there:
import android.view.ViewGroup.LayoutParams;
import android.view.View;
import android.widget.Toast;
import com.google.ads.Ad;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.ideaworks3d.marmalade.LoaderAPI;
import com.ideaworks3d.marmalade.LoaderActivity;
class AdmobAds
{
    private static final int ADVIEW_NOT_INITIALIZED = 1;
    private AdView adView;
    public int InitAds (final String pub_id)
    {
    	LoaderActivity.m_Activity.runOnUiThread (new Runnable () {
			@Override
			public void run () {
				Toast.makeText (LoaderActivity.m_Activity, pub_id, Toast.LENGTH_LONG) .show ();
				adView = new AdView (LoaderActivity.m_Activity, AdSize.BANNER, pub_id);
				adView.loadAd (new AdRequest ());
				LoaderActivity.m_Activity.addContentView (adView, new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			}
		});
        return 0;
    }
    public int ShowAds ()
    {
	if (adView! = null) {
		LoaderActivity.m_Activity.runOnUiThread (new Runnable () {
			@Override
			public void run () {
				adView.setVisibility (View.VISIBLE);
			}});
	} else { 
		return ADVIEW_NOT_INITIALIZED; 
	}
        return 0;
    }
    public int HideAds ()
    {
	if (adView! = null) {
		LoaderActivity.m_Activity.runOnUiThread (new Runnable () {
			@Override
			public void run () {
				adView.setVisibility (View.INVISIBLE);
			}});
	} else { 
		return ADVIEW_NOT_INITIALIZED; 
	}
        return 0;
    }
}

As you can see, nothing extraordinary, the standard sample code implementation of AdMob'a wiki advertisement

Run
  • AdmobAds_android.mkb
  • We edit AdmobAds_android_java.mkb and add the path to GoogleAdMobAdsSdk-4.1.0.jar there

#! / usr / bin / env mkb
# Builder mkb file for the java portion of the AdmobAds extension on android
platform java
files
{
    (source / android)
    AdmobAds.java
}
librarypath "$ MARMALADE_ROOT / s3e / deploy / plugins / android / android.jar"
librarypath "$ MARMALADE_ROOT / s3e / loader / android / s3e_release.jar"
librarypath "c: /Marmalade/5.0/examples/HelloWorld_java/GoogleAdMobAdsSdk-4.1.0.jar"
option output-name = lib / android / AdmobAds.jar

  • We execute AdmobAds_android_java.mkb


Now you need to edit s3eHelloWorld.mkb and add there
deployments
{
	android-manifest = AndroidManifest.xml
	android-external-jars = GoogleAdMobAdsSdk-4.1.0.jar
}


AndroidManifest.xml can be copied to the generated one (next to the .apk file there is an intermediate_files directory) and added there:

...
    


Now we repeat the export procedure, download to the phone:


For some reason, it crashes on the android emulator, it seems that it is not always friendly with marmalade, but this example is tested and works on Nexus S, huawei u8110, huawei ideos u8150, Samsung i5500 phones

Information was taken from the following sources
Google AdMob Ads Android Fundamentals
EDK tutorial
Marmalade documentation

At the request of the guys from Marmalade I posted the sources on their git (HelloWorld attention has been changed a bit - added the ability to show / hide ads)
https://github.com/marmalade/admob

Also popular now: