Android Studio 还为各种不同的应用模块类型(包括 Wear OS、Android TV 和 Cloud App Engine)提供了模板。您可以在添加项目组件时查看这些适用于不同模块类型的模板。其他 API 特定的模块和 activity(例如 Google AdMob 广告和 Google 地图)也可以使用模板。
启动 Android Studio,然后依次点击 File > New Project,打开一个新项目。
选择 Gemini API Starter 模板。
在新项目界面上选择 Gemini API 模板。
第 2 步:生成 API 密钥
在向导的下一步中,选择项目名称和位置后,请提供 API 密钥以对 Gemini API 进行身份验证。如果您没有 Gemini API 密钥,请点击向导中提供的链接前往 Google AI Studio 并申请新密钥。完成后,将新的 API 密钥复制回向导。点击完成。
Gemini API 模板的新项目向导。将 API 密钥复制并粘贴到“新建项目”向导中。
第 3 步:开始原型设计
Android Studio 会自动为您设置一个与 Gemini API 建立连接的项目,从而简化您的工作流程。点击运行,在 Android 模拟器中查看代码的实际效果。该应用附带一个硬编码的提示,要求模型“为我总结以下文本”;您可以直接在代码中修改或扩展该提示,以更改模型的功能。如需详细了解如何创建提示,请参阅 Google AI Studio 文档。
开始使用 Gemini API 开发原型。
了解详情
如需详细了解适用于 Android 的 Google AI SDK,请查看适用于 Android 的 Google AI SDK 快速入门指南。如需获取更多代码示例,您可以通过依次选择 File > New > Import Sample,然后在搜索框中输入 Generative AI Sample,将生成式 AI 代码示例导入 Android Studio。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Add code from a template\n\nAndroid Studio provides code templates that follow the Android design and\ndevelopment best practices to get you on the right track to creating\nbeautiful, functional apps. You can use templates to create new app modules,\nindividual activities, or other specific Android project components.\n\nSome templates provide starter code for common usage contexts, such as\nnavigation drawers or login screens. You can choose from these app module\nand activity templates when you first [create your project](/studio/projects/create-project),\nwhen you [add a new app module within an existing project](/studio/projects/add-app-module),\nor when you add a new activity within an app module.\n\nIn addition to activities, you can also add other Android project components\nto an existing app using templates. These templates include both code\ncomponents, such as services and fragments, and non-code components, such as\nfolders and XML files.\n\nThis page discusses how to add Android project components like activities to\nyour project and describes the commonly used activity templates available in\nAndroid Studio. Note that most templates depend on the [Android Support\nLibrary](/tools/support-library/features) to include user interface principles based on [Material Design](/design/material).\n\nAdd a project component\n-----------------------\n\n**Figure 1** . The templates menu, accessible through the\n**File** \\\u003e **New** menu or by right-clicking in the\n**Project** window.\n\nAndroid Studio groups templates by the type of component that they add, such\nas an **Activity** or an **XML** file, as shown in figure 1.\n\nTo add an Android project component using a template, use the **Project**\nwindow. Right-click on the\nfolder in which you want to add the new component, and select **New**. Based\non what components can be added to the folder you clicked on, you then see a\nlist of template types like those shown in figure 1.\n\nWhen you select the template you want to add, a corresponding wizard window\nappears and asks for the component's configuration information, such as its\nname. After you enter the configuration information, Android Studio creates\nand opens the files for your new component. It also runs a Gradle build to\nsync your project.\n\nSelect an activity template\n---------------------------\n\n**Figure 2**. The Empty Compose Activity template.\n\nOne of the most common uses of templates is adding new activities to an\nexisting app module. There are templates for creating screens for logging\ninto an account, presenting a list of items with details, or scrolling\nthrough a long block of text.\n\nAndroid Studio also provides templates for a variety of different app module\ntypes, including Wear OS, Android TV, and Cloud App Engine. You can view\ntemplates for these different module types when [adding a project component](#FindTemplates).\nTemplates also exist for more API-specific modules and activities, such as\nGoogle AdMobs Ads and Google Maps.\n\nOne of the most commonly used templates is the Empty Compose Activity\ntemplate, which creates an empty activity with a sample composable and a\npreview of the composable. It lets you to start from scratch when building\nyour app module or activity.\n\nUse the Gemini API template\n---------------------------\n\n| **Note:** The Gemini API template has been temporarily removed starting in Android Studio Narwhal \\| 2025.1.1. To use the template, download a previous version of Android Studio, which still supports the template, from the [Android Studio\n| download archive](/studio/archive) as a temporary workaround.\n\nUse the Gemini API template to build an application that implements\nGenerative AI using the [Google AI SDK](http://ai.google.dev/tutorials/android_quickstart?utm_source=android&utm_medium=referral).\n\n### Step 1: Build on the New Project template for AI\n\nStart Android Studio and open a new project using **File \\\u003e New Project** .\nSelect the **Gemini API Starter** template.\nChoose the Gemini API template on the **New Project** screen.\n\n### Step 2: Generate the API key\n\nIn the next step of the wizard, after you choose a project name and location,\nprovide an API key for authentication to the Gemini API. If you don't have a\nGemini API key, click the link provided in the wizard to navigate to\n[Google AI Studio](https://makersuite.google.com/app/apikey) and request a\nnew key. Once completed, copy your new API key back into the wizard. Click\n**Finish**.\nNew Project wizard for the Gemini API template. Copy and paste the API key into the New Project wizard.\n\n### Step 3: Start prototyping\n\nAndroid Studio automatically sets up a project for you with a connection to\nthe Gemini API, simplifying your workflow. Click **Run** to see the code in\naction in the Android Emulator. The app comes with a hard-coded prompt\nasking the model to \"Summarize the following text for me\"; you can edit or\nexpand the prompt directly in the code to modify what it can do. You can\nlearn more about creating prompts in the [Google AI Studio documentation](https://ai.google.dev/gemini-api/docs/ai-studio-quickstart).\nStart prototyping with Gemini APIs. **Important:** Remember, your API key is a secret! Don't share it with anyone, and it is strongly recommended that you don't check in an API key into your version control. The Gemini API Starter template stores the API key in a `local.properties` file (which is located in your project's root directory, but excluded from version control) and uses the [Secrets Gradle plugin](https://github.com/google/secrets-gradle-plugin) for Android to read the API key as a build configuration variable.\n\n### Learn more\n\nLearn more about the Google AI SDK for Android by reviewing the Google AI SDK\nfor Android [quick start guide](http://ai.google.dev/tutorials/android_quickstart?utm_source=android&utm_medium=referral).\nAnd for even more code samples, you can import the Generative AI code sample\ninto Android Studio through **File \\\u003e New \\\u003e Import Sample** and searching for\n**Generative AI Sample**.\nImport Generative AI Sample Wizard.\n\nTry out Gemini's chat, text, and multi-modal capabilities in the sample app.\n\n\u003cbr /\u003e\n\nGemini's chat, text and multi-modal capabilities. c\n\n\u003cbr /\u003e"]]