Integrate Third-Party Ad Networks on Android

MoPub partners with most ad networks and supports these mediation partners with pre-built adapters. Refer to our list of supported ad networks. You can easily integrate our pre-built adapters by using the MoPub Mediation Integration Tool. You can still mediate ad networks with whom we do not partner (unsupported networks) by using third-party adapters.

Use this guide to set up mediation by downloading adapters and integrating them into your app project. There are two ways to integrate networks: through our MoPub Mediation Integration Tool (recommended), or manually via source code.

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 customize anything.

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. The mediation adapter AARs include AndroidManifest dependencies for each ad network.

Integrate Adapters Manually via Source Code (GitHub)

If you prefer, you can still integrate adapters manually using the existing process. We have migrated our mediation adapters to a new GitHub repository.

  1. Download the mediation repository; that is, simply clone the entire repository and open it in Android Studio. You are now ready to build AARs using adapter source files for every network with which you want mediate.

  2. Open the gradle window in Android Studio and from the Network folder, select TasksBuildBuild.

    You can find the manually built AAR under ProjectNetworkbuildoutputsaar.

  3. Incorporate the aar from Step 2 into your project by adding the .aar file to the libs directory, if your gradle dependencies file has a rule of the form:

    implementation fileTree(dir: 'libs', include: '*.aar')

    Or you can use FileNewModuleImport 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 repository, and select the aar. Android Studio will copy it into your project. Then select the app module, right-click Open Module Settings, and add a Module Dependency on the aar module just created.

  4. Incorporate third-party SDKs into your project. This typically requires adding the network’s SDK .jar file to a libs directory, or adding its Gradle dependency (if available). You can usually find this information on the ad network’s support page.

  5. In the Publisher UI Networks tab, set up the corresponding network campaigns and target the proper ad units. Refer to our Networks tab documentation for more detail.

Set Up Ad Renderers for Native Ads

To run native ads through mediated ad networks, you must set up the native ad renderer. Create and register the appropriate renderer for each ad network to ensure that your native ad views are laid out as intended. Refer to our Networks tab documentation for more detail.

  1. 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()
    );
    
  2. Similarly, create ad renderers for third-party ad networks.

  • Google: Starting with the 15.0.1.0 release of the Google native ad adapters, the GooglePlayServicesAdRenderer instance requires a MoPub MediaViewBinder for compatibility with Google’s MediaView:

      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 FlurryViewBinder that wraps the regular ViewBinder created above. Register the Flurry renderer as shown:

      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, use a dedicated ViewBinder for Facebook (FacebookViewBinder), as shown:

      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 MediaView, AdIconView, and AdChoices RelativeLayout in the FacebookViewBinder, refer to this sample XML layout.

    Starting with the 5.2.0 release of the Facebook Audience Network SDK, you can 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 5.2.0.0 release) and pass in a new NativeAdViewAttributes() as shown below:

      FacebookTemplateRenderer facebookAdRenderer = new FacebookTemplateRenderer(new NativeAdViewAttributes());
    

    Note: At this time, we only encourage you to use the FacebookTemplateRenderer discussed above with MoPub’s manual native ad integration (MoPubNative), because of layout issues caused by using the Ad Placer (MoPubRecyclerAdapter, MoPubStreamAdPlacer, or MoPubAdAdapter). You may notice an irregular layout of assets when using the FacebookTemplateRenderer in conjunction with the MoPub Ad Placer. This behavior has been confirmed by the Facebook Audience Network team.

  • Pangle: Pangle uses a dedicated PangleAdViewBinder that wraps the regular ViewBinder created above. Register the Pangle renderer as shown:

        final PangleAdRenderer pangleAdRenderer = new PangleAdRenderer(
                new PangleAdViewBinder.Builder(R.layout.native_ad_pangle_list_item)
                        .callToActionId(R.id.native_cta)
                        .decriptionTextId(R.id.native_text)
                        .iconImageId(R.id.native_icon_image)
                        .titleId(R.id.native_title)
                        .mediaViewIdId(R.id.native_main_image) // Bind to <com.bytedance.sdk.openadsdk.adapter.MediaView /> in XML
                        .build());
    
  1. Finally, register your ad renderers, as shown below. Your classThatRegistersAdRenderers varies depending on which native ad integration you are using. Remember to register your MoPubStaticNativeAdRenderer instance last.

    // 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(pangleAdRenderer);
    classThatRegistersAdRenderers.registerAdRenderer(staticAdRender);
    

Last updated August 24, 2020

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.

© 2020 MoPub (a division of Twitter, Inc.)