Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by hammondt · Jan 03, 2016 at 11:01 PM · androidgoogle playgoogle-play-storedeviceuniqueidentifier

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. But it would be useful to do that with a real device too.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by viraf · Jan 14, 2017 at 06:29 AM

You can use Application.RequestAdvertisingIdentifierAsync

https://docs.unity3d.com/ScriptReference/Application.RequestAdvertisingIdentifierAsync.html

 using System;
 using UnityEngine;
 
 public class SampleBehaviour : MonoBehaviour
 {
     public void Start()
     {
         Application.RequestAdvertisingIdentifierAsync (
           (string advertisingId, bool trackingEnabled, string error) =>
             { Debug.Log ("advertisingId " + advertisingId + " " + trackingEnabled + " " + error); }
         );
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SamTyurenkov · Jan 20, 2021 at 11:26 PM 0
Share

No longer an option in Unity 2020+. Any replacement? I tried implementing java plug-in but, didn't succeed yet.

avatar image
0

Answer by hammondt · Jan 04, 2016 at 01:47 PM

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);
                 }));
             }
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

47 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Android Build crahes after submitting to play store 0 Answers

What is "good" Install/Uninstall rate for growing-good apps? 0 Answers

Unity : Read phone state permission android 0 Answers

I made a build using asset delivery and when I try to put it on the play console it gives me the following error 0 Answers

When using the google playservices plugin for android getting build errors in the GPGSAndroidSetupUI class 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges