Tích hợp SDK Quảng cáo của Google trên thiết bị di động với các công cụ AI

Để tối ưu hoá Gemini trong Android Studio, hãy sử dụng tệp AGENTS.md trong dự án của bạn. Bằng cách thêm tệp AGENTS.md vào dự án, bạn cung cấp cho Gemini ngữ cảnh cụ thể cho Google Mobile Ads SDK và cải thiện kết quả của tính năng tạo mã dựa trên AI. Để biết thông tin chi tiết, hãy xem bài viết Tuỳ chỉnh Gemini bằng AGENTS.mdtệp.

Hướng dẫn này trình bày cách tối ưu hoá Gemini trong Android Studio cho Google Mobile Ads SDK.

Trước khi bắt đầu

Trước khi tiếp tục, hãy làm như sau:

Thêm tệp AGENTS.md vào dự án

Google cung cấp một tệp AGENTS.md có hướng dẫn cho Gemini để giúp bạn thực hiện các bước tích hợp sau:

  • Thêm Google Mobile Ads SDK vào dự án.
  • Triển khai quảng cáo biểu ngữ.

Để sử dụng các chỉ dẫn dành riêng cho Google Mobile Ads SDK trong tính năng hỗ trợ viết mã bằng AI, hãy làm theo các bước sau:

  1. Tạo một tệp mới có tên AGENTS.md ở bất kỳ đâu trong hệ thống tệp của dự án.

  2. Trong tệp AGENTS.md, hãy thêm các chỉ dẫn sau:

# 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.

Đưa ra câu lệnh cho Gemini ở Chế độ tác nhân

Sau khi định cấu hình tệp AGENTS.md, bạn có thể nhắc Gemini trong Android Studio ở Chế độ tác nhân.

Ở Chế độ đại lý, bạn có thể sử dụng các câu lệnh mẫu sau:

Tích hợp SDK

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

Thêm biểu ngữ

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

Gửi phản hồi

Nếu bạn có ý kiến phản hồi về việc tối ưu hoá Gemini cho Google Mobile Ads SDK, hãy tham gia Google Mobile Ads SDK kênh Discord.