Tutorial: Howto implement AdMob/MoPub/any kind of ad in Android App

671 views
Skip to first unread message

Jo nas

unread,
Nov 24, 2013, 11:27:17 AM11/24/13
to codenameone...@googlegroups.com
Hey Codenameone community,
since I implemented MoPub I'm earning ~20-50 times the money that I earned with vserv.mobi, although I'm only doing this in Android. You can see an example on how these banners/interstatials looks like in my app: Scientific Calculator cFunction.
Because the only 'built-in' ad network in cn1 is the very bad paying vserv.mobi network, here is a small bullet-point tutorial on how to implement advertising (banners/interstatials) for Android with MoPub mediating to AdMob/Millennial Media. In general all ad networks can be implemented somehow like this, but I think MoPub is the best solutions for its mediation options. So here is how it goes:
  • Get the Android sources of your codenameone project ('include source') (can only be done when you're a paying cn1 user)
  • Register with MoPub/AdMob/Millennial Media
  • Import the Android sources of your app in eclipse
  • Download the MoPub full SDK
  • Add the MoPub SDK to your project and add the manifest entries like described here
  • Add mopub/lib/*.jars to your libs directory in eclipse
  • Add in proguard.cfg:
    -libraryjars libs/GoogleAdMobAdsSdk-6.4.1.jar
    -libraryjars libs/MMSDK.jar
    -libraryjars libs/android-support-v4.jar
    -keep class com.nuance.** { *; }
    -keep class com.millennialmedia.** { *; }
    -keep class com.google.** { *; }
    -keepclassmembers class com.millennialmedia.android.* { 
    public *; 

    -keep class com.millennialmedia.android.**
    -dontwarn com.millennialmedia.android.NVASpeechKit*
Then for banner ads:
  • Go to /src/codename1/impl/android/AndroidImplementation.java and replace initSurface() by this:

    public static MoPubView moPubView;
/**
* init view. a lot of back and forth between this thread and the UI thread.
*/
private void initSurface() {
//layout for whole screen
RelativeLayout rl = new RelativeLayout(activity);
rl.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));


//add banner
moPubView = new MoPubView(getContext());
moPubView.setAdUnitId(<your mopub id>); // Enter your
// Ad Unit
// ID from
moPubView.loadAd();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // You might want to tweak these to WRAP_CONTENT
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
final int dps = 50;
final float scale = getContext().getResources().getDisplayMetrics().density;
final int pixels = (int) (dps * scale + 0.5f);
//add implementation 
relativeLayout = new RelativeLayout(activity);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        lp2.setMargins(0, 0, 0, pixels);
        relativeLayout.setLayoutParams(lp2);
relativeLayout.setFocusable(false);
myView = new AndroidView(activity, AndroidImplementation.this);
myView.setVisibility(View.VISIBLE);


relativeLayout.addView(myView);
myView.setVisibility(View.VISIBLE);

int id = activity.getResources().getIdentifier("main", "layout", activity.getApplicationInfo().packageName);
RelativeLayout root = (RelativeLayout) LayoutInflater.from(activity).inflate(id, null);

rl.addView(relativeLayout);
root.addView(rl);
root.addView(moPubView, lp);
activity.setContentView(root);
myView.requestFocus();
}


  • Go to /res/layout/main.xml in your project and replace it with this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff000000" >
</RelativeLayout>


For interstatials


Hope this helps someone!

Greetings,
Jonas

Shai Almog

unread,
Nov 24, 2013, 2:05:25 PM11/24/13
to codenameone...@googlegroups.com
Thanks Jonas, that's very interesting!

adamca...@gmail.com

unread,
Feb 17, 2015, 2:03:27 PM2/17/15
to codenameone...@googlegroups.com
Hi Jonas

Is MoPub still working for you?
Reply all
Reply to author
Forward
0 new messages