-
Notifications
You must be signed in to change notification settings - Fork 6
Home
SmartyAds in-app advertising SDK requires Android SDK version 14 or higher.
Just a few steps to start:
-
Register your account on SmartyAds Supply Side Platfrom.
-
Confirm your registration by following the confirmation link sent via email
-
Create your first mobile inventory, it should be reviewed and approved.
-
After this, you will be granted access to create placements for your inventory, Add Placement button should become clickable.
-
Click on Add Placement, add the targeting options, floor price and size of your placement, then save your changes.
-
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.
- Download aar lib: 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
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" />
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>
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>
Extend android.app.Application class, override #attachBaseContext(Context) method and call com.smartyads.SmartyAds#init(Context) method as shown below:
public class SampleApp extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
SmartyAds.init(base);
}
}
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());
}
});
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);
InterstitialAdContainer interstitial = new InterstitialAdContainer(this, getString(R.string.banner_id));
interstitial.loadAd();
}
}
where R.string.banner_id - your interstitial id(see #important)
Also, you can use InterstitialAdContainer#loadAd(BannerOnLoadListener)
method if you want to show interstitial manually at specific time.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final InterstitialAdContainer interstitial = new InterstitialAdContainer(this, getString(R.string.banner_id));
interstitial.loadAd(new InterstitialListener() {
@Override
public void onSuccess() {
interstitial.showAd();
}
@Override
public void onFailure(Exception e) {
Log.e("YOUR_TAG", "Cannot load interstitial, reason: " + e.getMessage());
}
@Override
public void closed() {
Log.d("YOUR_TAG", "Tnterstitial closed");
}
});
}
}
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();
SmartyAds.init(base);
}
}
To disable caching for separate containers use SdkConfig.Builder#excludeCachingFor(Collection)
To disable caching for all containers use SdkConfig.Builder#setCacheEnabled(false)
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
Repo maintainer: [email protected]
Partnership, legal issues, other inquiries: [email protected]