Hi!
I’m trying to do the same. Specifically, to add an interstitial ad (AdMob) to the BasicGame project generated by the jMonkeyEngine 3.0 wizard. But it’s a nightmare…
I followed the latest Google instructions at https://developers.google.com/mobile-ads-sdk/docs/?hl=en_US, so, after adding the ‘google-play-services_lib/libs/google-play-services.jar’ library to the project (Properties -> Libraires -> Compile tab) I modified the Android manifest file in jMonkeyEngine as per Google instructions:
[java]
<?xml version=“1.0” encoding=“UTF-8”?>
<manifest xmlns:android=“http://schemas.android.com/apk/res/android” android:versionCode=“1” android:versionName=“1.0” package=“com.mycompany.admobtest”>
<application android:label="@string/app_name">
<meta-data android:name=“com.google.android.gms.version” android:value="@integer/google_play_services_version"/>
<activity android:label="@string/app_name" android:launchMode=“singleTask” android:name=“MainActivity”>
<intent-filter>
<action android:name=“android.intent.action.MAIN”/>
<category android:name=“android.intent.category.LAUNCHER”/>
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
<uses-sdk android:minSdkVersion="9"/>
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/>
<uses-permission android:name=“android.permission.INTERNET”/>
<uses-permission android:name=“android.permission.ACCESS_NETWORK_STATE”/>
</manifest>
[/java]
Then I (roughly!) ported to MainActivity.java in jMonkeyEngine the example project provided by Google for the interstitial ads (found at https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/interstitial), as follows:
[java]
package com.mycompany.admobtest;
import android.content.pm.ActivityInfo;
import com.jme3.app.AndroidHarness;
import com.jme3.system.android.AndroidConfigChooser.ConfigType;
import java.util.logging.Level;
import java.util.logging.LogManager;
import com.google.android.gms.ads.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.R;
import android.widget.FrameLayout;
import android.view.ViewGroup.LayoutParams;
import android.view.Gravity;
import android.widget.ImageView;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.view.View;
public class MainActivity extends AndroidHarness
{
/*
- Note that you can ignore the errors displayed in this file,
- the android project will build regardless.
- Install the ‘Android’ plugin under Tools->Plugins->Available Plugins
- to get error checks and code completion for the Android project files.
*/
public MainActivity()
{
// Set the application class to run
appClass = “mygame.Main”;
// Try ConfigType.FASTEST; or ConfigType.LEGACY if you have problems
eglConfigType = ConfigType.BEST;
// Exit Dialog title & message
exitDialogTitle = “Exit?”;
exitDialogMessage = “Press Yes”;
// Enable verbose logging
eglConfigVerboseLogging = false;
// Choose screen orientation
screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
// Enable MouseEvents being generated from TouchEvents (default = true)
mouseEventsEnabled = true;
// Set the default logging level (default=Level.INFO, Level.ALL=All Debug Info)
LogManager.getLogManager().getLogger("").setLevel(Level.INFO);
}
private InterstitialAd interstitialAd;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //<== COMPILAITION ERROR
// Create the interstitial.
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("my-ad-unit-id-here");
// Set the AdListener.
interstitialAd.setAdListener(new AdListener()
{
@Override
public void onAdLoaded()
{
logger.log(Level.INFO, "ADTEST - Ad loaded successfully");
}
@Override
public void onAdFailedToLoad(int errorCode)
{
logger.log(Level.INFO, "ADTEST - Ad failed to load: " + getErrorReason(errorCode));
}
});
logger.log(Level.INFO, "ADTEST - InitAdView done");
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitialAd.loadAd(adRequest); //<== APP CRASHES HERE
}
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial()
{
if (interstitialAd.isLoaded())
{
interstitialAd.show();
}
}
/** Gets a string error reason from an error code. */
private String getErrorReason(int errorCode)
{
String errorReason = “”;
switch(errorCode)
{
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorReason = “Internal error”;
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorReason = “Invalid request”;
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorReason = “Network Error”;
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorReason = “No fill”;
break;
}
return errorReason;
}
}
[/java]
However all I get at the moment is the following compilation error: No resource found that matches the given name (at ‘value’ with value ‘@integer/google_play_services_version’).
If I place a ‘hard-coded’ version number in the manifest (replacing ‘@integer/google_play_services_version’ with ‘4030500’ for example), the next compilation error is at the ‘setContentView(R.layout.main);’ line in the MainActivity code above.
Finally, if I comment out the offending line, the project compiles and deploys to the real device (Nexus 7 tablet), but crashes as soon as it tries to load the ad (‘interstitialAd.loadAd(adRequest);’ line in the code above).
Pretty depressing… 