Skip to content
MrZaitsev edited this page Oct 13, 2017 · 28 revisions

Requirements

SmartyAds in-app advertising SDK requires Android SDK version 14 or higher.

Getting Started

Just a few steps to start:

  1. Register your account on SmartyAds Supply Side Platform.

  2. Confirm your registration by following the confirmation link sent via email

  3. Create your first mobile inventory, it should be reviewed and approved.

  4. After this, you will be granted access to create placements for your inventory, Add Placement button should become clickable.

  5. Click on Add Placement, add the targeting options, floor price and size of your placement, then save your changes.

  6. Please note the Placement ID(e.g., ID#1234) below it's title. It will be used later in your code to initialize the ad container.

Installation

  • Provide Maven dependency into your Project gradle.build:
repositories {
        jcenter()
        // Smarty Maven dependepcies
        mavenCentral()
        mavenLocal()
        flatDir {
            dirs '../libs'
        }
        maven { url "https://jitpack.io" }
        maven { url "https://s3.amazonaws.com/moat-sdk-builds" }
        maven { url "https://dl.bintray.com/smartyads/maven/" }

        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
  • Include sdk dependency into your Module gradle.build:
  compile('com.smartyads:ad-container:0.4.5') {
    transitive = true
  }
  • Synchronize project with Gradle files (Gradle sync)

OR

  • Download aar lib from latest release: ad-container-release.aar
  • In Android studio: File -> New -> New Module and choose Import .jar/.aar Package, press next
  • File name: path to aar
  • File -> Project Structure -> {your_module_tab} -> dependencies and add just imported module as dependency

* Important!

Enable Multidex for your app.

Setup App Permissions (optional)

Edit your Android manifest to include the following (optional but recommended) permissions:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.smartyads.sampleapp">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  <!-- Grants the SDK permission to access a more accurate location based on GPS -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- Grants the SDK permission to access approximate location based on cell tower -->
    
	<!-- your app description -->
</manifest>

SmartyAds SDK already has the following normal permissions required by the SDK:

  • <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  • <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  • <uses-permission android:name="android.permission.INTERNET" />

Setting up required parameters in strings.xml

Set the app distribution type(required by SDK) in your strings.xml file:

<integer name="distribution_type">{0|1}</integer>

where:

  • 0 - app is free
  • 1 - the app is a paid version

Now, add the banner_id to strings.xml

<string name="banner_id">{your_banner_id_here}</string>

Important!

Do not forget to replace the your_banner_id_here with the placement ID from the Platform(Step 6 of the Getting Started section)

e.g, <string name="banner_id">{1234}</string>

Initializing SmartyAds SDK

Extend android.app.Application class, override #attachBaseContext(Context) method and call com.smartyads.Ads#init(Context) method as shown below:

public class SampleApp extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        Ads.init(base);
    }
}

Show Banners

You can configure your banner ad view using XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:smartyads="http://schemas.android.com/apk/res-auto">

   <com.smartyads.adcontainer.BannerContainer
        android:id="@+id/banner_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        smartyads:adContainerId="@string/banner_id"
        smartyads:banner_width="300dp"
        smartyads:banner_height="250dp"
        smartyads:refreshTime="20"
        />

</LinearLayout>

Where

  • smartyads:adContainerId="@string/banner_id" - the placement ID you received
  • smartyads:banner_width="300dp" - placement width
  • smartyads:banner_height="250dp" - placement height
  • smartyads:refreshTime="20" - Optional. Time in seconds after which the banner will expire and will be reloaded automatically. By default 20 sec.

Make sure that xmlns:smartyads="http://schemas.android.com/apk/res-auto" was added to your root layout

Finally, call BannerContainer#loadAd()

BannerContainer bannerContainer = (BannerContainer) findViewById(R.id.banner_container);
bannerContainer.loadAd();

There is an alternative method BannerContainer#loadAd(BannerOnLoadListener) that would be useful if you want to show a banner manually at specific time or you just want to know whether the banner was loaded successfully or not.

bannerContainer.loadAd(new BannerOnLoadListener() {
    @Override
    public void onSuccess() {
        Log.d("YOUR_TAG","Banner succesfully loaded");
        bannerContainer.showAd();
    }

    @Override
    public void onFailure(Exception e) {
        Log.e("YOUR_TAG","Cannot load ad: " + e.getMessage());
    }
});

Show Interstitial

To show interstitial you just need to initialize ad container and call InterstitialAdContainer#loadAd() method as shown in example below

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(savedInstanceState == null){//activity first time showing condition
            new InterstitialAdContainer(this, getString(R.string.banner_id))
                    .interstitial.loadAd();//interstitial will be shown as soon as ad will be loaded
        }
    }
}

where R.string.banner_id - your interstitial id(see #important) Note:

Also, you can use InterstitialAdContainer#loadAd(BannerOnLoadListener) method if you want to show interstitial manually at specific time.

public class MainActivity extends AppCompatActivity {

    private Button showAdButton;
    private InterstitialAdContainer interstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showAdButton = (Button) findViewById(R.id.show_ad);
        showAdButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(interstitialContainer.isLoaded())
                    interstitialContainer.showAd();
            }
        });

        interstitial = new InterstitialAdContainer(this, getString(R.string.banner_id));
        interstitial.loadAd(new InterstitialListener() {
            @Override
            public void onSuccess() {
                Log.e("YOUR_TAG", "Ad loaded");
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("YOUR_TAG", "Cannot load interstitial, reason: " + e.getMessage());
            }

            @Override
            public void closed() {
                Log.d("YOUR_TAG", "Interstitial closed");
            }
        });
    }
}

Show Vast(Video ad)

Update your AndroidManifest.xml with new permission WRITE_EXTERNAL_STORAGE

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.smartyads.sampleapp">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- Required. Allows an application to write to external storage. -->

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  <!-- Optional. Grants the SDK permission to access a more accurate location based on GPS -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- Optional. Grants the SDK permission to access approximate location based on cell tower -->

	<!-- your app description -->
</manifest>

The usage of VideoContainer exactly the same as the usage of InterstitialAdContainer):

public class MainActivity extends AppCompatActivity {

    private Button showAdButton;
    private VideoContainer videoContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showAdButton = (Button) findViewById(R.id.show_ad);
        showAdButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(videoContainer.isLoaded())
                    videoContainer.showAd();
            }
        });

        videoContainer = new VideoContainer(this, getString(R.string.video_container_id));
        videoContainer.loadAd(new InterstitialListener() {
            @Override
            public void onSuccess() {
                Log.e("YOUR_TAG", "Ad loaded");
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("YOUR_TAG", "Cannot load interstitial, reason: " + e.getMessage());
            }

            @Override
            public void closed() {
                Log.d("YOUR_TAG", "Interstitial closed");
            }
        });
    }
}

Show Native

To show native you just need to initialize ad container through Builder and call NativeAdContainer#loadAd(loadListener) method as shown in example below. You should give to a Builder all fields that you specified while creating native placement on SmartyAds Supply Side Platform.

NativeAdContainer native = NativeAdContainer.Builder(getContext(),
          getContext().getString(R.string.native_placement_id)) 
          .setTitleView(myTitleView)
          .setDescriptionView(myDescriptionView)
          .setSponsoredTextView(mySponsoredTextView)
          .setPriceView(myPriceView)
          .setImageView(myImageView)
          .setCtaView(myActionButton)
          // Another specified fields
          .build();
native.loadAd(new NativeOnLoadListener() {

      @Override public void onSuccess() {
        switchLoadingState(false);
        showResult("success");
      }

      @Override public void onFailure(Exception e) {
        switchLoadingState(false);
        showResult("Failure: " + e.getMessage());
      }
    });

where R.string.native_placement_id - your native placement id(see #important)

Ads caching configuration

By default caching allowed for all containers(recommended option). If you want to disable ads caching use SdkConfig class as shown below:

public class SampleApp extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        SdkConfig.getConfigBuilder(base)
                        .setCacheEnabled(true)
                        .excludeCachingFor(
                                getString(R.string.banner_id)
                        ).apply();
        Ads.init(base);
    }
}

To disable caching for separate containers use SdkConfig.Builder#excludeCachingFor(Collection)

To disable caching for all containers use SdkConfig.Builder#setCacheEnabled(false)

Adding targeting params

Targeting params are data that are provided for the SDK that will improve ad targeting. The params are not required and can be provided if your application has such data. There are three targeting parameters types:

  • Gender: male, female, other - user gender
  • Year of birth - year of a user birthday
  • Keywords - user interests or intents

Usage:

AdContainer adContainer = new InterstitialAdContainer(this, getString(R.string.banner_id));
...
UserKeywords userKeywords = new UserKeywords()
                .addKeywords("interest 1", "interest 2")
                .addKeywords(Collections.singletonList("interest 3"));

adContainer.addTargetingData(Gender.MALE)
        .addTargetingData(new YearOfBirth(1990))
        .addTargetingData(userKeywords);

...

adContainer.loadAd();

Rendering Problems in Android Studio

In case you got rendering problems(VerifyError) in Preview mode, add -noverify VM option to Android Studio:

  • Menu Help -> Edit Custom VM Options...
  • Add -noverify key
  • Restart Android Studio

Contact Us

Repo maintainer: support@smartyads.com

Partnership, legal issues, other inquiries: publishers@smartyads.com

https://smartyads.com