Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the new Android Advertiser id inside an SDK

Tags:

It makes a lot of sense that Android ad SDKs will use Android's the new advertiser id.

It seems that you can only get the id by using the google services sdk, as mentioned here: http://developer.android.com/google/play-services/id.html.

Using the google play services sdk, requires referencing the google-play-services_lib project, which causes several problems:

  1. A lot of SDKs are jars, meaning they can't use google-play-services_lib as is (because they can't include resources).
  2. If I only want the advertiser ID, I need to add google-play-services_lib to my project, which weights almost 1 MB.

Is there a way to only get the advertiser id, without using resources?

like image 568
dors Avatar asked Nov 20 '13 13:11

dors


People also ask

Does your app use Advertising ID This includes any SDKS that your app imports that use Advertising ID?

YES. Admobs uses Advertising ID. even if you did not put it in the manifest -> latest google ads sdk "play-services-ads" have it in their manifest and it is imported to yours.

What is an Advertising ID used for?

The Google advertising ID is a device identifier for advertisers that allows them to anonymously track user ad activity on Android devices. It has often also been called the Android advertising ID, but Google advertising ID (short form: GAID) is more commonly used.

Is it OK to delete Advertising ID?

The ad identifier - aka “IDFA” on iOS, or “AAID” on Android - is the key that enables most third-party tracking on mobile devices. Disabling it will make it substantially harder for advertisers and data brokers to track and profile you, and will limit the amount of your personal information up for sale.

How do I reset my Google ad ID on Android?

HOW TO RESET YOUR ANDROID ADVERTISING DEVICE ID. To reset your Android advertising ID, Open Google Settings on your Android device by tapping on menu and then on Google Settings once all apps are displayed on the screen. Locate and tap on the Ads menu under Services. Tap on “reset advertising ID” on the new page.


Video Answer


1 Answers

I ran into the same issue, if you just need the advertiserId you could interact with the Google Play Service directly using an Intent. Example of custom class:

import java.io.IOException; import java.util.concurrent.LinkedBlockingQueue; 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;  public final class AdvertisingIdClient {  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;     } } } 

Make sure that you are not calling this from the main UI thread. For example, use something like:

new Thread(new Runnable() {             public void run() {         try {             AdInfo adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);             advertisingId = adInfo.getId();             optOutEnabled = adInfo.isLimitAdTrackingEnabled();         } catch (Exception e) {             e.printStackTrace();                                     }                            } }).start(); 
like image 153
Adrian Avatar answered Sep 23 '22 11:09

Adrian