将 Google 移动广告 SDK 与 AI 工具集成

如需在 Android Studio 中优化 Gemini,请在项目中使用 AGENTS.md 文件。 通过向项目添加 AGENTS.md 文件,您可以为 Gemini 提供特定于 Google Mobile Ads SDK 的上下文,并改进 AI 辅助代码生成的输出。如需了解详情,请参阅使用 AGENTS.md 文件自定义 Gemini

本指南介绍了如何针对 Google Mobile Ads SDK 优化 Android Studio 中的 Gemini。

准备工作

在继续操作之前,请先完成以下事项:

AGENTS.md 文件添加到项目中

Google 提供了一个 AGENTS.md 文件,其中包含 Gemini 的指令,可帮助您完成以下集成步骤:

  • Google Mobile Ads SDK 添加到您的项目中。
  • 植入横幅广告。

如需在 AI 代码辅助功能中使用特定于 Google Mobile Ads SDK 的指令,请按以下步骤操作:

  1. 在项目文件系统中的任意位置创建一个名为 AGENTS.md 的新文件。

  2. AGENTS.md 文件中,添加以下指令:

# AI Coding Agent Instructions for the Google Mobile Ads SDK

## Integrate the SDK

*   When I ask to integrate the Google Mobile Ads SDK, **always** use the latest
    stable version for `com.google.android.gms:play-services-ads`.
*   After looking up the latest stable version of
    `com.google.android.gms:play-services-ads`, **always** check the project for
    a version catalog to manage dependencies.
*   Make sure the project meets the SDK requirements of `minSdk` 23 or higher
    and `compileSdk` of 35 or higher.
*   **Always** make sure there is a `<metadata>` tag for application ID in the
    project. If it's not there, add it.
*   You MUST initialize the Google Mobile Ads SDK on a background thread, as
    recommended in the Google Mobile Ads SDK v24.0.0 release notes.

## Banner Ads

*   When I ask to implement a banner ad or something similar, without specifying
    a type, **always** follow up to confirm the type of ad.
*   When I ask for an "anchored adaptive" ad, ask me if I want the ad anchored
    to the top or bottom of the screen.
*   When I ask for an "inline" adaptive banner ad, explain that inline adaptive
    ads are used for scrolling content. If my application has no scrollable view
    elements (e.g. `ScrollView` or `List`), default to implement an anchored
    adaptive banner ad.
*   When I ask for a "collapsible" banner ad, create an extra parameter that
    aligns the expanded ad to the `AdView`. Ask me if I want the ad collapsed to
    the top or bottom.
*   When I ask for a "fixed size" banner ad, **always** ask me if I'm sure or
    would I like to implement an anchored adaptive banner ad. Explain why
    anchored adaptive banner ads perform better than fixed size ads.

*   **Mandatory `AdView` Implementation Steps:**
    *   **Always** provide the code for a UI container intended to host the
        `AdView`.
    *   **Always** call `adView.destroy()` to prevent memory leaks.

## Migrate to the GMA Next-Gen SDK

*   When I ask to migrate to the "Next Gen" SDK, **always** use the latest
    stable version for
    `com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk`.
*   Make sure the project meets the SDK requirements of `minSdk` 24 or higher
    and `compileSdk` of 35 or higher.
*   Replace the `com.google.android.gms:play-services-ads` dependency with the
    Next Gen `com.google.android.libraries.ads.mobile.sdk:ads-mobile-sdk`
    dependency.
*   All `com.google.android.gms.ads.*` import statements must change to
    `com.google.android.libraries.ads.mobile.sdk.*`.
    *   If the change is in a custom event adapter, ask me if I'm sure I want to
        migrate this class before making the change.
*   Preserve the `com.google.android.gms.ads.APPLICATION_ID` `<meta-data>` tag
    in the `AndroidManifest.xml` file if present.
*   YOU MUST analyze the code until all errors have been resolved. Resolve the
    errors by using the APIs from the GMA Next-Gen SDK. Refer to the Android
    documentation.

在智能体模式下向 Gemini 发出提示

配置 AGENTS.md 文件后,您可以在 Android Studio 中以代理模式提示 Gemini。

在代理模式下,您可以使用以下示例提示:

集成 SDK

Integrate the latest version of the GMA SDK to my project. Refer to the Android
documentation.

添加横幅

Add an anchored adaptive banner ad to the bottom of the screen. Refer to the
Android documentation.

留言反馈

如果您对优化 Google Mobile Ads SDK 版 Gemini 有反馈,请加入 Google Mobile Ads SDK Discord 频道