i'm trying to display Facebook ads on Android. I have included the Audience Network .jar on my Android Studio Project. Also added the activity to the Android Manifest. This is the build.gradle:
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
testOptions {
unitTests.returnDefaultValues = true
}
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
}
debug {
debuggable true
jniDebuggable true
minifyEnabled false
shrinkResources false
}
}
}
dependencies {
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
compile 'com.google.android.gms:play-services:10.2.0'
compile files('libs/AudenceNetwork-4.24.0.jar')
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
}
This is a snippet on how i load and show interstitials and rewarded:
com.facebook.ads.InterstitialAd interstitial;
com.facebook.ads.RewardedVideoAd rewarded
public void loadInterstitial() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (interstitial == null) {
interstitial = new com.facebook.ads.InterstitialAd(activity, interstitialAd.getCode());
CustomFacebookInterstitialAdListener interestitialListener = new CustomFacebookInterstitialAdListener(AudienceNetworkAdProvider.this);
interstitial.setAdListener(interestitialListener);
}
interstitial.loadAd();
}
});
}
public void showInterstitial() {
if (isInterstitialAvailable()) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
interstitial.show();
}
});
}
}
public void loadRewardedAd() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (rewarded == null) {
rewarded = new com.facebook.ads.RewardedVideoAd(activity, rewardedAd.getCode());
CustomFacebookRewardedAdListener rewardedListener = new CustomFacebookRewardedAdListener(AudienceNetworkAdProvider.this);
rewarded.setAdListener(rewardedListener);
}
rewarded.loadAd();
}
});
}
public void showRewardedAd() {
if (isRewardedAdAvailable()) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
rewarded.show();
}
});
}
}
The code works fine on a Android native test app.
I'm making an adaptation to Unity, so i can show ads on a game. However, every time i want to display an ad it randomly crashes the app. Most of the times it let me see one interstitial or rewarded, but after that crashes.
This is how my Plugins/Android folder looks like:
AndroidManifest.xml
appcompat-v7-24.0.0
myaudiencenetworkadapter.aar
play-services-10.0.1.aar
play-services-auth-10.0.1
play-services-auth-base-10.0.1
play-services-basement-10.0.1
play-services-drive-10.0.1
play-services-tasks-10.0.1
recyclerview-v7-24.0.0
support-v4-24.0.0
It crashes when com.facebook.ads.RewardedVideoAd.show() method is invoked. This is the log of the crash:
08-07 16:51:30.861 16820-16820/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.somepackage.test, PID: 16820
java.lang.Error: FATAL EXCEPTION [main]
Unity version : 5.5.3f1
Device model : samsung SM-G925I
Device fingerprint: samsung/zeroltedv/zerolte:6.0.1/MMB29K/G925IDVS3EQF1:user/release-keys
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.ads.internal.DisplayAdController.c()' on a null object reference
at com.facebook.ads.RewardedVideoAd.show(Unknown Source)
at com.boxit.ads.facebook.AudienceNetworkAdProvider$3.run(AudienceNetworkAdProvider.java:168)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Any ideas?
I finally solved the issue. I was destroying Audience Network ads on the onPause event from my Unity activity. Every time an ad is displayed, onPause event is called so it was a mess.