google-mobile-ads-android-migrate-to-next-gen
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAI Migration Agent Instructions for the Google Mobile Ads SDK
Google Mobile Ads SDK的AI迁移Agent指南
Migration Workflow
迁移工作流
Use this checklist to track your migration progress:
- Configure Gradle:
-
Replacewith the latest stable version of
com.google.android.gms:play-services-ads. If you cannot access Maven directly, run the following command to fetch the latest version. The version returned in thecom.google.android.libraries.ads.mobile.sdk:ads-mobile-sdktag is the latest version of the GMA Next-Gen SDK:latestbashcurl -sS https://dl.google.com/dl/android/maven2/com/google/android/libraries/ads/mobile/sdk/ads-mobile-sdk/maven-metadata.xml | sed -n 's/.*<latest>\(.*\)<\/latest>.*/\1/p' -
Update(24+) and
minSdk(34+).compileSdk -
Excludeand
play-services-adsfrom all dependencies globally in the app-level build file to avoid duplicate symbol errors.play-services-ads-lite -
Sync Gradle before moving on to the next step.
-
- Per-File Migration:
- Refactor the codebase following the API Mapping and Method Mapping tables to migrate imports, class names, and method signature to GMA Next-Gen SDK.
- Verify and Build:
- Run to confirm a successful clean build. Resolve any GMA SDK related compile errors.
gradle build -x test
- Run
使用此清单跟踪迁移进度:
- 配置Gradle:
-
将替换为
com.google.android.gms:play-services-ads的最新稳定版本。如果无法直接访问Maven,运行以下命令获取最新版本。com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk标签中返回的版本即为GMA Next-Gen SDK的最新版本:latestbashcurl -sS https://dl.google.com/dl/android/maven2/com/google/android/libraries/ads/mobile/sdk/ads-mobile-sdk/maven-metadata.xml | sed -n 's/.*<latest>\(.*\)<\/latest>.*/\1/p' -
更新(24+)和
minSdk(34+)。compileSdk -
在应用级构建文件中全局排除所有依赖项中的和
play-services-ads,以避免重复符号错误。play-services-ads-lite -
同步Gradle后再进行下一步。
-
- 逐文件迁移:
- 遵循API映射和方法映射表重构代码库,将导入项、类名和方法签名迁移至GMA Next-Gen SDK。
- 验证与构建:
- 运行确认构建成功。解决所有与GMA SDK相关的编译错误。
gradle build -x test
- 运行
Core Migration Rules
核心迁移规则
- App ID Usage: Always use the value of the
meta-data tag from
com.google.android.gms.ads.APPLICATION_IDfor theAndroidManifest.xmlinapplicationId.InitializationConfig- Constraint: Preserve the tag in the manifest; it is still required for publishers using the User Messaging Platform SDK.
<meta-data>
- Constraint: Preserve the
- Initialization Sequence:
- Call on a background thread.
MobileAds.initialize() - Ensure is called before any other SDK methods.
initialize() - If using , bundle it into
RequestConfiguration. Do not callInitializationConfig.Builder.setRequestConfiguration()before initialization.MobileAds.setRequestConfiguration()
- Call
- UI Threading: MANDATORY: Callbacks in GMA-Next Gen SDK are invoked
on a background thread. ALL UI-RELATED OPERATIONS (e.g., Toasts, View
updates, Fragment transactions) MUST be wrapped in or
runOnUiThread {}within GMA SDK callbacks. SKIPPING THIS STEP WILL CAUSE THE APPLICATION TO CRASH.Dispatchers.Main.launch {} - Mediation: Classes implementing
MUST continue using
com.google.android.gms.ads.mediation.Adapter.com.google.android.gms.ads
- App ID使用: 务必使用中
AndroidManifest.xml元数据标签的值作为com.google.android.gms.ads.APPLICATION_ID中的InitializationConfig。applicationId- 约束: 保留清单中的标签;对于使用User Messaging Platform SDK的发布商,该标签仍然是必需的。
<meta-data>
- 约束: 保留清单中的
- 初始化顺序:
- 在后台线程调用。
MobileAds.initialize() - 确保在所有其他SDK方法之前调用。
initialize() - 如果使用,将其打包到
RequestConfiguration中。不要在初始化前调用InitializationConfig.Builder.setRequestConfiguration()。MobileAds.setRequestConfiguration()
- 在后台线程调用
- UI线程处理: 强制要求:GMA Next-Gen SDK中的回调会在后台线程触发。所有UI相关操作(如Toasts、视图更新、Fragment事务)必须在GMA SDK回调中用或
runOnUiThread {}包裹。跳过此步骤会导致应用崩溃。Dispatchers.Main.launch {} - 中介: 实现的类必须继续使用
com.google.android.gms.ads.mediation.Adapter。com.google.android.gms.ads
Format Specifics
格式细节
Banner Ads
Banner广告
- Use for loading GMA Next-Gen SDK banners.
com.google.android.libraries.ads.mobile.sdk.banner.AdView - The following API checks if a banner is collapsible:
.
adView.getBannerAd().isCollapsible()
- 使用加载GMA Next-Gen SDK的Banner广告。
com.google.android.libraries.ads.mobile.sdk.banner.AdView - 以下API用于检查Banner是否可折叠:。
adView.getBannerAd().isCollapsible()
Native Ads
Native广告
- NativeAdLoader: is abstract and cannot be instantiated. It is used statically (e.g.,
NativeAdLoader).NativeAdLoader.load(...) - The following APIs are now set on the :
NativeAdRequest.Builder.setCustomFormatIds(customFormatIds: List<String>).disableImageDownloading().setMediaAspectRatio(mediaAspectRatio: NativeMediaAspectRatio).setAdChoicesPlacement(adChoicesPlacement: AdChoicesPlacement).setVideoOptions(videoOptions: VideoOptions)
- Removal: Delete all "Mute This Ad" logic; it is unsupported in GMA Next-Gen SDK.
- MediaView: no longer has a direct
NativeAdvariable; usemediaViewto associate the ad with the view.registerNativeAd(nativeAd, mediaView)
- NativeAdLoader: 是抽象类,无法实例化。需静态使用(例如:
NativeAdLoader)。NativeAdLoader.load(...) - 以下API现在设置在上:
NativeAdRequest.Builder.setCustomFormatIds(customFormatIds: List<String>).disableImageDownloading().setMediaAspectRatio(mediaAspectRatio: NativeMediaAspectRatio).setAdChoicesPlacement(adChoicesPlacement: AdChoicesPlacement).setVideoOptions(videoOptions: VideoOptions)
- 移除: 删除所有"静音此广告"逻辑;GMA Next-Gen SDK不支持该功能。
- MediaView: 不再有直接的
NativeAd变量;使用mediaView将广告与视图关联。registerNativeAd(nativeAd, mediaView)
Ad preloading
广告预加载
- Unless specified in the mapping table, ad preloading methods in the GMA Next-Gen SDK retain the same API signatures and parameters as the Old SDK.
- 除非映射表中另有说明,GMA Next-Gen SDK中的广告预加载方法保留与旧版SDK相同的API签名和参数。
API Mapping
API映射
This table covers the main classes and their GMA Next-Gen SDK equivalents.
| Feature | Old SDK Import ( | GMA Next-Gen SDK Import ( |
|---|---|---|
| Core | ||
| Initialization | | |
| Initialization Listener | | |
| Ad Request | | Format specific (e.g. |
| Load Error | | |
| Full Screen Show Error | | |
| Request Configuration | | |
| Event Callbacks | | Format Specific (e.g., |
| Tools | ||
| Ad Inspector | | |
| Ad Inspector Listener | | |
| Formats | ||
| App Open | | |
| App Open Load | | |
| Banner | | |
| Banner Load | | |
| Banner Events | | |
| Interstitial | | |
| Interstitial Load | | |
| Ad Loader | | |
| Native | | |
| Native Custom Format Ad | | |
| Native Custom Click | | |
| Native Load | | |
| Native Ad View | | |
| Media Content | | |
| Media Aspect Ratio | | |
| Video Options | | |
| Video Controller | | |
| Rewarded | | |
| Rewarded Load | | |
| Rewarded Interstitial | | |
| Rewarded Interstitial Load | | |
| Paid Event Listener | | |
| Response Info | | |
| Adapter Response Info | | |
| Rewards | ||
| Reward Listener | | |
| Reward Item | | |
| Ad Value | | |
| Preloading | ||
| Configuration | | |
| Callback | | |
| Interstitial Preloader | | |
| Ad Manager | ||
| Ad Request | | |
| Ad View | | |
| App Event Listener | | |
此表涵盖主要类及其对应的GMA Next-Gen SDK等效类。
| 功能 | 旧版SDK导入路径 ( | GMA Next-Gen SDK导入路径 ( |
|---|---|---|
| 核心 | ||
| 初始化 | | |
| 初始化监听器 | | |
| 广告请求 | | 按格式区分(例如 |
| 加载错误 | | |
| 全屏展示错误 | | |
| 请求配置 | | |
| 事件回调 | | 按格式区分(例如 |
| 工具 | ||
| 广告检查器 | | |
| 广告检查器监听器 | | |
| 广告格式 | ||
| 开屏广告 | | |
| 开屏广告加载 | | |
| Banner广告 | | |
| Banner广告加载 | | |
| Banner广告事件 | | |
| 插屏广告 | | |
| 插屏广告加载 | | |
| 广告加载器 | | |
| Native广告 | | |
| Native自定义格式广告 | | |
| Native自定义点击 | | |
| Native广告加载 | | |
| Native广告视图 | | |
| 媒体内容 | | |
| 媒体宽高比 | | |
| 视频选项 | | |
| 视频控制器 | | |
| 激励广告 | | |
| 激励广告加载 | | |
| 激励插屏广告 | | |
| 激励插屏广告加载 | | |
| 付费事件监听器 | | |
| 响应信息 | | |
| 适配器响应信息 | | |
| 奖励 | ||
| 奖励监听器 | | |
| 奖励项 | | |
| 广告价值 | | |
| 预加载 | ||
| 配置 | | |
| 回调 | | |
| 插屏广告预加载器 | | |
| Ad Manager | ||
| 广告请求 | | |
| 广告视图 | | |
| 应用事件监听器 | | |
Method Mapping
方法映射
This table covers the main methods and their GMA Next-Gen SDK equivalents.
| Feature | Old SDK Method Signature | GMA Next-Gen SDK Method Signature |
|---|---|---|
| Core | ||
| MobileAds Initialization | | |
| InitializationConfig Builder | N/A | |
| Ad Request Builder | | |
| Add Network Extras (AdMobAdapter) | | |
| Add Network Extras (Ad Source Adapter) | | |
| Custom Targeting | | |
| Formats | ||
| App Open | | |
| Banner | | |
| Interstitial | | |
| Rewarded | | |
| Rewarded Interstitial | | |
| Native Builder | | |
| Native Load | | |
| Native Ad Register | | |
| Set an App Event Listener (Banner) | | |
| Set an App Event Listener (Interstitial) | | |
| Callbacks | ||
| onAdOpened | | |
| onAdClosed | | |
| onFailedToShowFullScreenContent | | |
| onAdLoaded | AdLoadCallback: | Parameter name is always |
| onAdFailedToLoad | | |
| onCustomFormatAdLoaded | | |
| onPaidEventListener | | |
| onVideoMute | | |
| onAdPreloaded | | |
| Preloading | ||
| Configuration | | |
| Response Info | ||
| Get Response Info | | |
| Loaded Adapter Responses Info | | |
| Get Adapter Responses | | |
| Get Mediation Adapter Class | | |
| Adapter Response Info | ||
| Ad Source ID | | |
| Ad Source Name | | |
| Ad Source Instance ID | | |
| Ad Source Instance Name | | |
此表涵盖主要方法及其对应的GMA Next-Gen SDK等效方法。
| 功能 | 旧版SDK方法签名 | GMA Next-Gen SDK方法签名 |
|---|---|---|
| 核心 | ||
| MobileAds初始化 | | |
| InitializationConfig构建器 | N/A | |
| 广告请求构建器 | | |
| 添加网络额外信息(AdMobAdapter) | | |
| 添加网络额外信息(广告源适配器) | | |
| 自定义定向 | | |
| 广告格式 | ||
| 开屏广告 | | |
| Banner广告 | | |
| 插屏广告 | | |
| 激励广告 | | |
| 激励插屏广告 | | |
| Native广告构建器 | | |
| Native广告加载 | | |
| Native广告注册 | | |
| 设置应用事件监听器(Banner广告) | | |
| 设置应用事件监听器(插屏广告) | | |
| 回调 | ||
| onAdOpened | | |
| onAdClosed | | |
| onFailedToShowFullScreenContent | | |
| onAdLoaded | AdLoadCallback: | 参数名称始终为 |
| onAdFailedToLoad | | |
| onCustomFormatAdLoaded | | |
| onPaidEventListener | | |
| onVideoMute | | |
| onAdPreloaded | | |
| 预加载 | ||
| 配置 | | |
| 响应信息 | ||
| 获取响应信息 | | |
| 已加载适配器响应信息 | | |
| 获取适配器响应 | | |
| 获取中介适配器类 | | |
| 适配器响应信息 | ||
| 广告源ID | | |
| 广告源名称 | | |
| 广告源实例ID | | |
| 广告源实例名称 | | |