Tom Hammond
Tom Hammond

Reputation: 6080

Unity Android Get Google Play Advertising ID

I'm attempting to get the google play advertising ID in Unity but it doesn't seem to be working at all.

Here's the code I'm using that I've found in a couple SO's like this one:

        AndroidJavaClass up = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = up.GetStatic<AndroidJavaObject> ("currentActivity");
        AndroidJavaClass client = new AndroidJavaClass ("com.google.android.gms.ads.identifier.AdvertisingIdClient");
        AndroidJavaObject adInfo = client.CallStatic<AndroidJavaObject> ("getAdvertisingIdInfo",currentActivity);

        advertisingID = adInfo.Call<string> ("getId").ToString();  

        using(AndroidJavaClass pluginClass = new AndroidJavaClass("example.com.Toast")) {
            if(pluginClass != null) {
                toastClass = pluginClass.CallStatic<AndroidJavaObject>("getInstance");
                activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
                    toastClass.Call("toastMessage", advertisingID);
                }));
            }
        } 

I have to do this on an actual device and haven't found a good way to actually log anything save a Toast message, which doesn't display anything here. But if I do this (which gets the android device ID) the toast displays just fine.

        AndroidJavaClass up = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = up.GetStatic<AndroidJavaObject> ("currentActivity");
        AndroidJavaObject contentResolver = currentActivity.Call<AndroidJavaObject> ("getContentResolver");  
        AndroidJavaClass secure = new AndroidJavaClass ("android.provider.Settings$Secure");
        string android_id = secure.CallStatic<string> ("getString", contentResolver, "android_id");

Any idea what I should be doing to get the Google Play Advertising ID?

I've also tried doing it within the jar code itself natively like this:

AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                AdvertisingIdClient.Info idInfo = null;
                try {
                    idInfo = AdvertisingIdClient.getAdvertisingIdInfo(ToastCLass.getInstance().context);
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String advertId = null;
                try{
                    advertId = idInfo.getId();
                }catch (NullPointerException e){
                    e.printStackTrace();
                }

                return advertId;
            }

            @Override
            protected void onPostExecute(String advertId) {
                Toast.makeText(ToastClass.getInstance().context, advertId, Toast.LENGTH_LONG).show();
            }

        };
        task.execute();

But that just causes an error on my app when it runs (I think because it's trying to run the AsyncTask on the UI thread?). Again hard, as I haven't really found a way to display the logs/errors.

It seems if I run my app on an emulator I can get to a log, which does help with logging out the info.

Upvotes: 1

Views: 4145

Answers (2)

Jerin A Mathews
Jerin A Mathews

Reputation: 8712

I know this answer is a bit late, but this may be helpful for someone who is facing this issue.

First of all the answer given by Tom is correct, it works. But the reason why this error occurs is, when we add aar file of a package, we are not including all the dependencies it has. You can actually do that by checking the pom file.

So in here, to avoid this error, you can just add play-services-ads-identifier-16.0.0.aar package along with play-service-ads. You can find this package in here

Upvotes: 2

Tom Hammond
Tom Hammond

Reputation: 6080

I ended up writing a custom class to access this, since the error was:

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.identifier.AdvertisingIdClient"

Here's the class I wrote:

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Looper;
import android.os.Parcel;
import android.os.RemoteException;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;

public final class AdvertisingIdClientInfo {

    public static final class AdInfo {
        private final String advertisingId;
        private final boolean limitAdTrackingEnabled;

        AdInfo(String advertisingId, boolean limitAdTrackingEnabled) {
            this.advertisingId = advertisingId;
            this.limitAdTrackingEnabled = limitAdTrackingEnabled;
        }

        public String getId() {
            return this.advertisingId;
        }

        public boolean isLimitAdTrackingEnabled() {
            return this.limitAdTrackingEnabled;
        }
    }

    public static AdInfo getAdvertisingIdInfo(Context context) throws Exception {
        if(Looper.myLooper() == Looper.getMainLooper()) throw new IllegalStateException("Cannot be called from the main thread");

        try { PackageManager pm = context.getPackageManager(); pm.getPackageInfo("com.android.vending", 0); }
        catch (Exception e) { throw e; }

        AdvertisingConnection connection = new AdvertisingConnection();
        Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
        intent.setPackage("com.google.android.gms");
        if(context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
            try {
                AdvertisingInterface adInterface = new AdvertisingInterface(connection.getBinder());
                AdInfo adInfo = new AdInfo(adInterface.getId(), adInterface.isLimitAdTrackingEnabled(true));
                return adInfo;
            } catch (Exception exception) {
                throw exception;
            } finally {
                context.unbindService(connection);
            }
        }
        throw new IOException("Google Play connection failed");
    }

    private static final class AdvertisingConnection implements ServiceConnection {
        boolean retrieved = false;
        private final LinkedBlockingQueue<IBinder> queue = new LinkedBlockingQueue<IBinder>(1);

        public void onServiceConnected(ComponentName name, IBinder service) {
            try { this.queue.put(service); }
            catch (InterruptedException localInterruptedException){}
        }

        public void onServiceDisconnected(ComponentName name){}

        public IBinder getBinder() throws InterruptedException {
            if (this.retrieved) throw new IllegalStateException();
            this.retrieved = true;
            return (IBinder)this.queue.take();
        }
    }

    private static final class AdvertisingInterface implements IInterface {
        private IBinder binder;

        public AdvertisingInterface(IBinder pBinder) {
            binder = pBinder;
        }

        public IBinder asBinder() {
            return binder;
        }

        public String getId() throws RemoteException {
            Parcel data = Parcel.obtain();
            Parcel reply = Parcel.obtain();
            String id;
            try {
                data.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                binder.transact(1, data, reply, 0);
                reply.readException();
                id = reply.readString();
            } finally {
                reply.recycle();
                data.recycle();
            }
            return id;
        }

        public boolean isLimitAdTrackingEnabled(boolean paramBoolean) throws RemoteException {
            Parcel data = Parcel.obtain();
            Parcel reply = Parcel.obtain();
            boolean limitAdTracking;
            try {
                data.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                data.writeInt(paramBoolean ? 1 : 0);
                binder.transact(2, data, reply, 0);
                reply.readException();
                limitAdTracking = 0 != reply.readInt();
            } finally {
                reply.recycle();
                data.recycle();
            }
            return limitAdTracking;
        }
    }
}

And then accessing it:

public void methodName(final Activity context) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    AdvertisingIdClientInfo.AdInfo adInfo = AdvertisingIdClientInfo.getAdvertisingIdInfo(context);
                    this._googleAdvertiserId = adInfo.getId();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

Then wrapped that up in a jar, and in Unity you can call it:

if (activityContext == null) {
            using(AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
            }
        }

        using(AndroidJavaClass pluginClass = new AndroidJavaClass("example.com.Class")) {
            if(pluginClass != null) {
                className = pluginClass.CallStatic<AndroidJavaObject>("getInstance");
                activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
                    className.Call("methodName", activityContext);
                }));
            }
        }

Upvotes: 0

Related Questions