0
votes

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?

2

2 Answers

1
votes

You shouldn't be messing with Java and the Facebook jar in Android Studio while there is an official Unity Facebook plugin. See this link for instructions on how to set it up and here to get the actual SDK.

When you download the plugin you will find InterstitialAdScene, RewardedVideoAdScene and NativeAdScene scenes that contains sample codes for you should load InterstitialAdScene scene since that's what you are looking for.

Below is C# Interstitial Ad example from the InterstitialAdTest.cs file in the AudienceNetwork\Samples\Interstitial folder.

public class InterstitialAdTest : MonoBehaviour
{

    private InterstitialAd interstitialAd;
    private bool isLoaded;

    // UI elements in scene
    public Text statusLabel;

    // Load button
    public void LoadInterstitial ()
    {
        this.statusLabel.text = "Loading interstitial ad...";

        // Create the interstitial unit with a placement ID (generate your own on the Facebook app settings).
        // Use different ID for each ad placement in your app.
        InterstitialAd interstitialAd = new InterstitialAd ("YOUR_PLACEMENT_ID");
        this.interstitialAd = interstitialAd;
        this.interstitialAd.Register (this.gameObject);

        // Set delegates to get notified on changes or when the user interacts with the ad.
        this.interstitialAd.InterstitialAdDidLoad = (delegate() {
            Debug.Log ("Interstitial ad loaded.");
            this.isLoaded = true;
            this.statusLabel.text = "Ad loaded. Click show to present!";
        });
        interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) {
            Debug.Log ("Interstitial ad failed to load with error: " + error);
            this.statusLabel.text = "Interstitial ad failed to load. Check console for details.";
        });
        interstitialAd.InterstitialAdWillLogImpression = (delegate() {
            Debug.Log ("Interstitial ad logged impression.");
        });
        interstitialAd.InterstitialAdDidClick = (delegate() {
            Debug.Log ("Interstitial ad clicked.");
        });

        // Initiate the request to load the ad.
        this.interstitialAd.LoadAd ();
    }

    // Show button
    public void ShowInterstitial ()
    {
        if (this.isLoaded) {
            this.interstitialAd.Show ();
            this.isLoaded = false;
            this.statusLabel.text = "";
        } else {
            this.statusLabel.text = "Ad not loaded. Click load to request an ad.";
        }
    }

    void OnDestroy ()
    {
        // Dispose of interstitial ad when the scene is destroyed
        if (this.interstitialAd != null) {
            this.interstitialAd.Dispose ();
        }
        Debug.Log ("InterstitialAdTest was destroyed!");
    }

    // Next button
    public void NextScene ()
    {
        SceneManager.LoadScene ("AdViewScene");
    }
}
0
votes

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.