Problems when adding Admob to Unity, can't build project because of console error!

2,783 views
Skip to first unread message

Anton Nelson

unread,
Dec 31, 2015, 9:01:42 AM12/31/15
to Google Mobile Ads SDK Developers
So my project was working just fine until I added Admob to the game.

Note that Google has absolutely NO HELP AT ALL in integrating Admob with Unity. The ONLY reason why I'm using Admob is because UnityAds doesn't support banner ads.

To set up UnityAds, all I had to do was log into unity, copy a script to my scene loader, and I was DONE. Simple, took about 5 minutes.

I've spent over 5 HOURS yesterday trying to get Admob to work with unity. After being redirected from one olbsolete link to another, finally, I found this video on YouTube: https://www.youtube.com/watch?v=q5T98X3EawA

After I did all the steps, I ran my project. The buttons showed (Request Banner, Show Banner etc...) , but clicking them did nothing. Surely, I thought, I'll try to build to an Android APK and see if it works on my tablet.

NOPE. When I went to build, I get this error: Unable to solve build tools directory. See the console for more details.

Error building Player: CommandInvokationFailure: Unable to resolve build tools directory. See the Console for more details.

C:\Program Files\Java\jdk1.8.0_45\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="C:/Program Files (x86)/Android/android-sdk\tools" -Dfile.encoding=UTF8 -jar "C:\Program Files\Unity\Editor\Data\PlaybackEngines\androidplayer/Tools\sdktools.jar" -

stderr[
Error:C:\Program Files (x86)\Android\android-sdk\tools is not a directory. calculated from system property com.android.sdkmanager.toolsdir
]

CommandInvokationFailure: Unable to resolve build tools directory. See the Console for more details.

C:\Program Files\Java\jdk1.8.0_45\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="C:/Program Files (x86)/Android/android-sdk\tools" -Dfile.encoding=UTF8 -jar "C:\Program Files\Unity\Editor\Data\PlaybackEngines\androidplayer/Tools\sdktools.jar" -

stderr[
Error:C:\Program Files (x86)\Android\android-sdk\tools is not a directory. calculated from system property com.android.sdkmanager.toolsdir
]

Error:C:\Program Files (x86)\Android\android-sdk\tools is not a directory. calculated from system property com.android.sdkmanager.toolsdir

UnityEditor.HostView:OnGUI()


If someone could PLEASE help me to get this to work, I'd be VERY, VERY grateful as so far every experience with a Google product has been awfully buggy.

Thank you so much, have a happy new year!






Veer Arjun Busani

unread,
Jan 4, 2016, 12:44:34 PM1/4/16
to Google Mobile Ads SDK Developers
Hi Anthon,

The Ads in Unity would only be displayed on an actual device. From the error log, it looks like the issue might be due to improper linking to the JDK.  Might I suggest you to double check your System Variables in the Advanced System Settings. Check if the JAVA_HOME environment variable is properly set. You can find them at System Properties -> Advance System Settings -> Environment Variables.

If this does not solve the issue, do get back to us with your implementation code and we would have a look into this further.

Thanks,
Veer Arjun Busani
Mobile Ads SDK Team

Anton Nelson

unread,
Jan 9, 2016, 6:39:58 PM1/9/16
to Google Mobile Ads SDK Developers
Hi there!

Thanks for getting back, and sorry that I'm so late :(

I noticed this problem occurs when I try to implement the Google Play Games leaderboards into my project. So the 2 seem to be conflicting.

Here is my code to show the ad, and currently it works. However when I add the Google Play Games plugin it stops me from building and it is very hard for me to create a new project from a backup.

using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoHandler : IInAppPurchaseHandler
{
    private readonly string[] validSkus = { "android.test.purchased" };

    //Will only be sent on a success.
    public void OnInAppPurchaseFinished(IInAppPurchaseResult result)
    {
        result.FinishPurchase();
        GoogleMobileAdsDemoScript.OutputMessage = "Purchase Succeeded! Credit user here.";
    }

    //Check SKU against valid SKUs.
    public bool IsValidPurchase(string sku)
    {
        foreach (string validSku in validSkus)
        {
            if (sku == validSku)
            {
                return true;
            }
        }
        return false;
    }

    //Return the app's public key.
    public string AndroidPublicKey
    {
        //In a real app, return public key instead of null.
        get { return null; }
    }
}

// Example script showing how to invoke the Google Mobile Ads Unity plugin.
public class GoogleMobileAdsDemoScript : MonoBehaviour
{

    void Start()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "ca-app-pub-1201473085202953/4067128022 ";
#elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
            string adUnitId = "unexpected_platform";
#endif

        RequestBanner();
        bannerView.Show();


    }

    private BannerView bannerView;
    private InterstitialAd interstitial;
    private static string outputMessage = "";

    public static string OutputMessage
    {
        set { outputMessage = value; }
    }

    private void RequestBanner()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "ca-app-pub-1201473085202953/4067128022 ";
#elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
            string adUnitId = "unexpected_platform";
#endif

        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
        // Register for ad events.
        bannerView.AdLoaded += HandleAdLoaded;
        bannerView.AdFailedToLoad += HandleAdFailedToLoad;
        bannerView.AdOpened += HandleAdOpened;
        bannerView.AdClosing += HandleAdClosing;
        bannerView.AdClosed += HandleAdClosed;
        bannerView.AdLeftApplication += HandleAdLeftApplication;
        // Load a banner ad.
        bannerView.LoadAd(createAdRequest());
       
    }

    private void RequestInterstitial()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "INSERT_ANDROID_INTERSTITIAL_AD_UNIT_ID_HERE";
#elif UNITY_IPHONE
            string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
            string adUnitId = "unexpected_platform";
#endif

        // Create an interstitial.
        interstitial = new InterstitialAd(adUnitId);
        // Register for ad events.
        interstitial.AdLoaded += HandleInterstitialLoaded;
        interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
        interstitial.AdOpened += HandleInterstitialOpened;
        interstitial.AdClosing += HandleInterstitialClosing;
        interstitial.AdClosed += HandleInterstitialClosed;
        interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
        GoogleMobileAdsDemoHandler handler = new GoogleMobileAdsDemoHandler();
        interstitial.SetInAppPurchaseHandler(handler);
        // Load an interstitial ad.
        interstitial.LoadAd(createAdRequest());
    }

    // Returns an ad request with custom ad targeting.
    private AdRequest createAdRequest()
    {
        return new AdRequest.Builder()
               // .AddTestDevice(AdRequest.TestDeviceSimulator)
               // .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
                .AddKeyword("game")
               // .SetGender(Gender.Male)
              //  .SetBirthday(new DateTime(1985, 1, 1))
                .TagForChildDirectedTreatment(false)
                .AddExtra("color_bg", "9B30FF")
                .Build();
      


    }

    private void ShowInterstitial()
    {
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
        }
        else
        {
            print("Interstitial is not ready yet.");
        }
    }

    #region Banner callback handlers

    public void HandleAdLoaded(object sender, EventArgs args)
    {
        print("HandleAdLoaded event received.");
    }

    public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleFailedToReceiveAd event received with message: " + args.Message);
    }

    public void HandleAdOpened(object sender, EventArgs args)
    {
        print("HandleAdOpened event received");
    }

    void HandleAdClosing(object sender, EventArgs args)
    {
        print("HandleAdClosing event received");
    }

    public void HandleAdClosed(object sender, EventArgs args)
    {
        print("HandleAdClosed event received");
    }

    public void HandleAdLeftApplication(object sender, EventArgs args)
    {
        print("HandleAdLeftApplication event received");
    }

    #endregion

    #region Interstitial callback handlers

    public void HandleInterstitialLoaded(object sender, EventArgs args)
    {
        print("HandleInterstitialLoaded event received.");
    }

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    }

    public void HandleInterstitialOpened(object sender, EventArgs args)
    {
        print("HandleInterstitialOpened event received");
    }

    void HandleInterstitialClosing(object sender, EventArgs args)
    {
        print("HandleInterstitialClosing event received");
    }

    public void HandleInterstitialClosed(object sender, EventArgs args)
    {
        print("HandleInterstitialClosed event received");
    }

    public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    {
        print("HandleInterstitialLeftApplication event received");
    }

    #endregion
}

Andrew Brogdon (Mobile Ads SDK Team)

unread,
Jan 11, 2016, 7:33:22 PM1/11/16
to Google Mobile Ads SDK Developers
The java compiler is complaining that it can't find a particular directory:

Error:C:\Program Files (x86)\Android\android-sdk\tools is not a directory. calculated from system property com.android.sdkmanager.toolsdir

When run via this command line:

C:\Program Files\Java\jdk1.8.0_45\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="C:/Program Files (x86)/Android/android-sdk\tools" -Dfile.encoding=UTF8 -jar "C:\Program Files\Unity\Editor\Data\PlaybackEngines\androidplayer/Tools\sdktools.jar"

I'd first check to see if that directory exists, then determine if the project settings are correctly referencing it. You'll note the path includes both forward and backward slashes, for instance, which could be throwing things off.

-Andrew
Reply all
Reply to author
Forward
0 new messages