dependencies{implementation'androidx.ads:ads-identifier:1.0.0-alpha01'// Used for the calls to addCallback() in the snippets on this page.implementation'com.google.guava:guava:28.0-android'}
Kotlin
dependencies{implementation("androidx.ads:ads-identifier:1.0.0-alpha01")// Used for the calls to addCallback() in the snippets on this page.implementation("com.google.guava:guava:28.0-android")}
MyAdIdClient
Kotlin
// Used for the call to addCallback() within this snippet.importcom.google.common.util.concurrent.Futures.addCallbackprivatefundetermineAdvertisingInfo(){if(AdvertisingIdClient.isAdvertisingIdProviderAvailable()){valadvertisingIdInfoListenableFuture=AdvertisingIdClient.getAdvertisingIdInfo(applicationContext)addCallback(advertisingIdInfoListenableFuture,object:FutureCallback<AdvertisingIdInfo>{overridefunonSuccess(adInfo:AdvertisingIdInfo?){valid:String=adInfo?.idvalproviderPackageName:String=adInfo?.providerPackageNamevalisLimitTrackingEnabled:Boolean=adInfo?.isLimitTrackingEnabled}// Any exceptions thrown by getAdvertisingIdInfo()// cause this method to be called.overridefunonFailure(t:Throwable){Log.e("MY_APP_TAG","Failed to connect to Advertising ID provider.")// Try to connect to the Advertising ID provider again or fall// back to an ad solution that doesn't require using the// Advertising ID library.}},Executors.newSingleThreadExecutor())}else{// The Advertising ID client library is unavailable. Use a different// library to perform any required ad use cases.}}
Java
// Used for the call to addCallback() within this snippet.importcom.google.common.util.concurrent.Futures;privatevoiddetermineAdvertisingInfo(){if(AdvertisingIdClient.isAdvertisingIdProviderAvailable()){ListenableFuture<AdvertisingIdInfo>advertisingIdInfoListenableFuture=AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());Futures.addCallback(advertisingIdInfoListenableFuture,newFutureCallback<AdvertisingIdInfo>(){@OverridepublicvoidonSuccess(AdvertisingIdInfoadInfo){Stringid=adInfo.getId();StringproviderPackageName=adInfo.getProviderPackageName();booleanisLimitTrackingEnabled=adInfo.isLimitTrackingEnabled();// Any exceptions thrown by getAdvertisingIdInfo()// cause this method to be called.@OverridepublicvoidonFailure(Throwablethrowable){Log.e("MY_APP_TAG","Failed to connect to Advertising ID provider.");// Try to connect to the Advertising ID provider again// or fall back to an ad solution that doesn't require// using the Advertising ID library.}});}else{// The Advertising ID client library is unavailable. Use a different// library to perform any required ad use cases.}}
广告 ID 库架构
图 1. 广告 ID 库架构
图 1 显示了广告 ID 库的结构。
该库包含以下模块:
客户端模块,它是应用中包含的一个薄层。
提供程序模块,由设备制造商提供。
此模块的实现必须定义设置界面,供用户重置其广告 ID 和切换广告跟踪偏好设置。
客户端模块与提供程序模块进行通信以检索广告
与广告跟踪相关的用户 ID 和用户偏好设置。
广告 ID 库如何处理多个提供程序
一台设备可以在
。如果广告 ID 库检测到这种情况,它会确保您的应用始终从同一个提供程序检索信息,但前提是该提供程序保持可用。此过程可让广告 ID 保持一致。
如果可用的广告提供商数量随时间推移而发生变化,并且您的应用
与其他广告标识符提供程序互动时,所有其他客户端应用都会启动
使用以下新的提供程序您的应用将表现出与用户请求重置其广告 ID 时相同的行为。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["Get a user-resettable advertising ID Part of [Android Jetpack](/jetpack).\n=========================================================================\n\n| **Note:** If you plan to publish and distribute your app using Google Play, use the [ads identifier\n| library](https://developers.google.com/android/reference/com/google/android/gms/ads/identifier/AdvertisingIdClient) that's available as part of Google Play services instead of this library.\n\nTo protect user privacy, it's a best practice for all Android apps to work with\nuser-resettable identifiers. One such identifier is an *advertising ID*, which\nuniquely identifies a particular user for advertising use cases, such as ad\npersonalization.\n\nTo support a standardized ad-tracking solution across the devices running your\napp, you can use the [Advertising ID library](/jetpack/androidx/releases/ads).\nThis library, which is available on devices running Android 4.0 (API level 14)\nand higher, defines an interface to interact with system-level ad providers.\nThis interface allows your app to receive consistent advertising ID values.\n\nThe ad provider that's included with the Advertising ID library also defines a\nstandard intent for opening a settings screen that the ad provider implements.\nThis settings screen allows the user to reset their advertising ID and opt out\nof ad personalization.\n\nThis guide explains how to use the client module of the Advertising ID library\nto obtain a consistent advertising ID on a per-device-user basis. This guide\nthen presents an overview of the library's architecture.\n\nConfigure your client app\n-------------------------\n\nBy interacting with the client module of the Advertising ID library, your app\ncan retrieve a consistent advertising ID that represents the user who is\ninteracting with the app.\n\nThe advertising ID is represented using version 3 of the [universally unique\nidentifier (UUID)\nformat](https://www.rfc-editor.org/rfc/rfc4122.txt) or an\nequivalent 128-bit format: \n\n```\n38400000-8cf0-11bd-b23e-10b96e40000d\n```\n\nThe Advertising ID library normalizes the return value as necessary to provide\nIDs using this format.\n| **Note:** Most ad providers make sure that the advertising ID is unique *per device\n| user*. On Android devices that support multiple users, including guest users, it's possible for your app to obtain different advertising IDs on the same device. These different IDs correspond to different users who could be signed in on that device.\n\nTo retrieve the user-resettable advertising ID for your app, complete the\nfollowing steps:\n\n1. Check whether an ad provider is available by calling\n [`AdvertisingIdClient.isAdvertisingIdProviderAvailable()`](/reference/androidx/ads/identifier/AdvertisingIdClient#isAdvertisingIdProviderAvailable(android.content.Context)).\n If this method returns\n `false`, your app should use another means to perform any required ad-tracking\n use cases.\n\n | **Caution:** Even if this method returns `true`, your app should be able to handle any exceptions that could occur when attempting to retrieve the advertising ID.\n2. Get the ad identifier details, including the advertising ID, by calling\n [`AdvertisingIdClient.getAdvertisingIdInfo()`](/reference/androidx/ads/identifier/AdvertisingIdClient#getAdvertisingIdInfo(android.content.Context)). The Advertising ID library\n executes this method on a worker thread and uses a 10-second connection timeout.\n\n | **Note:** Because the user can reset their advertising ID after your app starts, you should call `AdvertisingIdClient.getAdvertisingIdInfo()` each time your app needs to check the value of the ID. Don't cache the value.\n\nThe following code snippet demonstrates how to retrieve the advertising ID\nalong with other information from the ad provider:\n\napp/build.gradle \n\n### Groovy\n\n```groovy\ndependencies {\n implementation 'androidx.ads:ads-identifier:1.0.0-alpha01'\n\n // Used for the calls to addCallback() in the snippets on this page.\n implementation 'com.google.guava:guava:28.0-android'\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n implementation(\"androidx.ads:ads-identifier:1.0.0-alpha01\")\n\n // Used for the calls to addCallback() in the snippets on this page.\n implementation(\"com.google.guava:guava:28.0-android\")\n}\n```\n\nMyAdIdClient \n\n### Kotlin\n\n```kotlin\n// Used for the call to addCallback() within this snippet.\nimport com.google.common.util.concurrent.Futures.addCallback\n\nprivate fun determineAdvertisingInfo() {\n if (AdvertisingIdClient.isAdvertisingIdProviderAvailable()) {\n val advertisingIdInfoListenableFuture =\n AdvertisingIdClient.getAdvertisingIdInfo(applicationContext)\n\n addCallback(advertisingIdInfoListenableFuture,\n object : FutureCallback\u003cAdvertisingIdInfo\u003e {\n override fun onSuccess(adInfo: AdvertisingIdInfo?) {\n val id: String = adInfo?.id\n val providerPackageName: String = adInfo?.providerPackageName\n val isLimitTrackingEnabled: Boolean =\n adInfo?.isLimitTrackingEnabled\n }\n\n // Any exceptions thrown by getAdvertisingIdInfo()\n // cause this method to be called.\n override fun onFailure(t: Throwable) {\n Log.e(\"MY_APP_TAG\",\n \"Failed to connect to Advertising ID provider.\")\n // Try to connect to the Advertising ID provider again or fall\n // back to an ad solution that doesn't require using the\n // Advertising ID library.\n }\n }, Executors.newSingleThreadExecutor())\n } else {\n // The Advertising ID client library is unavailable. Use a different\n // library to perform any required ad use cases.\n }\n}\n```\n\n### Java\n\n```java\n// Used for the call to addCallback() within this snippet.\nimport com.google.common.util.concurrent.Futures;\n\nprivate void determineAdvertisingInfo() {\n if (AdvertisingIdClient.isAdvertisingIdProviderAvailable()) {\n ListenableFuture\u003cAdvertisingIdInfo\u003e advertisingIdInfoListenableFuture =\n AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());\n Futures.addCallback(advertisingIdInfoListenableFuture,\n new FutureCallback\u003cAdvertisingIdInfo\u003e() {\n @Override\n public void onSuccess(AdvertisingIdInfo adInfo) {\n String id = adInfo.getId();\n String providerPackageName =\n adInfo.getProviderPackageName();\n boolean isLimitTrackingEnabled =\n adInfo.isLimitTrackingEnabled();\n\n // Any exceptions thrown by getAdvertisingIdInfo()\n // cause this method to be called.\n @Override\n public void onFailure(Throwable throwable) {\n Log.e(\"MY_APP_TAG\",\n \"Failed to connect to Advertising ID provider.\");\n // Try to connect to the Advertising ID provider again\n // or fall back to an ad solution that doesn't require\n // using the Advertising ID library.\n }\n });\n } else {\n // The Advertising ID client library is unavailable. Use a different\n // library to perform any required ad use cases.\n }\n}\n```\n\nAdvertising ID library architecture\n-----------------------------------\n\n**Figure 1.** Advertising ID library architecture\n\nFigure 1 depicts the structure of the Advertising ID library.\nThe library consists of the following modules:\n\n- A *client module*, which is a thin layer included in apps.\n- A *provider module*, which the device manufacturer makes available. Implementations of this module must define a settings UI to give users the ability to reset their advertising ID and toggle ad tracking preferences.\n\nThe client module communicates with the provider module to retrieve advertising\nIDs and user preferences regarding ad tracking.\n\n### How the library handles multiple providers\n\nIt's possible for a device to support multiple system-level ad providers at the\nsame time. If the Advertising ID library detects this situation, it ensures that\nyour app always retrieves information from the same provider, assuming that the\nprovider remains available. This process keeps the advertising ID consistent.\n\nIf the set of available ad providers changes over time and your app\ninteracts with a different ad identifier provider, all other client apps start\nusing that new provider as well. Your app demonstrates the same behavior that\nwould occur if the user had requested to reset their advertising ID.\n\nThe Advertising ID provider library uses the following deterministic order to\nrank the providers:\n\n1. Providers that have requested the `androidx.ads.identifier.provider.HIGH_PRIORITY` permission.\n2. Providers that have been installed on the device for the longest time.\n3. Providers that appear first in alphabetical order."]]