Get Started with Avocarrot

Overview

Examples

Android

Overview

- Native Ads

List/Feed

Custom

- Standard IAB Formats

Banner

Interstitial

Video

- Others

Troubleshooting

Admob Android Adapter

Mopub Android Adapter

DFP Android Adapter

iOS

Overview

- Native Ads

List/Feed

Custom

- Standard IAB Formats

Banner

Interstitial

Video

- Others

Troubleshooting

- Adapters

Admob iOS Adapter

Mopub iOS Adapter

DFP iOS Adapter

API Documentation

Reporting API

Help

Supported Networks & Ad Types

General

Earnings

Payments and Accounting

Integration

Reporting

Mediation Network Optimization

Legal and Privacy

Updates

SDK Change Log

Android Change Log

iOS Change Log

Custom

Updated 4 days ago ​by Derek Lim

Overview

This tutorial helps you setup Avocarrot Custom ads in your Android app and receive your first native ad.

Custom is an ad unit that provides you access to the raw assets (eg. Icon, Image, Title & etc) of a native ad.

Currently the custom ad unit does not support client mediation networks such as AdMob, Facebook, MoPub, etc.

Prerequisites

Create and Avocarrot account
Create a  Custom ad unit from your app on Avocarrot Dashboard 

Requirements & Dependencies

  • Android 4.0 (API Version 14) and up
  • support-annotations, v26.1.0
  • support-v4, v26.1.0
  • Recommended Google Play Services v11.4.2

We strongly recommend compiling your app using Google Play services, in order to use the Android Advertising IDinstead of the Device ID, as required by Google. Failing to correctly use the Android Advertising ID may result in your submission to the Play Store being rejected.

You must have a different adUnitId for every ad space placement in your app. If you use the same ID for multiple placements or the IDs from the testing/demo examples, reporting errors will occur and there will be no payout. Please ask your account manager for further details.

Setup SDK

The SDK is available via:
1. jCenter AAR (Recommended) 
2. Zipped AAR 

  1. Add the following rows in your app module build.gradle file:
     
    repositories {
        maven { url 'https://maven.google.com'}
        maven { url "https://s3.amazonaws.com/avocarrot-android-builds/dist/" }
    }
  2. Add the following dependencies:


    dependencies {
      ...
      compile 'com.avocarrot.sdk:mediation-sdk-native-assets:4.7.1'
      compile 'com.google.android.gms:play-services-ads:11.4.2'
     }
  3. Update AndroidManifest.xml
     
    <?xml version="1.0" encoding="utf-8"?>
    <manifest ... >
       <!-- Required -->
       <uses-permission android:name="android.permission.INTERNET" />
       <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
       <!-- Optional -->
       <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
       <application ...>
          <activity> ... </activity>
          <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
       </application>
    </manifest>
  1. Include the Local .aar Libraries

    You can find local aar files in our sample project under libraries here
    Save the aar files under app module’s libs folder (eg: <project>/<app>/libs) Then add the following rows in your app module build.gradle file:

    repositories {
      flatDir {
        dirs 'libs'
      }
    }
    
  2. To install SDK, include the following dependencies:

    dependencies {
        compile(name: 'core', version:'4.7.1', ext: 'aar')
        compile(name: 'mediation-sdk', version:'4.7.1', ext: 'aar')
        compile(name: 'mediation-sdk-native-assets', version:'4.7.1', ext: 'aar')
        compile 'com.google.android.gms:play-services-ads:11.4.2'
    }
    
  3. Then add following rows in your app module proguard-rules.pro file:

    -keep class com.avocarrot.** { *; }
    -dontwarn com.avocarrot.**
    // To ensure GAID can be extract from Google play services
    -keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info { *; }     
    
  4. Update AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest ... >
       <!-- Required -->
       <uses-permission android:name="android.permission.INTERNET" />
       <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
       <!-- Optional -->
       <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
       <application ...>
          <activity> ... </activity>
          <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
       </application>
    </manifest>
  • ACCESS_COARSE_LOCATION (recommended) – Grants the SDK permission to access approximate location based on cell tower.
  • ACCESS_FINE_LOCATION (recommended) – Grants the SDK permission to access a more accurate location based on GPS.
    Although not technically required, the LOCATION permissions make it possible for the SDK to send location-based data to advertisers. Sending better location data generally leads to better monetization.
  • WRITE_EXTERNAL_STORAGE (optional) – Allows the SDK to cache all ad assets (creatives, custom frames, etc.) in external memory. This can maximize performance by ensuring immediate delivery of ads and minimize network traffic used by the SDK by keeping cached ad assets available even after the user closes the app.
  • READ_PHONE_STATE (recommended) – Allows the SDK to handle calls interrupting video playback during videos.
If you’re already integrated with a demand buyer,  It is not mandatory to remove all SDKs, but we would recommend it in order to ensure a seamless and coherent integration with Avocarrot.
 When using SDK as a library project, you shouldn’t need to worry about merging AndroidManifest.xml changes or Proguard settings. If you run into problems, make sure manifestmerger.enabled is set to true in project.properties Please see the Android documentation here.

Setup Native Custom Ads

  1. Create and add a layout file, eg:

    <Linearlayout android:id="@+id/native_assets_ad_container" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"/>
    <Textview android:id="@+id/native_assets_ad_title" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Textview android:id="@+id/native_assets_ad_body" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">
    <Textview android:id="@+id/native_assets_ad_choices_caption" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Textview android:id="@+id/native_assets_ad_call_to_action" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Imageview android:id="@+id/native_assets_ad_icon" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Imageview android:id="@+id/native_assets_ad_image" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Imageview android:id="@+id/native_assets_ad_choices_icon" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    <Ratingbar android:id="@+id/native_assets_ad_star_rating" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>
    
  2. Setup your layout view by adding the following code to onCreate:
    adContainer = (LinearLayout) findViewById(R.id.native_assets_ad_container);
    title = (TextView) findViewById(R.id.native_assets_ad_title);
    body = (TextView) findViewById(R.id.native_assets_ad_body);
    adChoiceText = (TextView) findViewById(R.id.native_assets_ad_choices_caption);
    cta = (TextView) findViewById(R.id.native_assets_ad_call_to_action);
    icon = (ImageView) findViewById(R.id.native_assets_ad_icon);
    image = (ImageView) findViewById(R.id.native_assets_ad_image);
    adChoiceIcon = (ImageView) findViewById(R.id.native_assets_ad_choices_icon);
    rating = (RatingBar) findViewById(R.id.native_assets_ad_star_rating);
    
  3. Configure Builder for NativeAssetsAd to load image resources by adding this lines to it: 
    final NativeAssetsConfig.Builder config = new NativeAssetsConfig.Builder()
           .prefetchIcon(true)
           .prefetchImage(true)
           .prefetchAdChoiceIcon(true);
    nativeAssetsAd = NativeAssetsAdPool.load(this, "YOUR_CUSTOM_AD_UNIT_ID", config, nativeAssetsAdListener);
  4. Implement onAdLoaded method of NativeAssetAdCallback, eg:

    NativeAssetsAdCallback nativeAssetsAdListener = new NativeAssetsAdCallback() {
        @Override
        public void onAdLoaded(@NonNull NativeAssetsAd nativeAssetsAd, @NonNull NativeAssets nativeAssets) {            
            title.setText(nativeAssets.getTitle());
            body.setText(nativeAssets.getText());
            renderImageView(icon, nativeAssets.getIcon());
            renderImageView(image, nativeAssets.getImage());
            cta.setText(nativeAssets.getCallToAction());
            AdChoice adChoice = nativeAssets.getAdChoice();
            if (adChoice != null) {                       
                adChoiceIcon.setImageDrawable(adChoice.getIcon().getDrawable());
                nativeAssetsAd.registerAdChoiceViewForClick(adChoiceIcon);
            }
            renderRatingBarView(rating, nativeAssets.getRating());
            nativeAssetsAd.registerViewsForClick(Arrays.asList(title, icon, image, cta));
            nativeAssetsAd.registerViewForImpression(adContainer);
        }
        @Override
        public void onAdFailed(@NonNull NativeAssetsAd ad, @NonNull ResponseStatus responseStatus) {
        }
        @Override
        public void onAdOpened(@NonNull NativeAssetsAd ad) {
        }
        @Override
        public void onAdClicked(@NonNull NativeAssetsAd ad) {
        }
        @Override
        public void onAdClosed(@NonNull NativeAssetsAd ad) {
        }
    }
    
  5. Implement Render methods for Native assets:

    private static void renderImageView(@NonNull ImageView view, Image image) {
        if (image != null) {
            view.setVisibility(View.VISIBLE);
            view.setAdjustViewBounds(true);
            int width = image.getWidth();
            int height = image.getHeight();
            if (width > 0 && height > 0) {
                view.setMaxWidth(width);
                view.setMaxHeight(height);
            }
            view.setImageDrawable(image.getDrawable());
            } else {
                view.setImageDrawable(null);
                view.setVisibility(GONE);
            }
    }
    private static void renderRatingBarView(@NonNull RatingBar view, @Nullable Rating starRating) {
        view.setStepSize(0.1F);
        view.setIsIndicator(true);
        if (starRating != null) {
            view.setNumStars((int) starRating.getScale());
            view.setRating((float) starRating.getValue());
            view.setVisibility(View.VISIBLE);
        } else {
                view.setVisibility(GONE);
        }
    }
                
  6. onDestroy() method should be called depending on the activity lifecycle events.
     @Override
        public void onDestroy() {
            super.onDestroy();
            if (nativeAssetsAd != null) {
                nativeAssetsAd.destroy();
            }
        }
  7. Compile and Run your project to get your first sample native ad.

    Make sure to test this in an emulator that supports Google Play Services or in a mobile device.
  8. Congratulations! You can now customize your native ads to match the look and feel of your app.

Advanced Customization

Go Live!

  1. Remove sandbox If you have received a sample ad then simply disable the Sandbox mode by including the following instruction in your code:
    Avocarrot.setTestMode(false);
    You should disable Sandbox mode ONLY when you are done with the integration and you are ready to publish your app. It is against Avocarrot's policies to use live ads during development, and doing so could get your Avocarrot account banned.
  2. Test your implementation on real devices with real ads before rolling it out to production. Please make sure that your test device has a sim-card and 'Ads Personalization' setting is turned on.
  3. Check whether Google Play Services are installed within your application.
  4. Publish your app. You are now ready to upload your app and start making money! If you have submitted your app in the Google Play/ Apple Store and you can see live ads, then you have nothing to worry about. Just wait until the impressions threshold has been reached and the app will be shown as live in your dashboard as well.
  5. Update payment details. Login to your account and update your payment settings.

Was this article useful?