6

I'm experimenting with getting the Android Advertising Id, but I can't find the proper way. In fact, I can't even get the Advertising Id Provider. isAdvertisingIdProviderAvailable( ) always returns false. I'm using a Samsung with 8.0 + PlayStore and also on the emulator with 8.1 + Google Play, running as a debug build. I've followed this guide: https://developer.android.com/training/articles/ad-id This must be something simple, but I can't see it. Thanks for any suggestions.

I created a blank project and this is my code:

public class MainActivity extends AppCompatActivity {
    public static final String TAG="MYTAG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean isProvider = AdvertisingIdClient.isAdvertisingIdProviderAvailable(getApplicationContext());
        Log.i(TAG, "isProviderAvailable:" + isProvider);
    }
}

Gradle:

 android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "company.com.adidviewer"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.gms:play-services-ads:18.3.0'
    implementation 'androidx.ads:ads-identifier:1.0.0-alpha04'
    //implementation 'androidx.ads:ads-identifier-common:1.0.0-alpha04'
    //implementation 'androidx.ads:ads-identifier-provider:1.0.0-alpha04'
    // Used for the calls to addCallback() in the snippets on this page.
    //implementation 'com.google.guava:guava:28.0-android'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

Manifest

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true"/>
</application>
dalton
  • 81
  • 1
  • 7
  • Hey, i'm experiencing a similar behaviour, did you manage to make it work? – SamuelPS Apr 07 '20 at 14:04
  • I found these lines work, but this is from a non-AndroidX project. AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context); androidId=info.getId(); However, they only seem to work in the main thread. If called from another thread, I get a system message about Global Read no longer supported. – dalton Apr 07 '20 at 20:06

1 Answers1

6

If you want to get the advertising id for your published app, use the following approach:

Add this line to your gradle file;

com.google.android.gms:play-services-ads-identifier:16.0.0

And the code like this;

  private void determineAdvertisingInfo(Context context) {

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            try {
                AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
                // You should check this in case the user disabled it from settings
                if (!advertisingIdInfo.isLimitAdTrackingEnabled()) {
                    String id = advertisingIdInfo.getId();
                    // getUserAttributes(id);
                } else {
                    //If you stored the id you should remove it
                }
            } catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            }
        }
    });
}

There is a note specifying this in the document as well: Note

bikes
  • 121
  • 2
  • 4
  • What does this command return for you? boolean isProvider = AdvertisingIdClient.isAdvertisingIdProviderAvailable(getApplicationContext()); – dalton Apr 11 '20 at 18:35
  • 1
    I struggled for a day just like you. It always returned false with different devices. i don't know for what but I realized that this guide was not for get the user advertising id. – bikes Apr 12 '20 at 08:59