Integrate Third-Party Ad Networks
- Recommended: Use Our Mediation Integration Tool
- Integrate Adapters Manually via Source Code (GitHub)
- Set Up Ad Renderers for Native Ads
The MoPub SDK can mediate most ad networks through our pre-built adapters, and custom events. To see a current list of supported ad networks, and to get the latest certified versions of mediation adapters, visit the MoPub Mediation Integration Tool. The tool also provides a convenient integration approach, discussed below.
This guide walks you through the mediation setup process; specifically, where to download adapters and how to integrate them into your app project. You can integrate networks through our MoPub Mediation Integration Tool, or manually via source code.
Recommended: Use Our Mediation Integration Tool
We recommend that you integrate adapters from Bintray/JCenter using our MoPub Mediation Integration Tool. This is the more efficient option if you’re just getting started with mediation, and don’t need to tweak anything.
Note: The mediation adapter AARs include AndroidManifest dependencies for each ad Network.
Our tool provides a new and convenient integration approach. Simply select the ad networks you want to mediate, and generate the Gradle/Cocoapods configuration required for your app setup.
Integrate Adapters Manually via Source Code (GitHub)
You can still integrate adapters manually using the existing process if you prefer. We have migrated our mediation adapters to a new GitHub repository.
Download the mediation repository i.e, simply clone the entire repository and open it in Android studio.
You are now ready to build aars using adapter source files for which ever network you are trying to mediate with.
gradlewindow in Android Studio, select Network folder > Tasks > build > build
- The manually built aar using source files will be found under Project > Network > build > outputs > aar
Incorporate above built aar into your project.
This involves adding
libsdirectory if your gradle dependencies has a rule of the form
implementation fileTree(dir: 'libs', include: '*.aar')
Or you can use File > New > Module > Import jar/aar to create a module in your app for the aar. In the file dialog, navigate to the build/outputs/aar directory in the cloned repo and select the aar. Android Studio will copy it into your project. Then you can select the app module and (right click)>Open Module Settings, and add a Module Dependency on the aar module just created.
Incorporate third-party SDKs into your project. This typically involves adding that network’s SDK
.jarfile to a
libsdirectory, or adding its Gradle dependency (if available). This information can usually be found on the ad network’s support page.
Set Up Ad Renderers for Native Ads
If native ad is among the formats you are mediating, create and register the appropriate renderer for each ad network to ensure your native ad views are laid out as intended.
Begin with the default
MoPubStaticNativeAdRenderer, shown below.
// Some third party SDKs rely on the MoPub renderer as a default renderer. MoPubStaticNativeAdRenderer moPubStaticNativeAdRenderer = new MoPubStaticNativeAdRenderer( new ViewBinder.Builder(R.layout.native_ad_list_item) .titleId(R.id.native_title) .textId(R.id.native_text) .mainImageId(R.id.native_main_image) .iconImageId(R.id.native_icon_image) .callToActionId(R.id.native_cta) .privacyInformationIconImageId(R.id.native_privacy_information_icon_image) .build() );
- Similarly, create ad renderers for third-party ad networks.
final GooglePlayServicesAdRenderer googlePlayServicesAdRenderer = new GooglePlayServicesAdRenderer( new MediaViewBinder.Builder(R.layout.native_video_layout) .mediaLayoutId(R.id.native_media_layout) // bind to your `com.mopub.nativeads.MediaLayout` element .iconImageId(R.id.native_icon_image) .titleId(R.id.native_title) .textId(R.id.native_text) .callToActionId(R.id.native_cta) .privacyInformationIconImageId(R.id.native_privacy_information_icon_image) .build());
Flurry: Flurry uses its own
FlurryViewBinderthat wraps the regular
ViewBindercreated above. You may register the Flurry renderer like so:
FlurryViewBinder flurryViewBinder = new FlurryViewBinder(new FlurryViewBinder.Builder(viewBinder)); FlurryNativeAdRenderer flurryNativeAdRenderer = new FlurryNativeAdRenderer(flurryViewBinder);
Facebook: Starting with the 4.99.0 release of the Facebook Audience Network SDK, you will be using a dedicated
ViewBinderfor Facebook (
FacebookViewBinder), like so:
FacebookAdRenderer facebookAdRenderer = new FacebookAdRenderer( new FacebookAdRenderer.FacebookViewBinder.Builder(R.layout.native_ad_list_item) .titleId(R.id.native_title) .textId(R.id.native_text) // Binding to new layouts from Facebook 4.99.0+ .mediaViewId(R.id.native_main_image) .adIconViewId(R.id.native_icon_image) .adChoicesRelativeLayoutId(R.id.native_ad_choices_relative_layout) .advertiserNameId(R.id.native_title) // Bind either the titleId or advertiserNameId depending on the FB SDK version // End of binding to new layouts .callToActionId(R.id.native_cta) .build());
To know which specific layout elements to use in your XML for the
AdIconView, and AdChoices
FacebookViewBinder, refer to this sample XML layout.
Starting with the 5.2.0 release of the Facebook Audience Network SDK, you have the option to simplify your ad layout by using a pre-defined template provided by Facebook. Instead of binding to your own XML views like above, simply import and create a new
FacebookTemplateRenderer(which is a new adapter class introduced in the 18.104.22.168 release) and pass in a new
NativeAdViewAttributes()like below, and you are good to go:
FacebookTemplateRenderer facebookAdRenderer = new FacebookTemplateRenderer(new NativeAdViewAttributes());
Note: Publishers are encouraged to use the
FacebookTemplateRendererfrom above only with MoPub’s manual native ad integration (
MoPubNative) at the moment because of layout issues integrating with the Ad Placer (
MoPubAdAdapter). You might notice an irregular layout of assets when using the
FacebookTemplateRendererin conjunction with the MoPub Ad Placer. This behavior has been confirmed by the Facebook Audience Network team.
Finally, register your ad renderers, as shown below. Your
classThatRegistersAdRenderersvaries depending on which native ad integration you are using. Remember to register your
// MoPubRecyclerAdapter MoPubRecyclerAdapter classThatRegistersAdRenderers = new MoPubRecyclerAdapter(...); // MoPubStreamAdPlacer MoPubStreamAdPlacer classThatRegistersAdRenderers = new MoPubStreamAdPlacer(...); // MoPubAdAdapter MoPubAdAdapter classThatRegistersAdRenderers = new MoPubAdAdapter(...); // MoPubNative MoPubNative classThatRegistersAdRenderers = new MoPubNative(...); classThatRegistersAdRenderers.registerAdRenderer(googlePlayServicesAdRenderer); classThatRegistersAdRenderers.registerAdRenderer(flurryNativeAdRenderer); classThatRegistersAdRenderers.registerAdRenderer(facebookAdRenderer); classThatRegistersAdRenderers.registerAdRenderer(staticAdRender);
Last updated August 23, 2019
TWITTER, MOPUB, and the Bird logo are trademarks of Twitter, Inc. or its affiliates. All third party logos and trademarks included are the property of their respective owners.
© 2019 MoPub (a division of Twitter, Inc.)