使用 AI 工具整合 Google Mobile Ads SDK

如要最佳化 Gemini 版 Android Studio,請在專案中使用 AGENTS.md 檔案。將 AGENTS.md 檔案新增至專案,即可為 Gemini 提供 Google Mobile Ads SDK 專屬脈絡,進而提升 AI 輔助程式碼生成功能的輸出結果。詳情請參閱「使用檔案自訂 Gemini」。AGENTS.md

本指南說明如何針對 Google Mobile Ads SDK 最佳化 Gemini 版 Android Studio。

事前準備

請先完成下列事項,再繼續操作:

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.
*   Resolve each `com.google.android.gms.ads.*` class to its specific
    counterpart in `com.google.android.libraries.ads.mobile.sdk.*`.
    *   If the change involves the
        `com.google.android.gms.ads.mediation.Adapter` class, ask me if I'm sure
        I want to migrate this class before making the change.
    *   When you need a class, search for that class name specifically in the
        `com.google.android.libraries.ads.mobile.sdk` package. Read the class
        stub or source file found in the library. Look at the public method
        signatures in that file.
*   Preserve the `com.google.android.gms.ads.APPLICATION_ID` `<meta-data>` tag
    in the `AndroidManifest.xml` file if present.
*   When performing UI-related operations within Google Mobile Ads callbacks,
    make sure you explicitly dispatch them to the UI thread.
*   Run a build/compile check. If there are **0 errors** related to the files
    you modified and no more references to `com.google.android.gms.ads`,
    consider the migration complete.

### Migrate Banner Ads

*   Declare the preferred
    `com.google.android.libraries.ads.mobile.sdk.banner.AdView` class to load a
    "Next-Gen" banner ad instead of the deprecated `BannerAd` APIs.

在代理程式模式中輸入提示詞給 Gemini

設定 AGENTS.md 檔案後,您可以在 Agent Mode 中提示 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.

提供意見

如要提供有關最佳化 Gemini Google Mobile Ads SDK 的意見回饋,請加入 Google Mobile Ads SDK Discord 頻道