Saturday, August 9, 2014

Integrating Tapjoy mobile monetization SDK with an Android app

Recently I tried some coding on integrating Tapjoy SDK into an Android application. Even though there are good documentations on how to do this, I decided to write the basic steps on my own words for future use. In this article, I only write down about how to put the relevant JAR files and configurations in an Android app to make an initial connection with Tapjoy back-end. The rest of the things are remaining for me to be explored in the future.  As the fist step, we create an account here to get access to the Tabjoy dashboard. After getting logged in to the dashboard, we need to create a new app from there. Select the platform as Android and put a name for our application. Application state is 'note live' yet. Note down the app ID and app secret key which we need in the next step. You will notice that the "Integrated?" filed in the dashboard is currently set to 'No'.

Download the Tapjoy SDK for Android from here and unzip the file. What we will really use from this SDK is the tapjoyconnectlibrary.jar file located in the Library folder in the SDK. Even though Tapjoy SDK comes with Google Play Services Library, I didn't use that since I already had integrated Google Play Services Library in my Android app. What we need to do now is copying the above mentioned JAR file into the 'libs' folder in the Android project in Eclipse. Right-click on the project and goto the Properties folder. In the 'Java build path' section, goto the 'Libraries' tab and we should choose 'Add External Jars' to browse and select the newly added JAR file. Put the check mark for the same library from the 'Order and Export' tab. Now, putting references to the JAR file done. What we need now is configuring our Android app to connect and use Tapjoy.
Add the following lines to the AndroidManifest.xml file.

Permissions:


1
2
3
4
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />          
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Activity details:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  <activity
      android:name="com.tapjoy.TJCOffersWebView"
          android:configChanges="orientation|keyboardHidden|screenSize" />
  <activity
          android:name="com.tapjoy.TapjoyFullScreenAdWebView"
          android:configChanges="orientation|keyboardHidden|screenSize" />
  <activity
          android:name="com.tapjoy.TapjoyVideoView"
          android:configChanges="orientation|keyboardHidden|screenSize" />
  <activity
          android:name="com.tapjoy.TJAdUnitView"
          android:configChanges="orientation|keyboardHidden|screenSize"
          android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
          android:hardwareAccelerated="true" />
  <activity
          android:name="com.tapjoy.mraid.view.ActionHandler"
          android:configChanges="orientation|keyboardHidden|screenSize" />
  <activity
          android:name="com.tapjoy.mraid.view.Browser"
          android:configChanges="orientation|keyboardHidden|screenSize" />

Add the following lines to the proguard.cfg file if we have it in the Android project.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
-keep class com.tapjoy.** { *; }
-keepattributes JavascriptInterface
-keep class * extends java.util.ListResourceBundle {
 protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
 @com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
 public static final ** CREATOR;
}
-keep class com.google.android.gms.ads.identifier.** { *; }

Now open the main activity of the Android project and add the following import and other statements in the relevant locations.

1
import com.tapjoy.TapjoyConnect;

Inside the onCreate() function: (app ID and app secret key should be obtained from the Tapjoy dashboard)

1
2
3
String appID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
String secretKey = "xxxxxxxxxxxxxxxxxxxxx";
TapjoyConnect.requestTapjoyConnect(this, appID, secretKey);

Inside onPause() funtion:


1
2
// Inform the Tapjoy SDK that the app has paused.  
TapjoyConnect.getTapjoyConnectInstance().appPause();

Now its time to build the project and run. After running the app on an Android device, we will notice that 'Integrated?' field in the Tapjoy dashboard becomes 'Yes' which means we have done the initial integration successfully.

No comments:

Post a Comment