Android SDK

899 views 24/04/2018 Shay Karpeles 0

Getting Started

This document details the process of integrating YouAppi’s SDK with your Android app.

If you have any question you can email us at support@youappi.com

The basic steps of integration are:

  • Add our SDK AAR and dependencies to your project.
  • Init the SDK.
  • Load an ad.
  • Show an ad.

Requirements:

Minimum API level: 9, Android 2.3

Integration

Integrate using AAR files

Our latest SDK version can be downloaded as a ZIP file from the following URL:

YouAppi Android SDK

The ZIP file contains:

  • SDK AAR file – youappi- android-sdk-moat.aar – YouAppi’s SDK
  • SDK AAR file – youappi- android-sdk-moat-admob.aar – YouAppi’s AdMob adapter
  • SDK AAR file – youappi- android-sdk-moat-mopub.aar – YouAppi’d MoPub adapter
  • Copyyouappi- android-sdk-moat.aar file to the apps\libs folder of your project.
  • Add the following to the project build.gradle file inside the repositories section:
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
  • Add to your app build.gradle file the following under dependencies section:
    dependencies {
        implementation name:'youappi-android-sdk-moat', ext:'aar'
        implementation 'com.google.code.gson:gson:2.6')
    }

Note: If Manifest Merger is disabled, the following items need to be added to the AndroidManifest.xml file:

  • Add AdActivity activity to <application> tag :
    <activity android:name="com.youappi.sdk.AdActivity"
       android:configChanges="screenSize|orientation"/>
  • These permissions should be added if your app is running on older devices:
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Integrate using repository

  • Add the following to the project build.gradle file inside the repositories section:
    maven { url "http://repository.youappi.com/repository/release" }
    
  • Add to your app build.gradle file the following under dependencies section:
    dependencies {
        implementation "com.youappi.sdk:youappi-sdk-android-moat:4.0.4"
    }

Demo App

You can download a demo app from GitHub at the following URL: youappi-sdk-android-demo

The demo 3 app contains:

  • app-demo – shows an example of using YouAppi SDK
  • app-demo-admob – shows example of using YouAppi SDK with AdMob
  • app-demo-mopub – shows example of using YouAppi SDK with MoPub

Using the SDK

  • Initialize YouAppi SDK as soon as your app loads with the app token supplied by YouAppi:
    YouAPPi.init(context, "<YOUR_APP_TOKEN>");
  • Please note: it is strongly recommended to initialize YouAppi SDK in the onCreate method of the Application object of your app. It will ensure YouAppi SDK instance is always properly initialized. Here is an example:
    import android.app.Application;
    import com.youappi.sdk.YouAPPi;
                
    public class YouAppApplication extends Application {
                    
          private static final String DEMO_TOKEN = "<YOUR_APP_TOKEN>;
     
          @Override
          public void onCreate() {
                super.onCreate();
     
                YouAPPi.init(this, DEMO_TOKEN);
          }
    }
    
  • Also, update the corresponding manifest.xml with the Application class name:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.youappi.sdk.demo">
    
        <application
            android:name = ".YouAppApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            ...
            ...
        </application>
    </manifest>
  • Create an ad instance for one of YouAppi’s products:
    • Rewarded Video
    • Interstitial Video
    • Interstitial Ad
    YARewardedVideoAd rewardedVideoAd = YouAPPi.getInstance().rewardedVideoAd("test_rewarded_video_ad");
    YAInterstitialVideoAd interstitialVideoAd = YouAPPi.getInstance().interstitialVideoAd("test_interstitial_ad");
    YAInterstitialAd interstitialAd = YouAPPi.getInstance().interstitialAd("test_interstitial_ad");
  • Please note: YouAppi ad unit id is created and controlled by the app developer. The ad unit id later appears in reports and helps to analyze performance for each ad unit. Any arbitrary value can be selected for ad unit id as long as it follows these rules:
    • it should be constructred only from letters (lowercase, uppercase)
    • numbers
    • underscores.
  • Show an ad (for example, a rewarded video ad):

    rewardedVideoAd.show();

Detailed example for Rewarded Video

Please note: this example is for rewarded video. Other products work in a similar way.

  • Create a rewarded video instance:
    YARewardedVideoAd rewardedVideoAd = YouAPPi.getInstance().rewardedVideoAd("test_rewarded_video_ad");
  • Add rewarded video event listener:

    rewardedVideoAd.setRewardedVideoAdListener(new YARewardedVideoAd.RewardedVideoAdListener() {
                @Override
                public void onRewarded(String s) {
                    
                }
     
                @Override
                public void onVideoStart(String s) {
     
                }
     
                @Override
                public void onVideoEnd(String s) {
     
                }
     
                @Override
                public void onVideoSkipped(String s, int i) {
     
                }
     
                @Override
                public void onCardShow(String s) {
     
                }
     
                @Override
                public void onCardClose(String s) {
     
                }
     
                @Override
                public void onCardClick(String s) {
     
                }
     
                @Override
                public void onLoadSuccess(String s) {
     
                }
     
                @Override
                public void onLoadFailure(String s, YAErrorCode yaErrorCode, Exception e) {
     
                }
     
                @Override
                public void onShowFailure(String s, YAErrorCode yaErrorCode, Exception e) {
     
                }
     
                @Override
                public void onAdStarted(String s) {
     
                }
     
                @Override
                public void onAdEnded(String s) {
     
                }
            });
  • Load rewarded video ad:
    rewardedVideoAd.load();
  • Once ad is loaded, the event onLoadSuccess is called and it can be shown by calling:

    rewardedVideoAd.show();
  • Ad availability can also be checked by calling:

    rewardedVideoAd.isAvailable();

    If you’re using proguard in your application the following entries should be added to your proguard file:

    -keep class com.google.gson.**{ *;}
    -keep class com.google.android.gms.**{*;}
    -keep class com.youappi.sdk.**{*;}
    -keep interface com.youappi.sdk.**{*;}
    -keep enum com.youappi.sdk.**{*;}
    -keepclassmembers class * {
       @android.webkit.JavascriptInterface <methods>;
    }

Logging and troubleshooting

after initializing the SDK you can set LogListener to get detailed log events from YouAppi’s SDK:

YouAPPi.getInstance().setLogListener(new Logger.LogListener()
   {
       @Override
       public void log(String tag, String message)
       {
           // This will be called each time YouAppi SDK generates a log message.
       }
   });

Load and Show best practices

  • Make sure to init the SDK as soon as the app starts. It might take few seconds to complete the init process.
  • Make sure to load the ad about 30 seconds before you want to show it since it takes time for the ad and assets to be prepared.
  • Make sure to show an ad as close as possible to its load, in order to have a better fill rate and relevant ads.
  • Make sure not to wait too long before showing an ad, since the ad will be expired 5 hours after being called for. In other words, “show” must be performed no more than 5 hours after the “load”.
  • Use ad event listeners in order to be notified when an ad is ready to be shown.
  • Use ad event listeners to handle load and show.
  • Loading an ad too many times without showing it might cause YouAppi servers to block the SDK from requests.
Tags: