Code Integration for Android

Get started with Prebid Mobile by creating a Prebid Server account.

Use Maven?

Easily include the Prebid Mobile SDK using Maven. Simply add this line to your gradle dependencies:

compile 'org.prebid:prebid-mobile-sdk:[0,1)'

Build framework from source

Build Prebid Mobile from source code. After cloning the repo, from the root directory run


to output the PrebidMobile framework for Android.

Ad Unit Setup for Android

Register Prebid Mobile ad units as early as possible in the application’s lifecycle. Each ad unit has an adUnitId which is an arbitrary unique identifier of the developer’s choice.

The steps for using Prebid Mobile are as follows:

  1. Create the ad units with ad unit ids and add sizes for banner ad units.
  2. Add a server-side configuration for each ad unit to Prebid Server Adapter.
  3. Set targeting parameters for the ad units. (Optional)
  4. Set the primary adserver for the bid to either DFP or MoPub. (Primary ad server is necessary to determine the caching mechanism.)
  5. Set the Host for the bid to AppNexus or Rubicon.
  6. Register the ad units with the adapter to start the bid fetching process.

How to Create Ad Units

Create the ad units that represent the ad spaces in your app using following APIs:

ArrayList<AdUnit> adUnits = new ArrayList<AdUnit>();

// Configure a Banner Ad Unit with size 320x50
BannerAdUnit adUnit1 = new BannerAdUnit("YOUR-AD-UNIT-ID-HERE", "YOUR-CONFIG-ID-HERE");
adUnit1.addSize(320, 50);

// Configure an Interstitial Ad Unit

// Add them to the list

Initialize the SDK

Once configuration is done, use the following API to initialize Prebid Mobile and start fetching Prebid ads for your list of ad units.

If you’re using DFP as your primary ad server, use the API like this:

// Register ad units for prebid.
try {
    Prebid.init(getApplicationContext(), adUnits, "YOUR-ACCOUNT-ID-HERE", Prebid.AdServer.DFP, Host.APPNEXUS);
} catch (PrebidException e) {

If you’re using MoPub as your primary ad server, use the API like this:

// Register ad units for prebid.
try {
    Prebid.init(getApplicationContext(), adUnits, "YOUR-ACCOUNT-ID-HERE", Prebid.AdServer.MOPUB, Host.APPNEXUS);
} catch (PrebidException e) {

Note that host should be the prebid server host you’re using.

Set Ad Server Targeting

The final step for implementing Prebid Mobile is to attach bid keywords on the ad object. You can either attach bids immediately or wait for ads before attaching bids. To attach bids immediately use the following API.

Prebid.attachBids(YOUR-AD-OBJECT-HERE, YOUR-AD-UNIT-ID-HERE, Context);

To wait for ads before attaching bids, implement the following listener.

public void onAttachComplete(Object adObj) {
	// using dfp implementation as an example
    if (adView != null && adObj != null && adObj instanceof PublisherAdRequest) {
        adView.loadAd((PublisherAdRequest) adObj);

Prebid Mobile will immediately tell your app whether it has a bid or not without waiting. If it does have a bid, the code below will attach the bids to the ad request by applying keyword targeting. Use the table below to see which ad objects are supported currently.

Primary Ad Server Ad Object Type Ad Object Load Method
DFP Banner PublisherAdRequest public void loadAd(PublisherAdRequest adRequest)
DFP Interstitial PublisherAdRequest public void loadAd(PublisherAdRequest adRequest)
MoPub Banner MoPubView public void loadAd()
MoPub Interstitial MoPubInterstitial public void load()

Enable Prebid With Auto Refresh On

Prebid Mobile Android does not update the bids automatically like iOS implementation. To enable prebid with auto refesh, the following code integration is required.

Primary Ad Server is MoPub

For MoPub banner, in the banner ad listener implementation, add the following API usage.

// MoPub Banner Listener Implementation
public void onBannerLoaded(MoPubView banner) {
    Prebid.attachBids(banner, YOUR-AD-UNIT-ID-HERE, Context);

public void onBannerFailed(MoPubView banner, MoPubErrorCode errorCode) {
    Prebid.attachBids(banner, YOUR-AD-UNIT-ID-HERE, Context);

Primary Ad Server is DFP

For DFP banner, the loadAd(AdRequest) has to be called again with updated bids info. If not, same set of bids will be used repeatedly until loadAd() is called with a new AdRequest. We recommend doing client side auto refresh yourself using code like the following:

final Handler handler = new Handler(Looper.getMainLooper());
Runnable refreshRunnable = new Runnable() {
    public void run() {
        Prebid.attachBids(request, YOUR-AD-UNIT-ID-HERE, Context);
        handler.postDelayed(this, 30000); // load ad with new bids every 30 seconds

// Assume some condition is triggered to stop the auto-refresh
boolean conditionToStopRefresh = true;
if(conditionToStopRefresh) {
    // remove refresh runnable and destroy the banner

If proguard is ON

Primary Ad Server is DFP

To avoid dfp class being obfuscated and prebid not working, add the following lines to your proguard file:

-keep class {
   public *;