How to get Advertising ID in android programmatically

I might be late but this might help someone else!

    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            AdvertisingIdClient.Info idInfo = null;
            try {
                idInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
            } 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(getApplicationContext(), advertId, Toast.LENGTH_SHORT).show();
        }

    };
    task.execute();

Get GAID(Google’s advertising ID)

1. Download latest Google Play Services SDK.
2. Import the code and add it as a library project.
3. Modify AndroidManifest.xml.

<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

4. Enable ProGuard to shrink and obfuscate your code in project.properties this line

 proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

5. Add rules in proguard-project.txt.

     -keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();  }

 -keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
        public static final *** NULL;      }

    -keepnames @com.google.android.gms.common.annotation.KeepName class *
    -keepclassmembernames class * {
        @com.google.android.gms.common.annotation.KeepName *;
    }

    -keepnames class * implements android.os.Parcelable {
        public static final ** CREATOR;
    }

6. Call AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext()).getId() in a worker thread to get the id in String. as 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(getApplicationContext());
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            String advertId = null;
            try{
                advertId = idInfo.getId();
            }catch (Exception e){
                e.printStackTrace();
            }
            return advertId;
        }
        @Override
        protected void onPostExecute(String advertId) {
            Toast.makeText(getApplicationContext(), advertId, Toast.LENGTH_SHORT).show();
        }
    };
    task.execute();

Enjoy!

or

https://developervisits.wordpress.com/2016/09/09/android-2/


You can call the below function in onCreate(Bundle savedInstanceState) of the activity

and in the logcat search for UIDMY then it will display the id like : I/UIDMY: a1cf5t4e-9eb2-4342-b9dc-10cx1ad1abe1

void getUIDs()
{
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            try {
                AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(SplashScreen.this);
                String myId = adInfo != null ? adInfo.getId() : null;
             
                Log.i("UIDMY", myId);
            } catch (Exception e) {
                Toast toast = Toast.makeText(context, "error occurred ", Toast.LENGTH_SHORT);
                toast.setGravity(gravity, 0,0);
                toast.show();
            }
        }
    });
}

Fetch the advertising id from background thread:

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            try {
                AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
                String adId = adInfo != null ? adInfo.getId() : null;
                // Use the advertising id
            } catch (IOException | GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException exception) {
                // Error handling if needed
            }
        }
    });

I added null checks to prevent any crashes. The Google example implementation code would crash with a NullPointerException if an exception occures.