Skip to content

Getting Started

rprunskas edited this page Oct 27, 2022 · 58 revisions

1. General Info

  • AdformSDK runs on Android 4.0, so created project version should be 4.0 and above.

alt tag

  • The instructions described here are done on Android studio. These instructions should also be compatible with Intelli J.

2. Setting up libraries

2.1 Using maven repository

  • To add a library to the dependencies, first we need to specify repository location. This can be done by editing build.gradle file and by inserting snippet (specified below) right above the android configuration group.

      ...
      repositories {
      	maven { url "https://github.com/adform/adform-android-sdk/raw/master/releases/" }
      }
      ...
    
  • Then in the dependency group we need to specify that we will be using AdformSdk, and also add Google Play services.

      ...
      dependencies {
      	...
      	implementation 'com.adform.advertising.sdk:advertising-sdk:2.19.2'
            ...
      }
      ...
    

3. Set up Google Play and Adform SDK

  • By default, AdformSdk needs Google Play services, so if your project does not have it already, please insert it.

      dependencies {
      	...
          implementation 'com.google.android.gms:play-services-ads:18.2.0'
          ...
      }
    

Sample build.gradle:

apply plugin: 'com.android.application'

// This can be used if using maven repository to include library
// repositories {
//      maven { url "https://github.com/adform/adform-android-sdk/raw/master/releases/" }
// }

android {
    compileSdkVersion 20
    buildToolsVersion "19.1.0"

    defaultConfig {
        applicationId "com.adform.adformdemo"
        minSdkVersion 14
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.gms:play-services-ads:18.2.0'

    implementation 'com.adform.advertising.sdk:advertising-sdk:2.19.2'

}

4. Extend Application class

For the SDK to work properly, you need to extend a default or use already created Application class and add two steps.

  • Initialize service by adding adService = AdApplicationService.init();
  • Implement AdApplicationService.ServiceListener interface.

Sample Application class

public class DemoApplication extends Application implements AdApplicationService.ServiceListener {

    private AdApplicationService adService;

    @Override
    public void onCreate() {
        super.onCreate();
        // [mandatory] Initializes application service
        adService = AdApplicationService.init();
    }

    // [mandatory] A mandatory method for the SDK to work properly
    @Override
    public AdApplicationService getAdService() {
        return adService;
    }
}

5. Update AndroidManifest.xml

Update AndroidManifest.xml with additional information.

  • Add proper permissions for the sdk to work properly. This can be done by inserting snippet shown below between the <manifest></manifest> tags.
<!--[mandatory] Base permissions that are used in sdk-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--[optional] This is needed for additional information to be sent when collecting data-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--[optional] This is needed for additional information to be sent when collecting data-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--[optional] This is needed when location data is used in ad requests-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
  • The SDK needs an additional window to display its various states. To do that, insert snippet shown below between <application></application> tags.
<activity
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:name="com.adform.sdk.activities.AdActivity" android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode"/>
<activity
        android:name="com.adform.sdk.activities.AdformBrowserActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.NoTitleBar"/>                 
  • The SDK still uses Apache HTTP Legacy library, so you must include the following declaration within the element of AndroidManifest.xml
<uses-library android:name="org.apache.http.legacy" android:required="false" />
  • Sign app to use earlier created application class by adding android:name=".DemoApplication" in <application> properties.
<application
    android:name=".DemoApplication"...      

Sample AndroidManifest.xml:

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

    <!--Permissions that are used in AdformSDK-->

    <!--[mandatory] Base permissions that are used in sdk-->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <!--[optional] This is needed for additional information to be sent when collecting data-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!--[optional] This is needed for additional information to be sent when collecting data-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!--[optional] This is needed when location data is used in ad requests-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <!--[mandatory] 'android:name=".DemoApplication"' must be set to the extended Application class with SDK implementation-->
    <application
        android:name=".DemoApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".SampleActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--[mandatory] Must be set for the SDK to work properly -->
        <activity
            android:name="com.adform.sdk.activities.AdActivity"
            android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
            <activity
            android:name="com.adform.sdk.activities.AdformBrowserActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.NoTitleBar"/>     

        <uses-library android:name="org.apache.http.legacy" android:required="false" />

    </application>

</manifest>

        

6. Proguard(optional)

If you use Proguard in your project, in order to prevent ProGuard from stripping away needed classes or class members, add a keep options to your ProGuard config:

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

-keepattributes JavascriptInterface

And you're set!

Clone this wiki locally