Multiple ad networks with one SDK on Android
Join the DZone community and get the full member experience.
Join For FreeAs an Android developer you know that including ads in your apps is one of the major ways of app monetization on the Play Store (when combined with in-app purchases, these methods account for more revenue than paid apps). But there's a myriad of ad networks out there making a developer's life harder (more SDKs, more coding, more tuning). On top of this what works for one region and ad network could be useless on other contexts and what works for a specific segment of your audience in terms of ad conversions can be a disaster for other segments.
You don't want to turn ad tweaking in your app into a full time job.
Here at Kii we work hard to make your life as an application developer easier. We want you to unleash the power of your app by focusing on making your app more awesome. We take care of the rest. That's why we created an easy to use and powerful ad network API in our Kii Ads SDK to handle all your advertising needs in your application.
In this blog post I'm going to show you how to display ads using Kii Ads SDK using Google's AdMob as a network example (this one of many ad networks that you can use in the Kii Ad Network). You'll also learn how to programmatically distribute ad serving among different ad networks and regions and how to target your audience in a granular way using filters.
Setting up Kii Ads
In order to give you the most flexibility we provide a backend service and a console that you can use as control panel to affect how your ads are displayed in your application. Using the Kii Ad network involves two steps: setting up your app on the developer console and adding the necessary code to display ads to your Android application.
In order to use the developer console to configure your app you first need to go to developer.kii.com and sign-up. Once you log in click on "Create App", check the Android box, click on "Create", then click on the Android icon, scroll down a little bit and download the Kii Ads SDK from the offered links (Ads SDK v2.1.2). You'll get a zip file with a jar file inside that you should include in a libs directory inside your Android Eclipse project and then, from Eclipse, you should "Add to Build path". Ok, we're now ready to work with Kii Ads in your Android app (check the next section for a code walkthrough).
Now it's time to set up your app on the console to work with different ad networks and regions of the world. For instance, an app developer might want to serve ads from SmartMAD for app users in China and use AdMob or Direct Ads (or a combination of those) for app users in the US instead.
There's a comprehensive configuration guide available here that you should follow. When you're done with the setup, you'll be able to define the weight (percentage of ads delivered thru a network) of ad serving for each network by configuring a slider:
This kind of granular control over your ads will give the ability to fine tune your eCPM, experiment with different region and networks and react quickly to changes in ad effectiveness without touching your application (just tweaking the console parameters).
Nice!
You'll also be able to target your audience selectively with comprehensive filters that you can use on your app when you set up Kii Ads (see below).
Configuring your Android project
Once you include the Ads SDK v2.1.2 (aka KiiAdNetSDK) to your build path as explained in the previous section you'll have to include the following permission your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
Note: If you want to use KiiAdNetSDK with proguard, please add the following settings to your proguard-project.txt or proguard.cfg:
-dontwarn com.kii.ad.adapters.* -keep class com.kii.ad.adapters.* { public *; }
Let's now add AdMob support to your app. Download GoogleAdMobAdsSdk-4.3.1.jar (the SDK only works with this version at this point) and put it in the project "libs" directory. Refresh the project, right click on the jar and do an "Add to Build Path" (Don't forget to add and configure your app on the AdMob console, otherwise ad serving won't be possible).
Add the following permission to AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Now change target version to Android 3.2 or above and add the following activity info to AndroidManifest.xml:
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
(For more info on how to configure other networks other than AdMob see this page)
Now let's deal with ad displaying. This is done by creating a com.kii.ad.KiiAdNetLayout. You must ensure that this code is called in the main thread. Here's an example set up that you can could from onCreate() of your activity:
/** * Initialize KiiAds. Please change APP_ID/APP_KEY to match your application */ public KiiAdNetLayout initKiiAds() { // get scaledDensity Display display = getWindowManager().getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); float scaledDensity = metrics.scaledDensity; // create an KiiAdnetLayout KiiAdNetLayout adLayout = new KiiAdNetLayout( this, Constants.APP_ID, Constants.APP_KEY, KiiAdNetLayout.SiteID.US); adLayout.setMaxWidth((int) (scaledDensity * 320)); adLayout.setMaxHeight((int) (scaledDensity * 52)); // set targeting parameters KiiAdNetTargeting.setKeywords("Travel"); KiiAdNetTargeting.setGender(KiiAdNetTargeting.Gender.MALE); KiiAdNetTargeting.setAge(35); KiiAdNetTargeting.setPostalCode("30900"); return adLayout; }
Note that we use the SDK to instantiate a KiiAdNetLayout with a specific size of 320 x 52 used the device scaled density. You can choose whatever size that suits your application for the layout but we suggest 320 (dip) x 52 (dip), as this is the largest ad unit size. If you choose to make the view smaller, the ads will still display but may cut off a few pixels and display a small scroll bar.
Check out the detailed ad targeting control in the code, you can set gender, age, region and more for your target audience.
If you're wondering how to include the ad layout in your activity this is pretty straight forward. Add a new relative layout element at the bottom of your activity layout (if you want the ads to display at the bottom):
<RelativeLayout android:id="@+id/layout_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <LinearLayout android:id="@+id/layout_ads" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"> </LinearLayout> </RelativeLayout>
Then, when your activity is created you can go ahead and add the ad layout to it:
// add Kii ad layout LinearLayout layout = (LinearLayout) getActivity().findViewById(R.id.layout_ads); KiiAdNetLayout adLayout = initKiiAds(); layout.addView(adLayout);
And voilà!
I hope you now feel more comfortable with working with the Kii Ads SDK in your Android application and understand some of the features a capabilities.
Stay tuned for our next Android post that will show how to do advanced, powerful analytics in your app with just a few lines of code.
For more information check out the Ads SDK Quickstart in our Android documentation. You can get up to speed with Kii Cloud following the Android Guide and the Android Quickstart pages.
Opinions expressed by DZone contributors are their own.
Comments